69
BST search BST deletion

4.Shamod 4-4 BST Search -Deletion

  • Upload
    robinpt

  • View
    213

  • Download
    0

Embed Size (px)

DESCRIPTION

4.Shamod 4-4 BST Search -Deletion

Citation preview

Page 1: 4.Shamod 4-4 BST Search -Deletion

BST searchBST deletion

Page 2: 4.Shamod 4-4 BST Search -Deletion

Searching in a binary TreeSearching for a data in a binary tree is much faster than in

arrays or linked lists->So the applications where frequent searching operations

are to be performed, this data structure is used->

To search for an item1. start from the root node, 2. if item < root node data

1. proceed to its left child->3. If item > root node data

1. proceed to its right child->4. Continue step 2 and 3 till the item is found or we reach to a

dead end (leaf node)

Page 3: 4.Shamod 4-4 BST Search -Deletion

Search 6

5

3 7

42 86

root

Page 4: 4.Shamod 4-4 BST Search -Deletion

Search 6

5

3 7

42 86

root

Steps:

1. ptr=root2. While(ptr!=NULL and ptr->data !=

item)1. If( item>ptr->data)

1. ptr=ptr->rchild2. Else

1. ptr=ptr->lchild3. EndIf

3. EndWhile4. If(ptr=NULL)

1. Print(“Item notFound”)2. exit

5. Else1. Print(“Item Found”)

6. EndIf7. Return (ptr)

ptr

Page 5: 4.Shamod 4-4 BST Search -Deletion

Search 6

5

3 7

42 86

root

Steps:

1. ptr=root2. While(ptr!=NULL and ptr->data !=

item)1. If( item>ptr->data)

1. ptr=ptr->rchild2. Else

1. ptr=ptr->lchild3. EndIf

3. EndWhile4. If(ptr=NULL)

1. Print(“Item notFound”)2. Exit

5. Else1. Print(“Item Found”)

6. EndIf7. Return (ptr)

ptr

Page 6: 4.Shamod 4-4 BST Search -Deletion

Search 6

5

3 7

42 86

root

Steps:

1. ptr=root2. While(ptr!=NULL and ptr->data !=

item)1. If( item>ptr->data)

1. ptr=ptr->rchild2. Else

1. ptr=ptr->lchild3. EndIf

3. EndWhile4. If(ptr=NULL)

1. Print(“Item notFound”)2. exit

5. Else1. Print(“Item Found”)

6. EndIf7. Return (ptr)

ptr

Page 7: 4.Shamod 4-4 BST Search -Deletion

Search 6

5

3 7

42 86

root

Steps:

1. ptr=root2. While(ptr!=NULL and ptr->data !=

item)1. If( item>ptr->data)

1. ptr=ptr->rchild2. Else

1. ptr=ptr->lchild3. EndIf

3. EndWhile4. If(ptr=NULL)

1. Print(“Item notFound”)2. exit

5. Else1. Print(“Item Found”)

6. EndIf7. Return (ptr)

ptr

Page 8: 4.Shamod 4-4 BST Search -Deletion

Search 6

5

3 7

42 86

root

Steps:

1. ptr=root2. While(ptr!=NULL and ptr->data !=

item)1. If( item>ptr->data)

1. ptr=ptr->rchild2. Else

1. ptr=ptr->lchild3. EndIf

3. EndWhile4. If(ptr=NULL)

1. Print(“Item notFound”)2. exit

5. Else1. Print(“Item Found”)

6. EndIf7. Return (ptr)

ptr

Page 9: 4.Shamod 4-4 BST Search -Deletion

Again Search 9

5

3 7

42 86

root

Steps:

1. ptr=root2. While(ptr!=NULL and ptr->data !=

item)1. If( item>ptr->data)

1. ptr=ptr->rchild2. Else

1. ptr=ptr->lchild3. EndIf

3. EndWhile4. If(ptr=NULL)

1. Print(“Item notFound”)2. exit

5. Else1. Print(“Item Found”)

6. EndIf7. Return (ptr)

ptr

Page 10: 4.Shamod 4-4 BST Search -Deletion

