1 / 8

Binary Search Trees Deletion

Binary Search Trees Deletion. COP 3502. Binary Search Tree - Deletion. Let’s consider several cases for Deletion: Deleting a Leaf node Deleting a node with 1 child Deleting a node with 2 children For all cases we have to identify the parent. 6. 5. 10. 13. 9. 3. 8. 12. 1. 4. 7.

golda
Télécharger la présentation

Binary Search Trees Deletion

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. Binary Search Trees Deletion COP 3502

  2. Binary Search Tree - Deletion • Let’s consider several cases for Deletion: • Deleting a Leaf node • Deleting a node with 1 child • Deleting a node with 2 children • For all cases we have to identify the parent. 6 5 10 13 9 3 8 12 1 4 7 9 11 13

  3. Binary Search Tree - Deletion parent 6 • Deleting a leaf node • Let’s say we want to delete 3 • Find the parent of 3 • which is 5 • Then what? • free(parent->left); • parent->left = NULL; • or if it’s a right leaf node: • free(parent->right); • parent->right= NULL; 5 9 3

  4. Binary Search Tree - Deletion • Deleting node with 1 child • Let’s say we want to delete 5 • We need to find the parent • AND the sole child • Then connect them • temp = parent->left; • child = temp->left; • parent->left = child; • free(temp); parent 6 temp 5 9 child 3 7 12 1 4

  5. Binary Search Tree - Deletion 6 • Deleting a node with 2 children: • In order to maintain the BST property , we can replace the deleted node with either: • Max in the left subtree • OR Min in the right subtree 5 10 9 3 8 12 1 4 7 9 11 13

  6. Binary Search Tree Deletion • In aiding in Deletion, we will want several auxiliary functions: • node* parent(node* root) • Finds the parent of a given node in a binary tree. • node* minVal(node* root), node* maxVal(node* root) • Finds the minimum (or maximum) value in a given binary tree. • intisLeaf(node* root) • Determines if a node is a leaf node or not. • inthasOnlyLeftChild(node* root), inthasOnlyRightChild(node* root) • Determines if a node ONLY has a left (or right) child or not. • node* findNode(node* root) • Returns a pointer to a node in a given tree that stores a particular value.

  7. node* delete(node* root, int value) { node *delnode, *parent;   // Get a pointer to the node to delete. delnode = findNode(root, value);   // Get the parent of this node. parent = parent(root, delnode); // Case 1: the node to delete is a leaf node. if (isLeaf(delnode)) {...} // Case 2: node to be deleted only has a left child. if (hasOnlyLeftChild(delnode)) {...} // Case 3: node to be deleted only has a right child. if (hasOnlyRightChild(delnode)) {...} // Case 4: the node has 2 children else {...} }

  8. Binary Search Tree Deletion • Fill in the code together in class for the Delete function.

More Related