BST @62
BST @62
DATA STRUCTURES
Date:
Description:
A Binary Search Tree (BST) is a data structure that maintains elements in a sorted order, allowing for efficient search,
insertion, and deletion operations. Each node in a BST contains a key (value), and each node has at most two
children, referred to as the left and right children. The left child contains values less than the parent node, while the
right child contains values greater than the parent node.
1. Ordered Structure:
▪ All values in the left subtree are less than the node’s value.
▪ All values in the right subtree are greater than the node’s value.
2. Unique Values:
o In a standard BST, each value is unique. However, variations can allow duplicate values.
3. Dynamic Size:
o A BST can grow and shrink dynamically as elements are inserted and deleted.
Common Operations:
1. Insertion:
o Insert a new value into the BST while maintaining the ordering property.
o Start at the root and recursively find the correct position for the new value.
2. Search:
o Start at the root and recursively traverse left or right depending on whether the value is less than or
greater than the current node’s value.
3. Deletion:
o Remove a value from the BST while maintaining the ordering property.
3. Node to be deleted has two children (replace with the minimum value from the right
subtree or maximum from the left subtree).
Laboratory record of: Roll no: 160123749062
DATA STRUCTURES
Date:
4. Traversal:
o In-Order Traversal: Visits nodes in ascending order. Left subtree, root, right subtree.
o Post-Order Traversal: Visits nodes in the order of left subtree, right subtree, root.
Analysis:
Time Complexity:
● Insertion: O(h), where h is the height of the tree. In a balanced BST, this is O(log n); in the worst case
(unbalanced), it can be O(n).
Space Complexity:
● The space complexity for operations (other than the space used for the BST itself) is O(h) due to the
recursive stack in function calls, where h is the height of the tree.
Algorithm:
Steps:
o Create a new node with the given key and assign it as the root.
2. Otherwise, call the recursive helper function _insert_recursive(node, key) with the root node.
Steps:
1. Call the recursive helper function _search_recursive(node, key) with the root node.
Steps:
1. Call the recursive helper function _delete_recursive(node, key) with the root node.
● Input: None
● Input: None
● Input: None
CODE :
class TreeNode:
self.left = None
self.right = None
self.val = key
class BinarySearchTree:
def __init__(self):
self.root = None
if self.root is None:
self.root = TreeNode(key)
Laboratory record of: Roll no: 160123749062
DATA STRUCTURES
Date:
else:
self._insert_recursive(self.root, key)
if node.left is None:
node.left = TreeNode(key)
else:
self._insert_recursive(node.left, key)
if node.right is None:
node.right = TreeNode(key)
else:
self._insert_recursive(node.right, key)
return node
if node is None:
return node
else:
if node.left is None:
return node.right
return node.left
min_larger_node = self._min_value_node(node.right)
node.val = min_larger_node.val
return node
current = node
current = current.left
return current
def in_order_traversal(self):
return self._in_order_recursive(self.root)
def pre_order_traversal(self):
return self._pre_order_recursive(self.root)
def post_order_traversal(self):
return self._post_order_recursive(self.root)
def main():
bst = BinarySearchTree()
while True:
print("\nMenu:")
print("7. Exit")
if choice == '1':
bst.insert(value)
found_node = bst.search(value)
if found_node:
else:
bst.delete(value)
print("Exiting...")
break
else:
if __name__ == "__main__":
main()
Output:
Menu:
1. Insert a value
3. Delete a value
4. In-order traversal
5. Pre-order traversal
6. Post-order traversal
7. Exit
Menu:
1. Insert a value
3. Delete a value
4. In-order traversal
5. Pre-order traversal
Laboratory record of: Roll no: 160123749062
DATA STRUCTURES
Date:
6. Post-order traversal
7. Exit
Menu:
1. Insert a value
3. Delete a value
4. In-order traversal
5. Pre-order traversal
6. Post-order traversal
7. Exit
Result:
The binary search tree (BST) is successfully implemented.