Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Four possible case to delete root of binary search tree

Case 1:When only one node of linked list. that means no left child and right child .

50Delete RootNULLNULL

After delete root node tree are empty.

Case 2: No left sub tree.


Move root to right child and delete root.

64 root908591

After delete


Case 3: No right sub tree.


Move root to left child and delete root.

6416 root8410

After delete


Case 4: When left and right subtree are exist.

Example to Delete Root node. Given binary search tree.


Swap node data [64<=>44].


After Delete


Try it yourself


Code execution:

Stack Areamainroot(pointer)delete_roothelp(pointer)root(pointer)temp(pointer) Heap Areastruct Treedata (int)= 64left_child (pointer) right_child (pointer) struct Treedata (int)= 16left_child (pointer) right_child (pointer) struct Treedata (int)= 90left_child (pointer) right_child (pointer) struct Treedata (int)= 8left_child (pointer) right_child (pointer) struct Treedata (int)= 44left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 85left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 91left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 4left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 10left_child (pointer)= NULLright_child (pointer)= NULL

Note that not given all step of execution process here.View How to insert bst node and delete also.

Try it yourself

C program to delete Root node of binary search tree.


Spread the post