BST Class - 3

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 185:-BST Class - 3 

Certainly, let's continue enhancing our Binary Search Tree (BST) class by adding more functionalities such as finding the minimum and maximum values, checking if a given value exists in the tree, and calculating the height of the tree.

Here's the next part 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) # ... (previous code) def find_min(self): if self.root is None: return None current = self.root while current.left: current = current.left return current.data def find_max(self): if self.root is None: return None current = self.root while current.right: current = current.right return current.data def exists(self, data): return self._exists_recursively(self.root, data) def _exists_recursively(self, node, data): if node is None: return False if node.data == data: return True if data < node.data: return self._exists_recursively(node.left, data) else: return self._exists_recursively(node.right, data) def height(self): return self._height_recursively(self.root) def _height_recursively(self, node): if node is None: return -1 left_height = self._height_recursively(node.left) right_height = self._height_recursively(node.right) 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("Min value:", bst.find_min()) print("Max value:", bst.find_max()) search_value = 7 print(f"Value {search_value} exists in the tree:", bst.exists(search_value)) print("Height of the tree:", bst.height())

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

Find Minimum and Maximum: The find_min and find_max methods find the minimum and maximum values in the binary search tree, respectively, by traversing left (for minimum) and right (for maximum) until reaching the leaf nodes.

Exists: The exists method checks whether a given value exists in the binary search tree or not.

Height: The height method calculates the height of the binary search tree by recursively calculating the maximum height of its left and right subtrees.

These additional functionalities enhance the capabilities of the Binary Search Tree class and allow you to perform common operations associated with 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?