Binary Search Tree
Binary Search Tree
1. Search Operation
2. Insertion Operation
3. Deletion Operation
1. Search Operation-
Search Operation is performed to search a particular element in the Binary Search Tree.
Rules-
For searching a given key in the BST,
Example-
2. Insertion Operation-
Rules-
The insertion of a new key always takes place as the child of some leaf node.
For finding out the suitable leaf node,
• Search the key to be inserted from the root node till some leaf node is reached.
• Once a leaf node is reached, insert the key as child of that leaf node.
Example-
Consider the following example where key = 40 is inserted in the given BST-
3. Deletion Operation-
Deletion Operation is performed to delete a particular element from the Binary Search Tree.
When it comes to deleting a node from the binary search tree, following three cases are possible-
Just remove / disconnect the leaf node that is to deleted from the tree.
Example-
Consider the following example where node with value = 20 is deleted from the BST-
Just make the child of the deleting node, the child of its grandparent.
Example-
Consider the following example where node with value = 30 is deleted from the BST-
A node with two children may be deleted from the BST in the following two ways-
Method-01:
Example-
Consider the following example where node with value = 15 is deleted from the BST-
Method-02:
Example-
Consider the following example where node with value = 15 is deleted from the BST-