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.

64908591

Move root to right child and delete root.

64 root908591

After delete

908591

Case 3: No right sub tree.

64168410

Move root to left child and delete root.

6416 root8410

After delete

168410

Case 4: When left and right subtree are exist.

Example to Delete Root node. Given binary search tree.

6416908449185410

Swap node data [64<=>44].

4416908649185410

After Delete

44169088591410

Try it yourself

Algorithm:

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.

OutPut:

Spread the post