Upcoming SlideShare
×

# Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)

10,834

Published on

Simple algorithm to insert and delete elements in binary search tree explained using animation. Algorithms for array and linked list implementation are given.

Published in: Education
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
10,834
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
275
0
Likes
2
Embeds 0
No embeds

No notes for slide

### Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)

1. 1. (Download file to view animation) Dr. S. Lovelyn Rose PSG College of Technology Coimbatore, India
2. 2. Algorithm Insert_LinkedList_BST(root, element)* root points to the root element of the linked list* ‘element’ is the element to the inserted* ‘data’ holds the data part of a node* lchild stores the address of the left child* rchild stores the address of the right child* GetNode() is a function that creates a node with a data part and 2 address parts and assigns null in the address parts
3. 3. 1.temp=root;2 if(root == null)3. rootdata = element; Insert as first node4. Return;5. end if6. while(1) Element to be inserted is 47. if(tempdata > element)8. if(templchild == null) 2 temp9.templchild = GetNode()10.templchild  data = 1 5 element10.break; 0 3 811.end if
4. 4. 12.else13.temp = templchild14.end else15.end if16.else17.if(temprchild == null) Element to be inserted is 418.temprchild = GetNode() 2 temp19.temprchild  data = element 1 520.break;21.end if 0 3 8
5. 5. 12.else13.temp = temprchild //Move to the right child23.end else24.end if25.end while26. end Insert_LinkedList_BST Element to be inserted is 4 2 temp 1 5 0 3 8
6. 6. Algorithm Delete_LinkedList_BST(root)* root is a pointer to the root node* ‘data’ holds the data part of a node* lchild stores the address of the left child* rchild stores the address of the right child* temp is used to find the address of the element to be deleted* parent stores the address of the parent of the element to be deleted* inorder_N is the inorder successor* parent_inorder_N is the parent of the inorder successor
7. 7. //Search for the element1. temp = root Element to be2. parent = root searched is 43. while((element != tempdata) && (temp != null))// Loop until the element is found4.parent = temp 55.if(element < tempdata)6.temp = temp left // Move left 3 77.end if8.else9.temp = tempright //Move right 2 4 910.end else11.end while
8. 8. 12. If(temp != null) temp is a leaf node Element found13.if((templchild == null) and (temprchild == null))14.if(parent == temp) Only one node15.root = null16.end if17. else if(temp == parentlchild)18. parentlchild = null 519. end else ifelse21.parentrchild = null 4 722.end else 323.FreeNode(temp)24.end if 2 4
9. 9. // Case 2 - Delete node with one childelse if((templchild == null)or(temprchild == null))26.if(parent == temp)27.if(templchild != null)28.root = templchild29.end if root30.else 431.root = temprchild32.end else33.end if 2
10. 10. else if(temp == parentlchild)35.if(templchild != null)36.parentlchild = templchild37.end if temp is a left child38.else39.parentlchild = temprchild40.end else 541.end else ifelse43.if(templchild != null) 4 744.parentrchild = templchild45.end if46.else 2 947. parentrchild = temprchild48.end else temp is a right child49.end else if50.FreeNode(temp)51.end else if
11. 11. //Case 3 – Delete node with 2 children52.else53.parent_inorder_N = temp54.inorder_N = tempright subtree 255.Repeat steps 56,57 Parent_inorder_N while inorder_Nleft ≠ null 1 6 456.parent_inorder_N = inorder_N57.inorder_N = inorder_N  left inorder_N58.end Repeat 0 3 559.tempdata = inorder_Ndata60. if(inorder_N rchild == null) 661. parent_ inorder_Nleft = null62. end if
12. 12. 63.else // Inorder successor has one child64. parent_ inorder_Nleft = inorder_Nright65.end else66. end else67.end if68.else69.Print “Element not Found”70.end else71.end Delete_LinkedList_BST
13. 13. Algorithm Insert_Array_BST(Tree[1:N], element)* Tree is an array of n elements representing the BST* ‘element’ is the element to the inserted* The empty positions in an array are denoted by -1
14. 14. 12.else1. temp=1;2. if(Tree[temp] == -1) 13.temp = 2 * temp 14.end else3.Tree[temp] = element; 15.end if4. Return; 16. else5. end if Insert as first node 17. if(Tree[2 * temp+1] == -1)6. while(1) To find the position 18.Tree[2 * temp+1] ==7. if(Tree[temp] > element) element child8. if(Tree[2 * temp] == -1) 19.break;9. Tree[2 * temp] == element 20.end if10.break;11. end if Element to be inserted is 6 10 4 15 -1 -1 6 -1 -1 -1 -1
15. 15. 21. else // The right child is occupied22.temp = 2 * temp + 1 //Move to the right child23. end else24. end if25. end while26. end Insert_Array_BST
16. 16. Algorithm Delete_Array_BST(Tree[1:N], element) * Tree is an array of n elements representing the BST * ‘element’ is the element to the inserted * The empty positions in an array are denoted by -1 * temp is used to find the subscript of the element to be deleted * inorder_N is the inorder successor
17. 17. //Search for the ‘element’ in the BST1. temp = 12. while((Tree[temp]!=element) && (Tree[temp] != -1)) // Loop until the element is found3.if(element < Tree[temp])4.temp = 2 * temp // Move left5.end if6. else7.temp = 2 * temp + 1 //Move right Element to be searched is 48. end else9. end while 10 4 15 -1 -1 -1 -1 temp
18. 18. 10. if(Tree[temp] != -1) // If the element is found// Case 1 - Delete leaf node11.if((Tree[2*temp] == -1) and (Tree[2*temp+1] == -1))12.Tree[temp] = -113.end if 10 -1 4 15 -1 -1 -1 -1 temp
19. 19. // Case 2 - Delete node with one child14.else if((Tree[2*temp] == -1) or (Tree[2*temp+1] == -1))15.if(Tree[2*temp] != -1) // Is the child in the left of temp16.Call Preorder(Tree[1:N], 2*temp) //Update the whole subtree17.end if18.else19.Call Preorder(Tree[1:N], 2*temp+1)20.end else21.end else if 10 4 15 -1 7 -1 -1 -1 -1 temp
20. 20. //Case 3 – Delete node with 2 children22.else23.inorder_N = 2*temp+1 // Inorder successor is surely in the right subtree24.Repeat steps 48,49 while Tree[2*inorder_N] ≠ -125.inorder_N = 2*inorder_N26.end Repeat27.Tree[temp] = Tree[inorder_N] // Replace with inorder successor28.if(Tree[2*inorder_N + 1] == -1) // Inorder successor has no child29.Tree[inorder_N] = -130.end if
21. 21. 31.else // Inorder successor has one child32.Call Preorder(Tree[1:N], 2*inorder_N+1)33.end else34.end else35.end if36.else37.Print “Element not Found”38.end else39.end Delete_Array_BST
22. 22. Algorithm Insert_LinkedList_BST(root, element)* root points to the root element of the linked list* ‘element’ is the element to the inserted* ‘data’ holds the data part of a node* lchild stores the address of the left child* rchild stores the address of the right child* GetNode() is a function that creates a node with a data part and 2 address parts and assigns null in the address parts
23. 23. 1.temp=root;2 if(root == null)3. rootdata = element; Insert as first node4. Return;5. end if6. while(1) Element to be inserted is 47. if(tempdata > element)8. if(templchild == null) 2 temp9.templchild = GetNode()10.templchild  data = 1 5 element10.break; 0 3 811.end if
24. 24. 12.else13.temp = templchild14.end else15.end if16.else17.if(temprchild == null) Element to be inserted is 418.temprchild = GetNode() 2 temp19.temprchild  data = element 1 520.break;21.end if 0 3 8
25. 25. 12.else13.temp = temprchild //Move to the right child23.end else24.end if25.end while26. end Insert_LinkedList_BST Element to be inserted is 4 2 temp 1 5 0 3 8
26. 26. Algorithm Delete_LinkedList_BST(root)* root is a pointer to the root node* ‘data’ holds the data part of a node* lchild stores the address of the left child* rchild stores the address of the right child* temp is used to find the address of the element to be deleted* parent stores the address of the parent of the element to be deleted* inorder_N is the inorder successor* parent_inorder_N is the parent of the inorder successor
27. 27. //Search for the element1. temp = root Element to be2. parent = root searched is 43. while((element != tempdata) && (temp != null))// Loop until the element is found4.parent = temp 55.if(element < tempdata)6.temp = temp left // Move left 3 77.end if8.else9.temp = tempright //Move right 2 4 910.end else11.end while
28. 28. 12. If(temp != null) temp is a leaf node Element found13.if((templchild == null) and (temprchild == null))14.if(parent == temp) Only one node15.root = null16.end if17. else if(temp == parentlchild)18. parentlchild = null 519. end else ifelse21.parentrchild = null 4 722.end else 323.FreeNode(temp)24.end if 2 4
29. 29. // Case 2 - Delete node with one childelse if((templchild == null)or(temprchild == null))26.if(parent == temp)27.if(templchild != null)28.root = templchild29.end if root30.else 431.root = temprchild32.end else33.end if 2
30. 30. else if(temp == parentlchild)35.if(templchild != null)36.parentlchild = templchild37.end if temp is a left child38.else39.parentlchild = temprchild40.end else 541.end else ifelse43.if(templchild != null) 4 744.parentrchild = templchild45.end if46.else 2 947. parentrchild = temprchild48.end else temp is a right child49.end else if50.FreeNode(temp)51.end else if
31. 31. //Case 3 – Delete node with 2 children52.else53.parent_inorder_N = temp54.inorder_N = tempright subtree 255.Repeat steps 56,57 Parent_inorder_N while inorder_Nleft ≠ null 1 6 456.parent_inorder_N = inorder_N57.inorder_N = inorder_N  left inorder_N58.end Repeat 0 3 559.tempdata = inorder_Ndata60. if(inorder_N rchild == null) 661. parent_ inorder_Nleft = null62. end if
32. 32. 63.else // Inorder successor has one child64. parent_ inorder_Nleft = inorder_Nright65.end else66. end else67.end if68.else69.Print “Element not Found”70.end else71.end Delete_LinkedList_BST
33. 33. Algorithm Insert_Array_BST(Tree[1:N], element)* Tree is an array of n elements representing the BST* ‘element’ is the element to the inserted* The empty positions in an array are denoted by -1
34. 34. 12.else1. temp=1;2. if(Tree[temp] == -1) 13.temp = 2 * temp 14.end else3.Tree[temp] = element; 15.end if4. Return; 16. else5. end if Insert as first node 17. if(Tree[2 * temp+1] == -1)6. while(1) To find the position 18.Tree[2 * temp+1] ==7. if(Tree[temp] > element) element child8. if(Tree[2 * temp] == -1) 19.break;9. Tree[2 * temp] == element 20.end if10.break;11. end if Element to be inserted is 6 10 4 15 -1 -1 6 -1 -1 -1 -1
35. 35. 21. else // The right child is occupied22.temp = 2 * temp + 1 //Move to the right child23. end else24. end if25. end while26. end Insert_Array_BST
36. 36. Algorithm Delete_Array_BST(Tree[1:N], element) * Tree is an array of n elements representing the BST * ‘element’ is the element to the inserted * The empty positions in an array are denoted by -1 * temp is used to find the subscript of the element to be deleted * inorder_N is the inorder successor
37. 37. //Search for the ‘element’ in the BST1. temp = 12. while((Tree[temp]!=element) && (Tree[temp] != -1)) // Loop until the element is found3.if(element < Tree[temp])4.temp = 2 * temp // Move left5.end if6. else7.temp = 2 * temp + 1 //Move right Element to be searched is 48. end else9. end while 10 4 15 -1 -1 -1 -1 temp
38. 38. 10. if(Tree[temp] != -1) // If the element is found// Case 1 - Delete leaf node11.if((Tree[2*temp] == -1) and (Tree[2*temp+1] == -1))12.Tree[temp] = -113.end if 10 -1 4 15 -1 -1 -1 -1 temp
39. 39. // Case 2 - Delete node with one child14.else if((Tree[2*temp] == -1) or (Tree[2*temp+1] == -1))15.if(Tree[2*temp] != -1) // Is the child in the left of temp16.Call Preorder(Tree[1:N], 2*temp) //Update the whole subtree17.end if18.else19.Call Preorder(Tree[1:N], 2*temp+1)20.end else21.end else if 10 4 15 -1 7 -1 -1 -1 -1 temp
40. 40. //Case 3 – Delete node with 2 children22.else23.inorder_N = 2*temp+1 // Inorder successor is surely in the right subtree24.Repeat steps 48,49 while Tree[2*inorder_N] ≠ -125.inorder_N = 2*inorder_N26.end Repeat27.Tree[temp] = Tree[inorder_N] // Replace with inorder successor28.if(Tree[2*inorder_N + 1] == -1) // Inorder successor has no child29.Tree[inorder_N] = -130.end if
41. 41. 31.else // Inorder successor has one child32.Call Preorder(Tree[1:N], 2*inorder_N+1)33.end else34.end else35.end if36.else37.Print “Element not Found”38.end else39.end Delete_Array_BST
42. 42. Algorithm Preorder(Tree[1:N], root)* Tree is the array holding the tree* root is the subscript of the root node* The empty positions in an array are denoted by -11. Tree[ceil(root/2)-1] = Tree[root]2. Tree[root] = -13. if(Tree[2*root] ≠ -1) // A left child exists4. call Preorder(Tree[], 2 * root)5. end if6. if ( Tree[2*root+1] ≠ -1) // Does a right child exist7. call Preorder(Tree[], 2 * root+1)8. end if9. end Preorder
43. 43. http://datastructuresinterview.blogspot.in/http://talkcoimbatore.blogspot.in/http://simpletechnical.blogspot.in/
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.