Sei sulla pagina 1di 69

BST search

BST deletion

Searching in a binary Tree


Searching 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 item
1.
2.
3.
4.

start from the root node,


if item < root node data
1.
proceed to its left child->
If item > root node data
1.
proceed to its right child->
Continue step 2 and 3 till the item is found or we reach to a
dead end (leaf node)

Search 6

root
5

Steps:
1. ptr=root
2. While(ptr!=NULL and ptr->data !=

item)

Search 6

3.

ptr
root

4.

5.

6.

If( item>ptr->data)
1.
ptr=ptr->rchild
2.
Else
1.
ptr=ptr->lchild
3.
EndIf
EndWhile
If(ptr=NULL)
1.
Print(Item notFound)
2.
exit
Else
1.
Print(Item Found)
EndIf
Return (ptr)
1.

7.

Steps:
1. ptr=root
2. While(ptr!=NULL and ptr->data !=

item)

Search 6

3.
4.

root
5

5.

ptr
3

6.

If( item>ptr->data)
1.
ptr=ptr->rchild
2.
Else
1.
ptr=ptr->lchild
3.
EndIf
EndWhile
If(ptr=NULL)
1.
Print(Item notFound)
2.
Exit
Else
1.
Print(Item Found)
EndIf
Return (ptr)
1.

7.

Steps:
1. ptr=root
2. While(ptr!=NULL and ptr->data !=

item)

Search 6

3.
4.

root
5

5.

6.

7.

ptr
2

If( item>ptr->data)
1.
ptr=ptr->rchild
2.
Else
1.
ptr=ptr->lchild
3.
EndIf
EndWhile
If(ptr=NULL)
1.
Print(Item notFound)
2.
exit
Else
1.
Print(Item Found)
EndIf
Return (ptr)
1.

Steps:
1. ptr=root
2. While(ptr!=NULL and ptr->data !=

item)

Search 6

3.
4.

root
5

5.

6.

7.

ptr
2

If( item>ptr->data)
1.
ptr=ptr->rchild
2.
Else
1.
ptr=ptr->lchild
3.
EndIf
EndWhile
If(ptr=NULL)
1.
Print(Item notFound)
2.
exit
Else
1.
Print(Item Found)
EndIf
Return (ptr)
1.

Steps:
1. ptr=root
2. While(ptr!=NULL and ptr->data !=

item)

Search 6

3.
4.

root
5

5.

6.

7.

ptr
2

If( item>ptr->data)
1.
ptr=ptr->rchild
2.
Else
1.
ptr=ptr->lchild
3.
EndIf
EndWhile
If(ptr=NULL)
1.
Print(Item notFound)
2.
exit
Else
1.
Print(Item Found)
EndIf
Return (ptr)
1.

Steps:
1. ptr=root
2. While(ptr!=NULL and ptr->data !=

item)

Again Search 9

3.

ptr
root

4.

5.

6.

7.

If( item>ptr->data)
1.
ptr=ptr->rchild
2.
Else
1.
ptr=ptr->lchild
3.
EndIf
EndWhile
If(ptr=NULL)
1.
Print(Item notFound)
2.
exit
Else
1.
Print(Item Found)
EndIf
Return (ptr)
1.

Steps:
1. ptr=root
2. While(ptr!=NULL and ptr->data !=

item)

Search 9

3.
4.

root
5

5.

ptr
3

6.

If( item>ptr->data)
1.
ptr=ptr->rchild
2.
Else
1.
ptr=ptr->lchild
3.
EndIf
EndWhile
If(ptr=NULL)
1.
Print(Item notFound)
2.
exit
Else
1.
Print(Item Found)
EndIf
Return (ptr)
1.

7.

Steps:
1. ptr=root
2. While(ptr!=NULL and ptr->data !=

item)

Search 9

3.
4.

root
5

5.

6.

7.

ptr
2

If( item>ptr->data)
1.
ptr=ptr->rchild
2.
Else
1.
ptr=ptr->lchild
3.
EndIf
EndWhile
If(ptr=NULL)
1.
Print(Item notFound)
2.
exit
Else
1.
Print(Item Found)
EndIf
Return (ptr)
1.

Steps:
1. ptr=root
2. While(ptr!=NULL and ptr->data !=

item)

Search 9

3.
4.

root
5

5.

6.

If( item>ptr->data)
1.
ptr=ptr->rchild
2.
Else
1.
ptr=ptr->lchild
3.
EndIf
EndWhile
If(ptr=NULL)
1.
Print(Item notFound)
2.
exit
Else
1.
Print(Item Found)
EndIf
Return (ptr)
1.