Search 9

5

3 7

42 86

root

Steps:

1. ptr=root2. While(ptr!=NULL and ptr->data !=

item)1. If( item>ptr->data)

1. ptr=ptr->rchild2. Else

1. ptr=ptr->lchild3. EndIf

3. EndWhile4. If(ptr=NULL)

1. Print(“Item notFound”)2. exit

5. Else1. Print(“Item Found”)

6. EndIf7. Return (ptr)

ptr

Page 11: 4.Shamod 4-4 BST Search -Deletion

Search 9

5

3 7

42 86

root

Steps:

1. ptr=root2. While(ptr!=NULL and ptr->data !=

item)1. If( item>ptr->data)

1. ptr=ptr->rchild2. Else

1. ptr=ptr->lchild3. EndIf

3. EndWhile4. If(ptr=NULL)

1. Print(“Item notFound”)2. exit

5. Else1. Print(“Item Found”)

6. EndIf7. Return (ptr)

ptr

Page 12: 4.Shamod 4-4 BST Search -Deletion

Search 9

5

3 7

42 86

root

Steps:

1. ptr=root2. While(ptr!=NULL and ptr->data !=

item)1. If( item>ptr->data)

1. ptr=ptr->rchild2. Else

1. ptr=ptr->lchild3. EndIf

3. EndWhile4. If(ptr=NULL)

1. Print(“Item notFound”)2. exit

5. Else1. Print(“Item Found”)

6. EndIf7. Return (ptr)

ptr=NULL

Page 13: 4.Shamod 4-4 BST Search -Deletion

Search 9Item not found

5

3 7

42 86

root

Steps:

1. ptr=root2. While(ptr!=NULL and ptr->data !=

item)1. If( item>ptr->data)

1. ptr=ptr->rchild2. Else

1. ptr=ptr->lchild3. EndIf

3. EndWhile4. If(ptr=NULL)

1. Print(“Item notFound”)2. exit

5. Else1. Print(“Item Found”)

6. EndIf7. Return (ptr)

ptr=NULL

Page 14: 4.Shamod 4-4 BST Search -Deletion

Algorithm BST_SEARCH(item)

Input: item is the data to be searched

Output: if found then pointer to the node containing data

Data Structure: Linked Structure

 

Steps:

1. ptr=root

2. While(ptr!=NULL and ptr->data != item)1. If( item>ptr->data)

1. ptr=ptr->rchild2. Else

1. ptr=ptr->lchild3. EndIf

3. EndWhile4. If(ptr=NULL)

1. Print(“Item notFound”)2. exit

5. Else1. Print(“Item Found”)

6. EndIf7. Return (ptr)

Page 15: 4.Shamod 4-4 BST Search -Deletion

Deleting a node from a binary search treeSuppose

T binary search treeITEM data to be deleted from T if it exists in the treeN node which contains the information ITEMPARENT(N) the parent node of NSUCC(N) the inorder successor of node N

Then the deletion of the node N depends on the number of children for node N. Hence, three cases may arise:

Case 1: N is a leaf nodeCase 2: N has exactly one childCase 3: N has two Childs.

 

Page 16: 4.Shamod 4-4 BST Search -Deletion

Deleting a node from a binary search treeSteps:1. Search for the particular node2. If item Not found

1. Print a message2. Exit

3. Else1. Case 1: N is a leaf node2. Case 2: N has exactly one child and it’s a left child3. Case 3: N has exactly one child and it’s a right child4. Case 4: N has two Childs->

4.   Stop

Page 17: 4.Shamod 4-4 BST Search -Deletion

IMPLEMENTATIONDeleting a node from a binary search treeDelete 6

Search : 1. Flag=02. ptr=root3. While(ptr!=NULL)

1. If(ptr->data=item)1. Flag=12. ExitWhile

2. Else1. Parent=ptr2. If(item>ptr->data)

1. ptr=ptr->rchild 3. Else ptr=ptr->lchild 4. EndIf

3. EndIf 4. EndWhile 5. If(flag=0)

1. Print(“ item NOT found in the tree”)2. Exit

