Using C++
Aim : .
To implement the following operations on a Binary Search Tree (BST):
- Find the Minimum Key
- Find the Maximum Key
- Search for a Given Key
Theory
A Binary Search Tree (BST) is a hierarchical data structure that follows these properties:
- The left subtree contains values smaller than the node.
- The right subtree contains values greater than the node.
- No duplicate values are allowed.
Algorithm:
1️⃣ Insert a Node into BST
- If the tree is empty, create a new node and set it as the root.
- Compare the value with the root:
- If smaller, insert into the left subtree.
- If larger, insert into the right subtree.
2️⃣ Find the Minimum Key
- Start from the root.
- Keep moving left until you reach a node with no left child.
- That node contains the minimum key.
3️⃣ Find the Maximum Key
- Start from the root.
- Keep moving right until you reach a node with no right child.
- That node contains the maximum key.
4️⃣ Search for a Key in BST
- Start at the root.
- If the root is NULL, the key does not exist.
- If the root's value matches the key, return true.
- If the key is smaller, search in the left subtree.
- If the key is larger, search in the right subtree.
Program :
Conclusion : Hence we have performed our
practical successfully