7.

8
ptr=NULL

Steps:
1. ptr=root
2. While(ptr!=NULL and ptr->data !=

item)

Search 9
Item not found
3.
4.

root
5

5.

6.

If( item>ptr->data)
1.
ptr=ptr->rchild
2.
Else
1.
ptr=ptr->lchild
3.
EndIf
EndWhile
If(ptr=NULL)
1.
Print(Item notFound)
2.
exit
Else
1.
Print(Item Found)
EndIf
Return (ptr)
1.

7.

8
ptr=NULL

Steps:
1. ptr=root
2. While(ptr!=NULL and ptr-

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

>data != item)
1.
If( item>ptr->data)
1.
ptr=ptr->rchild
2.
Else
1.
ptr=ptr->lchild
3.
EndIf
3. EndWhile
4. If(ptr=NULL)
1.
Print(Item notFound)
2.
exit
5. Else
1.
Print(Item Found)
6. EndIf
7. Return (ptr)

Deleting a node from a binary


search tree
Suppose
T
binary search tree
ITEM
data to be deleted from T if it exists in the tree
N
node which contains the information ITEM
PARENT(N) the parent node of N
SUCC(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 node
Case 2: N has exactly one child
Case 3: N has two Childs.

Deleting a node from a binary


search tree
Steps:
1. Search for the particular node
2. If item Not found
1.
Print a message
2.
Exit
3. Else
1.
Case 1: N is a leaf node
2.
Case 2: N has exactly one child and its a left child
3.
Case 3: N has exactly one child and its a right child
4.
Case 4: N has two Childs->
4. Stop

IMPLEMENTATION
Deleting a node from a binary search
tree
1. Flag=0
Search6:
Delete

2.
3.

ptr
root
5

7
4.

5.

6.

ptr=root
While(ptr!=NULL)
1. If(ptr->data=item)
1.
Flag=1
2.
ExitWhile
2. Else
1.
Parent=ptr
2.
If(item>ptr->data)
1.
ptr=ptr->rchild
3.
Else ptr=ptr->lchild
4.
EndIf
3. EndIf
EndWhile
If(flag=0)
1. Print( item NOT found in the tree)
2. Exit
EndIf

IMPLEMENTATION
Deleting a node from a binary search
tree
1. Flag=0
Search:
Delete
6

2.
3.

parent
ptr
root
5

7
4.

5.

6.

ptr=root
While(ptr!=NULL)
1. If(ptr->data=item)
1.
Flag=1
2.
ExitWhile
2. Else
1.
Parent=ptr
2.
If(item>ptr->data)
1.
ptr=ptr->rchild
3.
Else ptr=ptr->lchild
4.
EndIf
3. EndIf
EndWhile
If(flag=0)
1. Print( item NOT found in the tree)
2. Exit
EndIf

IMPLEMENTATION
Deleting a node from a binary search
tree
1. Flag=0
Search6:
Delete

2.
3.

parent
root
5
ptr
3

7
4.

5.

6.

ptr=root
While(ptr!=NULL)
1. If(ptr->data=item)
1.
Flag=1
2.
ExitWhile
2. Else
1.
Parent=ptr
2.
If(item>ptr->data)
1.
ptr=ptr->rchild
3.
Else ptr=ptr->lchild
4.
EndIf
3. EndIf
EndWhile
If(flag=0)
1. Print( item NOT found in the tree)
2. Exit
EndIf

IMPLEMENTATION
Deleting a node from a binary search
tree
1. Flag=0
Search6:
Delete

2.
3.

root
5

parent
ptr

7
4.

5.

6.

ptr=root
While(ptr!=NULL)
1. If(ptr->data=item)
1.
Flag=1
2.
ExitWhile
2. Else
1.
Parent=ptr
2.
If(item>ptr->data)
1.
ptr=ptr->rchild
3.
Else ptr=ptr->lchild
4.
EndIf
3. EndIf
EndWhile
If(flag=0)
1. Print( item NOT found in the tree)
2. Exit
EndIf

IMPLEMENTATION
Deleting a node from a binary search
tree
1. Flag=0
Search:
Delete
6

2.
3.

root
5

parent

7
ptr

4.

5.

6.

ptr=root
While(ptr!=NULL)
1. If(ptr->data=item)
1.
Flag=1
2.
ExitWhile
2. Else
1.
Parent=ptr
2.
If(item>ptr->data)
1.
ptr=ptr->rchild
3.
Else ptr=ptr->lchild
4.
EndIf
3. EndIf
EndWhile
If(flag=0)
1. Print( item NOT found in the tree)
2. Exit
EndIf

IMPLEMENTATION
Deleting a node from a binary search
tree
1. Flag=0
Search6: Flag=1
Delete

2.
3.

root
5

parent

7
ptr

4.

5.

6.

ptr=root
While(ptr!=NULL)
1. If(ptr->data=item)
1.
Flag=1
2.
ExitWhile
2. Else
1.
Parent=ptr
2.
If(item>ptr->data)
1.
ptr=ptr->rchild
3.
Else ptr=ptr->lchild
4.
EndIf
3. EndIf
EndWhile
If(flag=0)
1. Print( item NOT found in the tree)
2. Exit
EndIf

Deleting a node from a binary


search
tree
Case
1: N is a leaf node
1.
2.
3.
4.

Case 2: N has exactly one child and its a left child


Case 3: N has exactly one child and its a right child
Case 4: N has two Childs->
root
5
Case
74

3
Case
2

4
Case
1

Case
8 3

Deleting a node from a binary


search
tree
Case
1: N is a leaf node
1.

N is the root
2.
N is the left child of its parent
3.
N is the right child of its parent
Case 2: N has exactly one child and its a left child
1.
N is the root
2.
N is the left child of its parent
3.
N is the right child of its parent
Case 3: N has exactly one child and its a right child
1.
N is the root
2.
N is the left child of its parent
3.
N is the right child of its parent
1.

2.

3.

4.

Case 4: N has two Childs->

Deleting a node from a binary


search tree

Case 1: N is a leaf node


N is the root
( Delete 5)

ptr
root
5

Case 1: N is a leaf node


N is the root
( Delete 5)

ptr
Root = NULL

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=NULL
2.
FREESPACE(ptr)
3.
ElseIf(parent->rchild=ptr)
1.
Parent->rchild=NULL
2.
FREESPACE(ptr)
4.
EndIf

Case 1: N is a leaf node


N 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

root
5
parent
3

7
ptr

Case 1: N is a leaf node


N 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

root
5
parent
3

NULL
ptr

Case 1: N is a leaf node


N is the left child of its parent
( Delete 6)

root
5
parent
3

NULL
ptr

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=NULL
2.
FREESPACE(ptr)
3.
ElseIf(parent->rchild=ptr)
1.
Parent->rchild=NULL
2.
FREESPACE(ptr)
4.
EndIf

Case 1: N is a leaf node


N is the right child of its parent
( Delete 8)

root
5
parent
3

7
ptr

Case 1: N is a leaf node


N is the right child of its parent
( Delete 8)

root
5
parent
3

ptr
NULL

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=NULL
2.
FREESPACE(ptr)
3.
ElseIf(parent->rchild=ptr)
1.
Parent->rchild=NULL
2.
FREESPACE(ptr)
4.
EndIf

Deleting a node from a binary


search tree
Case 2: N has exactly one child and its a right child
1.
N is the root
2.
N is the left child of its parent
3.
N is the right child of its parent

N has exactly one child and its a right child


N is the root

ptr
root
5

Delete 5

N has exactly one child and its a right child


N is the root
Delete 5

ptr=NULL

root
7

N has exactly one child and its a right child


N is the root
Delete 5

ptr=NULL

root
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

N has exactly one child and its a right child


N 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

parent
root
5
ptr
3

N has exactly one child and its a right child


N 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

parent
root
5
ptr
3

N has exactly one child and its a right child


N is the right child of its parent
Delete 7

parent
root
5

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)
ptr=NULL 3. ElseIf(parent->rchild=ptr)
1.
Parent->rchild=ptr->rchild
2.
FREESPACE(ptr)
4. EndIf