6. EndIf

5

3 7

42 86

rootptr

Page 18: 4.Shamod 4-4 BST Search -Deletion

IMPLEMENTATIONDeleting a node from a binary search treeDelete 6

Search: 1. Flag=02. ptr=root3. While(ptr!=NULL)

1. If(ptr->data=item)1. Flag=12. ExitWhile

2. Else1. Parent=ptr2. If(item>ptr->data)

1. ptr=ptr->rchild 3. Else ptr=ptr->lchild 4. EndIf

3. EndIf 4. EndWhile 5. If(flag=0)

1. Print(“ item NOT found in the tree”)2. Exit

6. EndIf

5

3 7

42 86

rootptrparent

Page 19: 4.Shamod 4-4 BST Search -Deletion

IMPLEMENTATIONDeleting a node from a binary search treeDelete 6

Search : 1. Flag=02. ptr=root3. While(ptr!=NULL)

1. If(ptr->data=item)1. Flag=12. ExitWhile

2. Else1. Parent=ptr2. If(item>ptr->data)

1. ptr=ptr->rchild 3. Else ptr=ptr->lchild 4. EndIf

3. EndIf 4. EndWhile 5. If(flag=0)

1. Print(“ item NOT found in the tree”)2. Exit

6. EndIf

5

3 7

42 86

root

ptr

parent

Page 20: 4.Shamod 4-4 BST Search -Deletion

IMPLEMENTATIONDeleting a node from a binary search treeDelete 6

Search : 1. Flag=02. ptr=root3. While(ptr!=NULL)

1. If(ptr->data=item)1. Flag=12. ExitWhile

2. Else1. Parent=ptr2. If(item>ptr->data)

1. ptr=ptr->rchild 3. Else ptr=ptr->lchild 4. EndIf

3. EndIf 4. EndWhile 5. If(flag=0)

1. Print(“ item NOT found in the tree”)2. Exit

6. EndIf

5

3 7

42 86

root

ptrparent

Page 21: 4.Shamod 4-4 BST Search -Deletion

IMPLEMENTATIONDeleting a node from a binary search treeDelete 6

Search: 1. Flag=02. ptr=root3. While(ptr!=NULL)

1. If(ptr->data=item)1. Flag=12. ExitWhile

2. Else1. Parent=ptr2. If(item>ptr->data)

1. ptr=ptr->rchild 3. Else ptr=ptr->lchild 4. EndIf

3. EndIf 4. EndWhile 5. If(flag=0)

1. Print(“ item NOT found in the tree”)2. Exit

6. EndIf

5

3 7

42 86

root

ptr

parent

Page 22: 4.Shamod 4-4 BST Search -Deletion

IMPLEMENTATIONDeleting a node from a binary search treeDelete 6

Search : Flag=1 1. Flag=02. ptr=root3. While(ptr!=NULL)

1. If(ptr->data=item)1. Flag=12. ExitWhile

2. Else1. Parent=ptr2. If(item>ptr->data)

1. ptr=ptr->rchild 3. Else ptr=ptr->lchild 4. EndIf

3. EndIf 4. EndWhile 5. If(flag=0)

1. Print(“ item NOT found in the tree”)2. Exit

6. EndIf

5

3 7

42 86

root

ptr

parent

Page 23: 4.Shamod 4-4 BST Search -Deletion

Deleting a node from a binary search tree1. Case 1: N is a leaf node2. Case 2: N has exactly one child and it’s a left child3. Case 3: N has exactly one child and it’s a right child4. Case 4: N has two Childs->

5

3 7

42 86

root

1 9

Case 4

Case 3

Case 2

Case 1

Page 24: 4.Shamod 4-4 BST Search -Deletion

Deleting a node from a binary search tree1. Case 1: N is a leaf node

1. N is the root2. N is the left child of its parent3. N is the right child of its parent

2. Case 2: N has exactly one child and it’s a left child1. N is the root2. N is the left child of its parent3. N is the right child of its parent

3. Case 3: N has exactly one child and it’s a right child1. N is the root2. N is the left child of its parent3. N is the right child of its parent

