BST Class - 2

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 184:-  BST Class - 2

Certainly! Let's continue enhancing our Binary Search Tree (BST) class by adding more functionalities such as deletion, traversal methods, and checking whether the tree is balanced or not. Additionally, we'll add methods to perform in-order, pre-order, and post-order traversals.

Here's the continuation of the Binary Search Tree class:

pythonCopy code

class BinarySearchTree: def __init__(self): self.root = None def insert(self, data): self.root = self._insert_recursively(self.root, data) def _insert_recursively(self, node, data): if node is None: return TreeNode(data) if data < node.data: node.left = self._insert_recursively(node.left, data) elif data > node.data: node.right = self._insert_recursively(node.right, data) return node def search(self, data): return self._search_recursively(self.root, data) def _search_recursively(self, node, data): if node is None or node.data == data: return node if data < node.data: return self._search_recursively(node.left, data) else: return self._search_recursively(node.right, data) def delete(self, data): self.root = self._delete_recursively(self.root, data) def _delete_recursively(self, node, data): if node is None: return node if data < node.data: node.left = self._delete_recursively(node.left, data) elif data > node.data: node.right = self._delete_recursively(node.right, data) else: if node.left is None: return node.right elif node.right is None: return node.left min_right_subtree = self._find_min(node.right) node.data = min_right_subtree.data node.right = self._delete_recursively(node.right, min_right_subtree.data) return node def _find_min(self, node): while node.left: node = node.left return node def in_order_traversal(self): self._in_order_recursively(self.root) print() def _in_order_recursively(self, node): if node: self._in_order_recursively(node.left) print(node.data, end=" ") self._in_order_recursively(node.right) def pre_order_traversal(self): self._pre_order_recursively(self.root) print() def _pre_order_recursively(self, node): if node: print(node.data, end=" ") self._pre_order_recursively(node.left) self._pre_order_recursively(node.right) def post_order_traversal(self): self._post_order_recursively(self.root) print() def _post_order_recursively(self, node): if node: self._post_order_recursively(node.left) self._post_order_recursively(node.right) print(node.data, end=" ") def is_balanced(self): return self._is_balanced_recursively(self.root) is not None def _is_balanced_recursively(self, node): if node is None: return 0 left_height = self._is_balanced_recursively(node.left) right_height = self._is_balanced_recursively(node.right) if left_height is None or right_height is None: return None if abs(left_height - right_height) > 1: return None return max(left_height, right_height) + 1 # Example usage bst = BinarySearchTree() bst.insert(10) bst.insert(5) bst.insert(15) bst.insert(3) bst.insert(7) bst.insert(12) bst.insert(18) print("In-order traversal:") bst.in_order_traversal() print("Pre-order traversal:") bst.pre_order_traversal() print("Post-order traversal:") bst.post_order_traversal() delete_value = 10 bst.delete(delete_value) print(f"Deleted node with value {delete_value}") bst.in_order_traversal() print("Is the tree balanced?", bst.is_balanced())

In this continuation, we've added the following functionalities to the BinarySearchTree class:

Delete Node: The delete method allows you to delete a node with a given value from the tree. It maintains the binary search tree property while deleting nodes.

Traversal Methods: The in_order_traversal, pre_order_traversal, and post_order_traversal methods perform in-order, pre-order, and post-order traversals of the binary search tree, respectively.

Is Balanced: The is_balanced method checks whether the binary search tree is balanced or not. It uses a similar approach as before to calculate the heights of subtrees while checking for balance.

This enhanced Binary Search Tree class now includes more functionalities commonly associated with binary search trees. You can further expand it by adding other operations and algorithms related to binary search trees.

26. Binary Search Tree

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?