Câu hỏi:

11/07/2024 154

Trong bài 9, chúng ta đã học thao tác duyệt cây. Với bài toán thực tế quản lí danh bạn điện thoại, làm thế nào để sử dụng các thao tác đó vào cây tìm kiếm nhị phân để thêm, tìm kiếm, hiển thị toàn bộ các liên hệ theo thứ tự sắp xếp của tên lên hệ trong danh bạ.

Quảng cáo

Trả lời:

verified
Giải bởi Vietjack

Với bài toán thực tế quản lí danh bạn điện thoại, để sử dụng các thao tác đó vào cây tìm kiếm nhị phân để thêm, tìm kiếm, hiển thị toàn bộ các liên hệ theo thứ tự sắp xếp của tên lên hệ trong danh bạ ta phải viết ứng dụng quản lí danh bạ, sử dụng cấu trúc dữ liệu cây tìm kiếm nhị phân để viết ứng dụng này.

CÂU HỎI HOT CÙNG CHỦ ĐỀ

Lời giải

Để hiển thị các món trong tệp menu.inp theo thứ tự giá tiền tăng dần bằng cây tìm kiếm nhị phân, chúng ta cần đọc dữ liệu từ tệp, sau đó chèn mỗi món vào cây tìm kiếm nhị phân dựa trên giá tiền của món. Nếu có nhiều món có cùng giá tiền, chúng ta có thể sử dụng danh sách liên kết hoặc danh sách kết hợp để lưu trữ các món có cùng giá tiền. Dưới đây là một cách để thực hiện điều này:

class MenuItem:

    def __init__(self, name, price):

        self.name = name

        self.price = price

class TreeNode:

    def __init__(self, menu_item):

        self.menu_item = menu_item

        self.left = None

        self.right = None

        self.same_price = []  # Danh sách các món có cùng giá tiền

class MenuDatabase:

    def __init__(self):

        self.root = None

    def insert(self, menu_item):

        self.root = self._insert_recursive(self.root, menu_item)

    def _insert_recursive(self, root, menu_item):

        if root is None:

            return TreeNode(menu_item)

        if menu_item.price < root.menu_item.price:

            root.left = self._insert_recursive(root.left, menu_item)

        elif menu_item.price > root.menu_item.price:

            root.right = self._insert_recursive(root.right, menu_item)

        else:

            root.same_price.append(menu_item)

        return root

    def display_menu_by_price(self, root):

        if root:

           self.display_menu_by_price(root.left)

            print("Name:", root.menu_item.name, "- Price:", root.menu_item.price)

            for item in root.same_price:

                print("Name:", item.name, "- Price:", item.price)

           self.display_menu_by_price(root.right)

# Đọc dữ liệu từ tệp menu.inp và chèn mỗi món vào cây tìm kiếm nhị phân

menu_db = MenuDatabase()

with open("menu.inp", "r") as file:

    for line in file:

        name, price = line.strip().split(", ")

        menu_item = MenuItem(name, float(price))

        menu_db.insert(menu_item)

# In danh sách món theo thứ tự giá tiền tăng dần

print("Menu sorted by price (ascending):")

menu_db.display_menu_by_price(menu_db.root)

Lời giải

Bản phác thảo Python mẫu cho chương trình quản lí danh sách học sinh của một lớp sử dụng cây tìm kiếm nhị phân:

class Student:

    def __init__(self, student_id, full_name, date_of_birth):

        self.student_id = student_id

        self.full_name = full_name

        self.date_of_birth = date_of_birth

class TreeNode:

    def __init__(self, student):

        self.student = student

        self.left = None

        self.right = None

class StudentDatabase:

    def __init__(self):

        self.root = None

    def insert(self, student):

        self.root = self._insert_recursive(self.root, student)

    def _insert_recursive(self, root, student):

        if root is None:

            return TreeNode(student)

        if student.student_id < root.student.student_id:

            root.left = self._insert_recursive(root.left, student)

        elif student.student_id > root.student.student_id:

            root.right = self._insert_recursive(root.right, student)

        return root

    def search(self, student_id):

        return self._search_recursive(self.root, student_id)

    def _search_recursive(self, root, student_id):

        if root is None or root.student.student_id == student_id:

            return root.student if root else None

        if student_id < root.student.student_id:

            return self._search_recursive(root.left, student_id)

        else:

            return self._search_recursive(root.right, student_id)

    def display_students_in_order(self, root):

        if root:

           self.display_students_in_order(root.left)

            print("ID:", root.student.student_id, "- Name:", root.student.full_name, "- Date of Birth:", root.student.date_of_birth)

           self.display_students_in_order(root.right)

    def display_students_in_reverse_order(self, root):

        if root:

           self.display_students_in_reverse_order(root.right)

            print("ID:", root.student.student_id, "- Name:", root.student.full_name, "- Date of Birth:", root.student.date_of_birth)

           self.display_students_in_reverse_order(root.left)