4. Case 4: N has two Childs->

Page 25: 4.Shamod 4-4 BST Search -Deletion

Deleting a node from a binary search tree

Page 26: 4.Shamod 4-4 BST Search -Deletion

Case 1: N is a leaf nodeN is the root( Delete 5)

 

5

rootptr

Page 27: 4.Shamod 4-4 BST Search -Deletion

Case 1: N is a leaf nodeN is the root( Delete 5)  If(ptr->lchild=NULL && ptr-

>rchild=NULL)1. If(root=ptr)

1. FREESPACE(ptr)2. Root=NULL

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=NULL2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=NULL2. FREESPACE(ptr)

4. EndIf

Root = NULLptr

Page 28: 4.Shamod 4-4 BST Search -Deletion

Case 1: N is a leaf nodeN is the left child of its parent( Delete 6)

  N is deleted by simply setting the pointer of N in the parent node PARENT(N) to a NULL value

5

3 7

42 86

root

parent

ptr

Page 29: 4.Shamod 4-4 BST Search -Deletion

Case 1: N is a leaf nodeN is the left child of its parent( Delete 6)  N is deleted by simply setting the pointer of N in the parent

node PARENT(N) to a NULL value

5

3 7

42 8

root

NULL

parent

ptr

Page 30: 4.Shamod 4-4 BST Search -Deletion

Case 1: N is a leaf nodeN is the left child of its parent( Delete 6)  If(ptr->lchild=NULL && ptr-

>rchild=NULL)1. If(root=ptr)

1. FREESPACE(ptr)2. Root=NULL

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=NULL2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=NULL2. FREESPACE(ptr)

4. EndIf

5

3 7

42 8

root

NULL

parent

ptr

Page 31: 4.Shamod 4-4 BST Search -Deletion

Case 1: N is a leaf nodeN is the right child of its parent( Delete 8)

parent

5

3 7

42 86

root

ptr

Page 32: 4.Shamod 4-4 BST Search -Deletion

Case 1: N is a leaf nodeN is the right child of its parent( Delete 8)

If(ptr->lchild=NULL && ptr->rchild=NULL)

1. If(root=ptr)1. FREESPACE(ptr)2. Root=NULL

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=NULL2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=NULL2. FREESPACE(ptr)

4. EndIf

parent

5

3 7

42 6

root

ptrNULL

Page 33: 4.Shamod 4-4 BST Search -Deletion

Deleting a node from a binary search tree

Case 2: N has exactly one child and it’s a right child1. N is the root2. N is the left child of its parent3. N is the right child of its parent

Page 34: 4.Shamod 4-4 BST Search -Deletion

N has exactly one child and it’s a right childN is the root Delete 5

5

7

8

rootptr

Page 35: 4.Shamod 4-4 BST Search -Deletion

N has exactly one child and it’s a right childN is the root Delete 5

7

8

root

ptr=NULL

Page 36: 4.Shamod 4-4 BST Search -Deletion

N has exactly one child and it’s a right childN is the root Delete 5

7

8

root

ptr=NULL

ptr->lchild == NULL && ptr->rchild != NULL

1. If(root=ptr)1. Root=root->rchild 2. FREESPACE(ptr)

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=ptr->rchild 2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=ptr->rchild 2. FREESPACE(ptr)

4. EndIf

Page 37: 4.Shamod 4-4 BST Search -Deletion

N has exactly one child and it’s a right childN is the right child of its parent Delete 7

ptr->lchild == NULL && ptr->rchild != NULL

1. If(root=ptr)1. Root=root->rchild 2. FREESPACE(ptr)

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=ptr->rchild 2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=ptr->rchild 2. FREESPACE(ptr)

4. EndIf

5

3 7

42 8

root

ptr

parent

Page 38: 4.Shamod 4-4 BST Search -Deletion

N has exactly one child and it’s a right childN is the right child of its parent Delete 7

ptr->lchild == NULL && ptr->rchild != NULL

1. If(root=ptr)1. Root=root->rchild 2. FREESPACE(ptr)

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=ptr->rchild 2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=ptr->rchild 2. FREESPACE(ptr)

