Upload
lovelyn-rose
View
10.092
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Simple algorithm to insert and delete elements in binary search tree explained using animation. Algorithms for array and linked list implementation are given.
Citation preview
(Download file to view animation)
Dr. S. Lovelyn Rose
PSG College of Technology
Coimbatore, India
Binary Search TreesInsertion and Deletion
(using Arrays and Linked lists)
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
Insertion in a Binary Search Tree Using Linked Lists
1.temp=root; 2 if(root == null)3. rootdata = element;4. Return;5. end if6. while(1) 7. if(tempdata >
element)8. if(templchild ==
null)9.templchild =
GetNode()10.templchild data
= element10.break;11.end if
Insert as first node
2
1
0
5
83
Element to be inserted is 4
temp
12.else13.temp = templchild14.end else15.end if16.else17.if(temprchild ==
null)18.temprchild =
GetNode()19.temprchild data
= element20.break;21.end if
2
1
0
5
83
Element to be inserted is 4
temp
12.else13.temp = temprchild //Move to the right child23.end else24.end if25.end while26. end Insert_LinkedList_BST
2
1
0
5
83
Element to be inserted is 4
temp
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
Delete a node in a BST (using Linked List)
//Search for the element
1. temp = root
2. parent = root
3. while((element != tempdata) && (temp != null))
// Loop until the element is found
4.parent = temp
5.if(element < tempdata)
6.temp = temp left // Move left
7.end if
8.else
9.temp = tempright //Move right
10.end else
11.end while
5
3
2 4
7
9
Element to be searched
is 4
12. If(temp != null)
13.if((templchild == null) and (temprchild == null))
14.if(parent == temp)
15.root = null
16.end if
17. else if(temp == parentlchild)
18. parentlchild = null
19. end else if
else
21.parentrchild = null
22.end else
23.FreeNode(temp)
24.end if
Element found
temp is a leaf node
Only one node
5
4 73
2 4
// Case 2 - Delete node with one child
else if((templchild == null)or(temprchild == null))
26.if(parent == temp)
27.if(templchild != null)
28.root = templchild
29.end if
30.else
31.root = temprchild
32.end else
33.end if
4
2
root
else if(temp == parentlchild)
35.if(templchild != null)
36.parentlchild = templchild
37.end if
38.else
39.parentlchild = temprchild
40.end else
41.end else if
else
43.if(templchild != null)
44.parentrchild = templchild
45.end if
46.else
47. parentrchild = temprchild
48.end else
49.end else if
50.FreeNode(temp)
51.end else if
5
4
2
7
9
temp is a left child
temp is a right child
//Case 3 – Delete node with 2 children
52.else53.parent_inorder_N = temp54.inorder_N = tempright subtree55.Repeat steps 56,57
while inorder_Nleft ≠ null56.parent_inorder_N = inorder_N57.inorder_N = inorder_N left58.end Repeat59.tempdata = inorder_Ndata60. if(inorder_N rchild == null)61. parent_ inorder_Nleft = null62. end if
2
1
0
4
53
6
Parent_inorder_N
inorder_N
6
63.else // Inorder successor has one child64. parent_ inorder_Nleft = inorder_Nright65.end else66. end else67.end if68.else69.Print “Element not Found”70.end else71.end Delete_LinkedList_BST
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
Insertion in a Binary Search Tree Using Arrays
1. temp=1;2. if(Tree[temp] == -1)3.Tree[temp] = element;4. Return;5. end if6. while(1)7. if(Tree[temp] > element)8. if(Tree[2 * temp] == -1)9. Tree[2 * temp] ==
element10.break;11. end if
10 15 -1 -1 -1 -14
Element to be inserted is 6
Insert as first node
To find the position
12.else13.temp = 2 * temp14.end else15.end if16. else17. if(Tree[2 * temp+1]
== -1)18.Tree[2 * temp+1]
== element child19.break;20.end if
6 -1-1
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
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
Delete a node in a BST using Arrays
//Search for the ‘element’ in the BST
1. 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 right8. end else9. end while 10 4 15 -1 -1 -1 -1
Element to be searched is 4
temp
10. if(Tree[temp] != -1) // If the element is found
// Case 1 - Delete leaf node
11.if((Tree[2*temp] == -1) and (Tree[2*temp+1] == -1))
12.Tree[temp] = -113.end if
10 4 15 -1 -1 -1 -1
temp
-1
// Case 2 - Delete node with one child
14.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 temp
16.Call Preorder(Tree[1:N], 2*temp) //Update the whole subtree
17.end if18.else19.Call Preorder(Tree[1:N], 2*temp+1)20.end else21.end else if
10 15 -1 7 -1 -1
temp
4 -1-1
//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
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
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
Insertion in a Binary Search Tree Using Linked Lists
1.temp=root; 2 if(root == null)3. rootdata = element;4. Return;5. end if6. while(1) 7. if(tempdata >
element)8. if(templchild ==
null)9.templchild =
GetNode()10.templchild data
= element10.break;11.end if
Insert as first node
2
1
0
5
83
Element to be inserted is 4
temp
12.else13.temp = templchild14.end else15.end if16.else17.if(temprchild ==
null)18.temprchild =
GetNode()19.temprchild data
= element20.break;21.end if
2
1
0
5
83
Element to be inserted is 4
temp
12.else13.temp = temprchild //Move to the right child23.end else24.end if25.end while26. end Insert_LinkedList_BST
2
1
0
5
83
Element to be inserted is 4
temp
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
Delete a node in a BST (using Linked List)
//Search for the element
1. temp = root
2. parent = root
3. while((element != tempdata) && (temp != null))
// Loop until the element is found
4.parent = temp
5.if(element < tempdata)
6.temp = temp left // Move left
7.end if
8.else
9.temp = tempright //Move right
10.end else
11.end while
5
3
2 4
7
9
Element to be searched
is 4
12. If(temp != null)
13.if((templchild == null) and (temprchild == null))
14.if(parent == temp)
15.root = null
16.end if
17. else if(temp == parentlchild)
18. parentlchild = null
19. end else if
else
21.parentrchild = null
22.end else
23.FreeNode(temp)
24.end if
Element found
temp is a leaf node
Only one node
5
4 73
2 4
// Case 2 - Delete node with one child
else if((templchild == null)or(temprchild == null))
26.if(parent == temp)
27.if(templchild != null)
28.root = templchild
29.end if
30.else
31.root = temprchild
32.end else
33.end if
4
2
root
else if(temp == parentlchild)
35.if(templchild != null)
36.parentlchild = templchild
37.end if
38.else
39.parentlchild = temprchild
40.end else
41.end else if
else
43.if(templchild != null)
44.parentrchild = templchild
45.end if
46.else
47. parentrchild = temprchild
48.end else
49.end else if
50.FreeNode(temp)
51.end else if
5
4
2
7
9
temp is a left child
temp is a right child
//Case 3 – Delete node with 2 children
52.else53.parent_inorder_N = temp54.inorder_N = tempright subtree55.Repeat steps 56,57
while inorder_Nleft ≠ null56.parent_inorder_N = inorder_N57.inorder_N = inorder_N left58.end Repeat59.tempdata = inorder_Ndata60. if(inorder_N rchild == null)61. parent_ inorder_Nleft = null62. end if
2
1
0
4
53
6
Parent_inorder_N
inorder_N
6
63.else // Inorder successor has one child64. parent_ inorder_Nleft = inorder_Nright65.end else66. end else67.end if68.else69.Print “Element not Found”70.end else71.end Delete_LinkedList_BST
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
Insertion in a Binary Search Tree Using Arrays
1. temp=1;2. if(Tree[temp] == -1)3.Tree[temp] = element;4. Return;5. end if6. while(1)7. if(Tree[temp] > element)8. if(Tree[2 * temp] == -1)9. Tree[2 * temp] ==
element10.break;11. end if
10 15 -1 -1 -1 -14
Element to be inserted is 6
Insert as first node
To find the position
12.else13.temp = 2 * temp14.end else15.end if16. else17. if(Tree[2 * temp+1]
== -1)18.Tree[2 * temp+1]
== element child19.break;20.end if
6 -1-1
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
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
Delete a node in a BST using Arrays
//Search for the ‘element’ in the BST
1. 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 right8. end else9. end while 10 4 15 -1 -1 -1 -1
Element to be searched is 4
temp
10. if(Tree[temp] != -1) // If the element is found
// Case 1 - Delete leaf node
11.if((Tree[2*temp] == -1) and (Tree[2*temp+1] == -1))
12.Tree[temp] = -113.end if
10 4 15 -1 -1 -1 -1
temp
-1
// Case 2 - Delete node with one child
14.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 temp
16.Call Preorder(Tree[1:N], 2*temp) //Update the whole subtree
17.end if18.else19.Call Preorder(Tree[1:N], 2*temp+1)20.end else21.end else if
10 15 -1 7 -1 -1
temp
4 -1-1
//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
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
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 -1
1. 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
http://datastructuresinterview.blogspot.in/
http://talkcoimbatore.blogspot.in/
http://simpletechnical.blogspot.in/
My Blogs