# Sử dụng

student_db = StudentDatabase()

# Thêm học sinh mới

student_db.insert(Student(101, "John Doe", "2005-01-15"))

student_db.insert(Student(102, "Alice Smith", "2004-08-20"))

student_db.insert(Student(103, "Bob Johnson", "2005-03-10"))

# In danh sách học sinh theo thứ tự mã từ nhỏ đến lớn

print("Students sorted by ID (ascending):")

student_db.display_students_in_order(student_db.root)

# In danh sách học sinh theo thứ tự mã từ lớn đến nhỏ

print("\nStudents sorted by ID (descending):")

student_db.display_students_in_reverse_order(student_db.root)

# Tìm kiếm học sinh theo mã

search_id = 102

found_student = student_db.search(search_id)

if found_student:

    print("\nStudent found - ID:", found_student.student_id, "- Name:", found_student.full_name, "- Date of Birth:", found_student.date_of_birth)

else:

    print("\nStudent with ID", search_id, "not found.")

Lời giải

Bạn cần đăng ký gói VIP ( giá chỉ từ 199K ) để làm bài, xem đáp án và lời giải chi tiết không giới hạn.

Nâng cấp VIP

Vietjack official store
Đăng ký gói thi VIP

VIP +1 - Luyện thi tất cả các đề có trên Website trong 1 tháng

  • Hơn 100K đề thi thử, đề minh hoạ, chính thức các năm
  • Với 2tr+ câu hỏi theo các mức độ Nhận biết, Thông hiểu, Vận dụng
  • Tải xuống đề thi [DOCX] với đầy đủ đáp án
  • Xem bài giảng đính kèm củng cố thêm kiến thức
  • Bao gồm tất cả các bậc từ Tiểu học đến Đại học
  • Chặn hiển thị quảng cáo tăng khả năng tập trung ôn luyện

Mua ngay

VIP +3 - Luyện thi tất cả các đề có trên Website trong 3 tháng

  • Hơn 100K đề thi thử, đề minh hoạ, chính thức các năm
  • Với 2tr+ câu hỏi theo các mức độ Nhận biết, Thông hiểu, Vận dụng
  • Tải xuống đề thi [DOCX] với đầy đủ đáp án
  • Xem bài giảng đính kèm củng cố thêm kiến thức
  • Bao gồm tất cả các bậc từ Tiểu học đến Đại học
  • Chặn hiển thị quảng cáo tăng khả năng tập trung ôn luyện

Mua ngay

VIP +6 - Luyện thi tất cả các đề có trên Website trong 6 tháng

  • Hơn 100K đề thi thử, đề minh hoạ, chính thức các năm
  • Với 2tr+ câu hỏi theo các mức độ Nhận biết, Thông hiểu, Vận dụng
  • Tải xuống đề thi [DOCX] với đầy đủ đáp án
  • Xem bài giảng đính kèm củng cố thêm kiến thức
  • Bao gồm tất cả các bậc từ Tiểu học đến Đại học
  • Chặn hiển thị quảng cáo tăng khả năng tập trung ôn luyện

Mua ngay

VIP +12 - Luyện thi tất cả các đề có trên Website trong 12 tháng

  • Hơn 100K đề thi thử, đề minh hoạ, chính thức các năm
  • Với 2tr+ câu hỏi theo các mức độ Nhận biết, Thông hiểu, Vận dụng
  • Tải xuống đề thi [DOCX] với đầy đủ đáp án
  • Xem bài giảng đính kèm củng cố thêm kiến thức
  • Bao gồm tất cả các bậc từ Tiểu học đến Đại học
  • Chặn hiển thị quảng cáo tăng khả năng tập trung ôn luyện

Mua ngay