Algorithms and Data Structures with PythonChapter 105

Listing All Contacts

Section 5 of 5-~ 12 min read-Synced from Cuantum content

Finally, let's implement a functionality to list all contacts in alphabetical order, which can be achieved through an in-order traversal of the BST.

class ContactBookBST:    # ... previous methods ...     def in_order_traversal(self, root):        if root:            self.in_order_traversal(root.left)            print(f"{root.name}: {root.phone}, {root.email}")            self.in_order_traversal(root.right)     def list_contacts(self):        self.in_order_traversal(self.root) # Example Usagecontact_book.list_contacts()