4. EndIf

5

3 7

42 8

root

ptr

parent

Page 39: 4.Shamod 4-4 BST Search -Deletion

N has exactly one child and it’s a right childN is the right child of its parent Delete 7

ptr->lchild == NULL && ptr->rchild != NULL

1. If(root=ptr)1. Root=root->rchild 2. FREESPACE(ptr)

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=ptr->rchild 2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=ptr->rchild 2. FREESPACE(ptr)

4. EndIf

5

3

42 8

root

ptr=NULL

parent

Page 40: 4.Shamod 4-4 BST Search -Deletion

N has exactly one child and it’s a right childN is the right child of its parent Delete 7

ptr->lchild == NULL && ptr->rchild != NULL

1. If(root=ptr)1. Root=root->rchild 2. FREESPACE(ptr)

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=ptr->rchild 2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=ptr->rchild 2. FREESPACE(ptr)

4. EndIf

5

3

42

8

rootparent

Page 41: 4.Shamod 4-4 BST Search -Deletion

N has exactly one child and it’s a right childN is the left child of its parent Delete 3

ptr->lchild == NULL && ptr->rchild != NULL

1. If(root=ptr)1. Root=root->rchild 2. FREESPACE(ptr)

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=ptr->rchild 2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=ptr->rchild 2. FREESPACE(ptr)

4. EndIf

5

3 7

4 8

root

ptr

parent

Page 42: 4.Shamod 4-4 BST Search -Deletion

N has exactly one child and it’s a right childN is the left child of its parent Delete 3

ptr->lchild == NULL && ptr->rchild != NULL

1. If(root=ptr)1. Root=root->rchild 2. FREESPACE(ptr)

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=ptr->rchild 2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=ptr->rchild 2. FREESPACE(ptr)

4. EndIf

5

3 7

4 8

root

ptr

parent

Page 43: 4.Shamod 4-4 BST Search -Deletion

N has exactly one child and it’s a right childN is the left child of its parent Delete 3

ptr->lchild == NULL && ptr->rchild != NULL

1. If(root=ptr)1. Root=root->rchild 2. FREESPACE(ptr)

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=ptr->rchild 2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=ptr->rchild 2. FREESPACE(ptr)

4. EndIf

5

7

4 8

root

ptr=NULL

parent

Page 44: 4.Shamod 4-4 BST Search -Deletion

N has exactly one child and it’s a right childN is the left child of its parent Delete 3

ptr->lchild == NULL && ptr->rchild != NULL

1. If(root=ptr)1. Root=root->rchild 2. FREESPACE(ptr)

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=ptr->rchild 2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=ptr->rchild 2. FREESPACE(ptr)

4. EndIf

5

74

8

rootparent

Page 45: 4.Shamod 4-4 BST Search -Deletion

Deleting a node from a binary search tree

Case 3: N has exactly one child and it’s a left child

1. N is the root2. N is the left child of its parent3. N is the right child of its parent

 

Page 46: 4.Shamod 4-4 BST Search -Deletion

N has exactly one child and it’s a left childN is the left child of its parent Delete 3

ptr->lchild != NULL && ptr->rchild == NULL

1. If(root=ptr)1. Root=root->lchild 2. FREESPACE(ptr)

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=ptr->lchild 2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=ptr->lchild 2. FREESPACE(ptr)

4. EndIf

5

3 7

2 6

root

ptr

parent

Page 47: 4.Shamod 4-4 BST Search -Deletion

N has exactly one child and it’s a left childN is the right child of its parent Delete 7

ptr->lchild != NULL && ptr->rchild == NULL

1. If(root=ptr)1. Root=root->lchild 2. FREESPACE(ptr)

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=ptr->lchild 2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=ptr->lchild 2. FREESPACE(ptr)

4. EndIf

5

3 7

42 6

root

ptr

parent

Page 48: 4.Shamod 4-4 BST Search -Deletion

Deleting a node from a binary search tree

Case 3: N has two Childs-> 

Page 49: 4.Shamod 4-4 BST Search -Deletion

