Department of Computer EngineeringData Structures — Lab ManualCS-301 · PR. 04
Implement a binary search tree andtraverse it in-order.
AimTo construct a binary search tree from a set of integer keysand print them in ascending order using in-order traversal.
TheoryA binary search tree is a rooted tree in which every node'sleft subtree holds smaller keys and its right subtree larger.The invariant holds at every node, so an in-order walk sorts.
Algorithm- 1. Start with an empty root pointer.
- 2. For each key, descend left if smaller, right if larger.
- 3. Insert at the first empty position reached.
- 4. Traverse: left subtree, node, right subtree.
Codeclass Node: def __init__(self, key): self.key = key self.left = self.right = Nonedef insert(root, key): if root is None: return Node(key) return root