Binary Search Tree(BST) - Insertion And Deletion

 Binary Search Tree(BST) 

Insertion And Deletion



Binary Search tree is a Data structure in which the elements are saved in the form of nodes such that they satisfy the following conditions:

1. The left subtree of a node has a key less than or equal to its parent node's key.

2. The right sub-tree of a node has a key greater than to its parent node's key


Insertion Operation 

Whenever an element is to be inserted, first loate its proper location. Start searching from the root node, then if the data is less than the value, search for empty location in the left sub-tree and insert the data,otherwise, search for an empty location in right sub-tree and insert the data.


Deletion Operation

The deletion Operation is performed as per the following three cases:

CASE 1: NO CHILD

For deleting a node with no child, simply remove the node from the tree.

CASE 2: ONE CHILD

For deleting a node with one child, simply remove the node and replace it with its child.

CASE 3: TWO CHILD

Suppose the node that is to be deleted is X. Do not delete X, choose either its in-order successor or its in-order predeccessor node R.

Copy the value of R to X, then recursively call delete on R until reaching one of the first two nodes.

If you choose in-order successor of a node, as right sub-tree is not NULL, then its in-order successor is node with least value in its right sub-tree, which will have a maximum of 1 sub-tree, So deleting it would fall in one of the first two cases 



C Program implementing Insertion and Deletion in BST





SHARE If you find this useful, please share with your friends and Community.
CODE TOGETHER..GROW TOGETHER.
Newer Posts Newer Posts Older Posts Older Posts

More posts

Comments

Post a Comment

Sponsored Content