Deleting a node from a binary search treeCase 3: N has two Childs->

Steps:

1. Find SUCCESSOR (N)2. Copy data of SUCCESSOR(N) to NODE(N)3. Delete SUCCESSOR(N)4. Reset the leftchild of the parent of SUCCESSOR(N) by the rightchild of

SUCCESSOR(N)

Page 50: 4.Shamod 4-4 BST Search -Deletion

SUCCESSOR(ptr)

50

30

70

20

40

60

80

18

25

55

65

27

57

35

45

75

85

22

ptr Successor (N)

Leftmost child of the right child of N

Page 51: 4.Shamod 4-4 BST Search -Deletion

SUCCESSOR(ptr)

1. Temp = ptr->rchild2. If (temp !=NULL)

1. While(temp->lchild !=NULL)

1. temp=temp->lchild2. Endwhile

3. Endif4. Return (temp)5. stop

50

30

70

20

40

60

80

18

25

55

65

27

57

35

45

75

85

22

ptr

temp

Page 52: 4.Shamod 4-4 BST Search -Deletion

SUCCESSOR(ptr)

1. Temp = ptr->rchild2. If (temp !=NULL)

1. While(temp->lchild !=NULL)

1. temp=temp->lchild2. Endwhile

3. Endif4. Return (temp)5. stop

50

30

70

20

40

60

80

18

25

55

65

27

57

35

45

75

85

22

ptr

temp

Page 53: 4.Shamod 4-4 BST Search -Deletion

SUCCESSOR(ptr)

1. Temp = ptr->rchild2. If (temp !=NULL)

1. While(temp->lchild !=NULL)

1. temp=temp->lchild2. Endwhile

3. Endif4. Return (temp)5. stop

50

30

70

20

40

60

80

18

25

55

65

27

57

35

45

75

85

22

ptr

temp

Page 54: 4.Shamod 4-4 BST Search -Deletion

SUCCESSOR(ptr)

1. Temp = ptr->rchild2. If (temp !=NULL)

1. While(temp->lchild !=NULL)

1. temp=temp->lchild2. Endwhile

3. Endif4. Return (temp)5. stop

50

30

70

20

40

60

80

18

25

55

65

27

57

35

45

75

85

22

ptr

temp

Page 55: 4.Shamod 4-4 BST Search -Deletion

SUCCESSOR(ptr)

1. Temp = ptr->rchild2. If (temp !=NULL)

1. While(temp->lchild !=NULL)

1. temp=temp->lchild2. Endwhile

3. Endif4. Return (temp)5. stop

50

30

70

20

40

60

80

18

25

55

65

27

57

35

45

75

85

22

ptr

temp

Page 56: 4.Shamod 4-4 BST Search -Deletion

SUCCESSOR(ptr)

1. Temp = ptr->rchild2. If (temp !=NULL)

1. While( temp->lchild !=NULL )

1. temp=temp->lchild2. Endwhile

3. Endif4. Return (temp)5. stop

50

30

70

20

40

60

80

18

25

55

65

27

57

35

45

75

85

22

ptr

temp

Page 57: 4.Shamod 4-4 BST Search -Deletion

DELETE 5N has two Childs->1. Find SUCCESSOR (N)2. Copy data of

SUCCESSOR(N) to NODE(N)

3. Reset the leftchild of the parent of SUCCESSOR(N) by the rightchild of SUCCESSOR(N)

4. Delete SUCCESSOR(N)

5

3 7

42 86

root

Page 58: 4.Shamod 4-4 BST Search -Deletion

DELETE 5N has two Childs->1. Find SUCCESSOR (N)

1. SUCCESSOR(5) = NODE(6)2. Copy data of SUCCESSOR(N) to

NODE(N)3. Delete SUCCESSOR(N)

5

3 7

42 86

root

Page 59: 4.Shamod 4-4 BST Search -Deletion

DELETE 5N has two Childs->1. Find SUCCESSOR (N)2. Copy data of SUCCESSOR(N) to

NODE(N)3. Delete SUCCESSOR(N)

6

3 7

42 86

root

