Академический Документы
Профессиональный Документы
Культура Документы
BST deletion
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)
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
3
Case
2
4
Case
1
Case
8 3
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.
ptr
root
5
ptr
Root = NULL
root
5
parent
3
7
ptr
root
5
parent
3
NULL
ptr
root
5
parent
3
NULL
ptr
root
5
parent
3
7
ptr
root
5
parent
3
ptr
NULL
ptr
root
5
Delete 5
ptr=NULL
root
7
ptr=NULL
root
7
parent
root
5
ptr
3
parent
root
5
ptr
3
parent
root
5
parent
root
5
parent
root
5
ptr
3
parent
root
5
ptr
3
parent
root
5
ptr
=NULL
parent
root
5
N is the root
N is the left child of its parent
N is the right child of its parent
parent
root
5
ptr
3
parent
root
5
ptr
3
Delete 7
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
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
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
root
6
Parent(success
or)
7
3 cases
No child
root
6
Parent(success
or)
7
3 cases
No child
root
6
Parent(success
or)
7
3 cases
No child
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.
>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.
ptr->rchild != NULL)
1. Succ = SUCCESSOR (ptr)
2. item = succ->data
3. Delete (item)
4. ptr->data=item
14. Endif
15. stop