Deletion From A Binary Search Tree

You might also like

Download as pps, pdf, or txt
Download as pps, pdf, or txt
You are on page 1of 10

Deletion From a Binary Search Tree

Instructor : Prof. Jyh-Shing Roger Jang


Designer : Shao-Huan Wang
The ideas are reference to the textbook “Fundamentals of Data Structures in C “ .
Deletion From a Binary Search Tree
 How to delete a node from binary search
tree? Rule: (i)Find the largest node of left subtree
(ii)Find the smallest node of right subtree
10
Case: (a) Delete a node which has no children

5 15

2 7 12 20

9 17
Deletion From a Binary Search Tree
 How to delete a node from binary search
tree? Rule: (i)Find the largest node of left subtree
(ii)Find the smallest node of right subtree
10
Case: (a) Delete a node which has no children
(b) Delete a node whose left child is
5 15 the smallest node of left subtree

2 7 12 20

9 17
Deletion From a Binary Search Tree
 How to delete a node from binary search
tree? Rule: (i)Find the largest node of left subtree
(ii)Find the smallest node of right subtree
10
Case: (a) Delete a node which has no children
(b) Delete a node whose left child is
15 the smallest node of left subtree

7 12 20

2 9 17
Deletion From a Binary Search Tree
 How to delete a node from binary search
tree? Rule: (i)Find the largest node of left subtree
(ii)Find the smallest node of right subtree
10
Case: (a) Delete a node which has no children
(b) Delete a node whose left child is
5 15 the smallest node of left subtree
(c) Delete a node which only has left child
2 7 12 20

9 17
Deletion From a Binary Search Tree
 How to delete a node from binary search
tree? Rule: (i)Find the largest node of left subtree
(ii)Find the smallest node of right subtree
10
Case: (a) Delete a node which has no children
(b) Delete a node whose left child is
5 15 the smallest node of left subtree
(c) Delete a node which only has left child
2 7 12 20 (d) Delete a node which has two children

9 13
Deletion From a Binary Search Tree
 How to delete a node from binary search
tree? An pointer which can revise
 C Program codes: the link from parent node
void deleteNode(TreeNodePtr * treePtr, int value){ The node you want to delete
TreeNodePtr current = * treePtr, parentPtr, tmpPtr;
if(!current){ Get the information of current node
fprintf(stderr, "The tree is empty or has not this
node.\n"); Return a error messenge for empty tree
return; or the tree without this node
}
If find the node, following the four cases
if(current->data == value){
/*it's a leaf node*/
if(!current->rightPtr && !current->leftPtr){
* treePtr = NULL; Case (a), let the parent node link to NULL,
free(current); and free(delete) the node
}
Deletion From a Binary Search Tree
 How to delete a node from binary search
tree? Case(c), let the parent node link to the
 C Program codes: left child, and free the current node
/*the right child is NULL, and left child isn't*/
else if(!current->rightPtr){
* treePtr = current->leftPtr;
free(current);
} Case (b) and (d), find the smallest node
/*the node has both children*/ of the right subtree
else{
tmpPtr = current->rightPtr; Get the right children to decide (b) or (d)
/*the rightPtr without left child*/
Case(b), let the left child node link to the
if(!tmpPtr->leftPtr)
right child, and let the parent node
tmpPtr->leftPtr = current->leftPtr;
link to the left child.
Deletion From a Binary Search Tree
 How to delete a node from binary search
tree?
 C Program codes: Case(d), find the smallest node of right subtree
/*the rightPtr have left child*/
else{
/*find the smallest node in right subtree*/
while(tmpPtr->leftPtr){ Find the smallest node, and record
/*record the parent node of tmpPtr*/ its parent node
parentPtr = tmpPtr;
tmpPtr = tmpPtr->leftPtr;
Let the parent node of smallest node
}
link to the right child of smallest node
parentPtr->leftPtr = tmpPtr->rightPtr;
tmpPtr->leftPtr = current->leftPtr; Let right child of the smallest node
tmpPtr->rightPtr = current->rightPtr; link to the left child of current node,
} so as the right one
Deletion From a Binary Search Tree
 How to delete a node from binary search
tree?
 C Program codes: Let the parent node of current node link to
* treePtr = tmpPtr; The smallest node, and free the current node
free(current); If the node you want to delete is big than
} current node, find the right child
}else if(value > current->data){/*search the node*/
deleteNode(&(current->rightPtr), value);
}else if(value < current->data){
deleteNode(&(current->leftPtr), value);
If the node is small than current node,
}
find the left child
}

You might also like