Page 60: 4.Shamod 4-4 BST Search -Deletion

DELETE 5N has two Childs->1. Find SUCCESSOR (N)2. Copy data of SUCCESSOR(N) to

NODE(N)3. Delete SUCCESSOR(N)

3 cases No child Only right child No chance of a left child

6

3 7

42 86

root

Page 61: 4.Shamod 4-4 BST Search -Deletion

DELETE 5N has two Childs->1. Find SUCCESSOR (N)2. Copy data of SUCCESSOR(N) to

NODE(N)3. Delete SUCCESSOR(N)

3 cases No child Only right child No chance of a left child

6

3 7

42 8

root

Page 62: 4.Shamod 4-4 BST Search -Deletion

DELETE 5N has two Childs->1. Find SUCCESSOR (N)2. Copy data of SUCCESSOR(N) to

NODE(N)3. Delete SUCCESSOR(N)

3 cases No child Only right child No chance of a left child

6

3 7

42 8

root

Parent(successor)

NULL

Page 63: 4.Shamod 4-4 BST Search -Deletion

DELETE 5 – if the successor have a right child ??N has two Childs->1. Find SUCCESSOR (N)2. Copy data of SUCCESSOR(N) to

NODE(N)3. Delete SUCCESSOR(N)

3 cases No child Only right child No chance of a left child

6

3 7

42 8

root

Parent(successor)

6

x

Page 64: 4.Shamod 4-4 BST Search -Deletion

DELETE 5 – if the successor have a right child ??N has two Childs->1. Find SUCCESSOR (N)2. Copy data of SUCCESSOR(N) to

NODE(N)3. Delete SUCCESSOR(N)

3 cases No child Only right child No chance of a left child

6

3 7

42 8

root

Parent(successor)

6

x

Page 65: 4.Shamod 4-4 BST Search -Deletion

DELETE 5 – if the successor have a right child ??N has two Childs->1. Find SUCCESSOR (N)2. Copy data of SUCCESSOR(N) to

NODE(N)3. Delete SUCCESSOR(N)

3 cases No child Only right child No chance of a left child

6

3 7

42 8

root

Parent(successor)

x

Page 66: 4.Shamod 4-4 BST Search -Deletion

DELETE 5 – if the successor have a right child ??ptr->lchild != NULL && ptr->rchild !=

NULL1. Succ = SUCCESSOR (ptr)2. item = succ->data3. Delete (item)4. ptr->data=item

6

3 7

42 8

root

Parent(successor)

x

Page 67: 4.Shamod 4-4 BST Search -Deletion

Algorithm BST_DELETE(item)

Input: item is the data to be

removed

Output: if the node with data as item

exist it is deleted else a message->

Data Structure: Linked structure of binary

tree, root will point to the root node

Page 68: 4.Shamod 4-4 BST Search -Deletion

10. If(ptr->lchild=NULL AND ptr->rchild=NULL)

1. If(parent=NULL)1. FREESPACE(ptr)2. Root=NULL

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=NULL2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=NULL2. FREESPACE(ptr)

4. EndIf

11. ElseIf(ptr->lchild=NULL AND ptr->rchild!=NULL)1. If(parent=NULL)

1. Root=root->rchild 2. FREESPACE(ptr)

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=ptr-

>rchild 2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=ptr-

>rchild 2. FREESPACE(ptr)

4. EndIf

Page 69: 4.Shamod 4-4 BST Search -Deletion

12. ElseIf(ptr->lchild!=NULL AND ptr->rchild=NULL)

1. If(parent=NULL)1. Root=root->lchild 2. FREESPACE(ptr)

2. ElseIf(parent->lchild=ptr)1. Parent->lchild=ptr-

>lchild 2. FREESPACE(ptr)

3. ElseIf(parent->rchild=ptr)1. Parent->rchild=ptr-

>lchild 2. FREESPACE(ptr)

4. EndIf

13. Else if (ptr->lchild != NULL && ptr->rchild != NULL)

1. Succ = SUCCESSOR (ptr)

2. item = succ->data3. Delete (item)4. ptr->data=item

14. Endif 15. stop