1 / 10

Deletion From a Binary Search Tree

Learn how to delete a node from a binary search tree using the rules outlined in the textbook "Fundamentals of Data Structures in C". Includes C program codes.

Download Presentation

Deletion From a Binary Search Tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 “ .

  2. 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

  3. 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 the smallest node of left subtree 5 15 2 7 12 20 9 17

  4. 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 the smallest node of left subtree 15 7 12 20 2 9 17

  5. 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 the smallest node of left subtree 5 15 (c) Delete a node which only has left child 2 7 12 20 9 17

  6. 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 the smallest node of left subtree 5 15 (c) Delete a node which only has left child 2 7 12 20 (d) Delete a node which has two children 9 13

  7. Deletion From a Binary Search Tree • How to delete a node from binary search tree? • C Program codes: An pointer which can revise the link from parent node void deleteNode(TreeNodePtr * treePtr, int value){ TreeNodePtr current = * treePtr, parentPtr, tmpPtr; if(!current){ fprintf(stderr, "The tree is empty or has not this node.\n"); return; } if(current->data == value){ /*it's a leaf node*/ if(!current->rightPtr && !current->leftPtr){ * treePtr = NULL; free(current); } The node you want to delete Get the information of current node Return a error messenge for empty tree or the tree without this node If find the node, following the four cases Case (a), let the parent node link to NULL, and free(delete) the node

  8. Deletion From a Binary Search Tree • How to delete a node from binary search tree? • C Program codes: Case(c), let the parent node link to the 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); } /*the node has both children*/ else{ tmpPtr = current->rightPtr; /*the rightPtr without left child*/ if(!tmpPtr->leftPtr) tmpPtr->leftPtr = current->leftPtr; Case (b) and (d), find the smallestnode of the right subtree Get the right children to decide (b) or (d) Case(b), let the left child node link to the right child, and let the parent node link to the left child.

  9. 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){ /*record the parent node of tmpPtr*/ parentPtr = tmpPtr; tmpPtr = tmpPtr->leftPtr; } parentPtr->leftPtr = tmpPtr->rightPtr; tmpPtr->leftPtr = current->leftPtr; tmpPtr->rightPtr = current->rightPtr; } Find the smallest node, and record its parent node Let the parent node of smallest node link to the right child of smallest node Let right child of the smallest node link to the left child of current node, so as the right one

  10. 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 The smallest node, and free the current node * treePtr = tmpPtr; free(current); } }else if(value > current->data){/*search the node*/ deleteNode(&(current->rightPtr), value); }else if(value < current->data){ deleteNode(&(current->leftPtr), value); } } If the node you want to delete is big than current node, find the right child If the node is small than current node, find the left child

More Related