N has exactly one child and its a right child


N is the right child of its parent
Delete 7

parent
root
5

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

N has exactly one child and its a right child


N 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

parent
root
5
ptr
3

N has exactly one child and its a right child


N 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

parent
root
5
ptr
3

N has exactly one child and its a right child


N 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

parent
root
5
ptr
=NULL

N has exactly one child and its a right child


N 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

parent
root
5

Deleting a node from a binary


search tree

Case 3: N has exactly one child and its a left child


1.
2.
3.

N is the root
N is the left child of its parent
N is the right child of its parent

N has exactly one child and its a left child


N is the left child of its parent
Delete 3

parent
root
5
ptr
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

N has exactly one child and its a left child


N is the right child of its parent

parent
root
5
ptr
3

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

Deleting a node from a binary


search tree

Case 3: N has two Childs->

Deleting a node from a binary


search tree
Case 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)

SUCCESSOR(ptr)
pt
r5

Successor (N)
Leftmost child of the right child
of N

3
0

7
0

2
0

1
8

4
0

2
5

2
2

3
5

2
7

6
0

4
5

8
0

5
5

6
5

5
7

7
5

8
5

SUCCESSOR(ptr)
pt
r5

1.
2.

temp
3
0

7
0

3.
2
0

1
8

4
0

