Câu hỏi:

26/06/2024 24

Sử dụng cây tìm kiếm nhị phân để viết chương trình quản lí danh sách học sinh của một lớp. Thông tin mỗi học sinh gồm mã (duy nhất), tên đầy đủ, ngày sinh. Chương trình cho phép thêm mới thông tin các học sinh, in danh sách sắp xếp theo mã từ nhỏ đến lớn và từ lớn đến nhỏ, tìm kiếm học sinh theo mã.

Siêu phẩm 30 đề thi thử THPT quốc gia 2024 do thầy cô VietJack biên soạn, chỉ từ 100k trên Shopee Mall.

Mua ngay

Quảng cáo

Trả lời:

verified
Giải bởi Vietjack

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.")

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

Câu 1:

Tiếp tục với ứng dụng quản lí danh bạ, chức năng hiển thị danh sách liên hệ theo thứ tự từ điển. Do hạn chế của màn hình, mỗi trang chỉ hiển thị được 20 liên hệ. Hãy thêm tính năng in các liên hệ ở trang n bất kì do người dùng nhập vào, điều kiện n nguyên, lớn hơn 0 và nhỏ hơn hoặc bằng tổng số trang có thể hiển thị.

Xem đáp án » 26/06/2024 15

Câu 2:

Sử dụng cây tìm kiếm nhị phân để hiển thị các món trong tệp menu.inp ở Bài 8 theo thứ tự giá tiền tăng dần. Mỗi dòng in ra gồm tên món và giá tiền. Nếu có hai hoặc nhiều món cùng giá tiền thì các món đó được hiển thị theo thứ tự xuất hiện trong tệp menu.inp.

Xem đáp án » 26/06/2024 15

Câu 3:

Hãy vẽ cây tìm kiếm nhị phân ứng với

a) Dữ liệu tệp contacts.inp ở trong phần thực hành.

b) Từ cây nhận được ở ý a, thêm liên hệ “Anh, Nguyễn Văn Tùng, 0982 000 134”.

Xem đáp án » 26/06/2024 14

Câu 4:

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ạ.

Xem đáp án » 26/06/2024 13

Bình luận


Bình luận