2
5

2
2

3
5

2
7

6
0

4
5

4.

8
0

5
5

6
5

5
7

7
5

5.

8
5

Temp = ptr->rchild
If (temp !=NULL)
1.
While(temp->lchild !=NULL)
1.
temp=temp->lchild
2.
Endwhile
Endif
Return (temp)
stop

SUCCESSOR(ptr)
pt
r5

1.
2.

temp
3
0

7
0

3.
2
0

1
8

4
0

2
5

2
2

3
5

2
7

6
0

4
5

4.

8
0

5
5

6
5

5
7

7
5

5.

8
5

Temp = ptr->rchild
If (temp !=NULL)
1.
While(temp->lchild !=NULL)
1.
temp=temp->lchild
2.
Endwhile
Endif
Return (temp)
stop

SUCCESSOR(ptr)
pt
r5

1.
2.

temp
3
0

7
0

3.
2
0

1
8

4
0

2
5

2
2

3
5

2
7

6
0

4
5

4.

8
0

5
5

6
5

5
7

7
5

5.

8
5

Temp = ptr->rchild
If (temp !=NULL)
1.
While(temp->lchild !=NULL)
1.
temp=temp->lchild
2.
Endwhile
Endif
Return (temp)
stop

SUCCESSOR(ptr)
pt
r5

1.
2.

3
0

7
0

3.

temp
2
0

1
8

4
0

2
5

2
2

3
5

2
7

6
0

4
5

4.

8
0

5
5

6
5

5
7

7
5

5.

8
5

Temp = ptr->rchild
If (temp !=NULL)
1.
While(temp->lchild !=NULL)
1.
temp=temp->lchild
2.
Endwhile
Endif
Return (temp)
stop

SUCCESSOR(ptr)
pt
r5

1.
2.

3
0

7
0

3.
2
0

4
0

6
0

4.

8
0

5.

temp
1
8

2
5

2
2

3
5

2
7

4
5

5
5

6
5

5
7

7
5

8
5

Temp = ptr->rchild
If (temp !=NULL)
1.
While(temp->lchild !=NULL)
1.
temp=temp->lchild
2.
Endwhile
Endif
Return (temp)
stop

SUCCESSOR(ptr)
pt
r5

1.
2.

3
0

7
0

2
0

4
0

6
0

3.

8
0

4.
5.

temp
1
8

2
5

2
2

3
5

2
7

4
5

5
5

6
5

5
7

7
5

8
5

Temp = ptr->rchild
If (temp !=NULL)
1.
While( temp->lchild !
=NULL )
1.
temp=temp->lchild
2.
Endwhile
Endif
Return (temp)
stop

DELETE 5
N 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)

root
5

DELETE 5
N 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)

root
5

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

root
6

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

root

3 cases

No child

Only right child

No chance of a left child

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

root

3 cases

No child

Only right child

No chance of a left child

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

root

6
Parent(success
or)
7

NULL

3 cases

No child

Only right child

No chance of a left child

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)

root

6
Parent(success
or)
7

3 cases

No child

Only right child

No chance of a left child

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)

root

6
Parent(success
or)
7

3 cases

No child

Only right child

No chance of a left child

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)

root

6
Parent(success
or)
7

3 cases

No child

Only right child

No chance of a left child

DELETE 5 if the successor have a


right child ??
ptr->lchild != NULL && ptr->rchild !=
NULL
1. Succ = SUCCESSOR (ptr)
2. item = succ->data
3. Delete (item)
4. ptr->data=item

root
6

Parent(success
or)
7

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

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=NULL
2.
FREESPACE(ptr)
3.
ElseIf(parent->rchild=ptr)
1.
Parent->rchild=NULL
2.
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

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->data
3. Delete (item)
4. ptr->data=item
14. Endif
15. stop

Potrebbero piacerti anche