1 Binary Search Trees III Delete Chapter 6
2 Objectives You will be able to write code to delete a node from a Binary Search Tree.
3 Issues in Deletion from a BST Deleting a node from a BST is more complex than adding a node. We have to ensure that the BST is still a binary tree after we delete a node. And that the BST constraints are still met. Everything to the left of any node is <=. Everything to the right of any node is >=.
4 Deletion from a BST Three possible cases for deleting a node, x, from a BST: 1. The node, x, is a leaf. 2. The node, x, has one child. 3. The node, x, has two children.
5 Deleting a Leaf Case 1. The node, x, is a leaf.
6 Deleting a Node with One Child Case 2. The node, x, has one child.
7 Deleting a Node with Two Children Case 3: The node, x, has two children. Deletion by copying. (Hibbard & Knuth) Replace contents of x with the contents of its inorder successor K Note that inorder successor will have no left child. Why?
8 Delete the former inorder successor node as described for cases 1 and 2 Deleting a Node with Two Children K The node that was the inorder successor will either be a leaf or have one child (a right child.) In this example it has a right child.
9 Implementing Deletion Download most recent version of BST _03_21_Delete_Node/ _03_21_Delete_Node/ BST_Vertical_Display Expand. Build and run. Rename folder BST_with_Delete
10 Downloaded Program Running
11 Add Delete Node Method In public section of BST class definition: void delete_node(const T&); In protected section of BST class definition: void delete_leaf(BSTNode *p, BSTNode *prev); void delete_node_with_one_child(BSTNode *p, BSTNode *prev) ; void delete_node_with_two_children(BSTNode *p) ;
12 Implementation Copy: _03_21_Delete_Node/ File BST_Delete_Node.cpp.txt _03_21_Delete_Node/
13 Below Class Defintion template void BST ::delete_node(const T& el) { BSTNode *p = root; BSTNode *prev = 0; // Find node to be deleted. while (p != 0 && !(p->key == el)) { prev = p; if (p->key < el) { p = p->right; } else { p = p->left; }
14 Delete Node Method (continued) if (p == 0) { // Declare success! cout << el << " is not in the tree\n"; return; } // p points to the node to be deleted. if ((p->left != 0) && (p->right != 0)) { // Delete node with two children abort(); // Not yet implemented } if ((p->left != 0) || (p->right != 0)) { // Delete node with one child. abort(); // Not yet implemented }
15 Delete Node Method (continued) // Delete leaf; delete_leaf(p, prev); }
16 delete_leaf() template void BST ::delete_leaf(BSTNode *p, BSTNode *prev) { cout key << endl; if (prev->left == p) { prev->left = 0; } else { prev->right = 0; } delete(p); }
17 In main.cpp Delete traversals. Add at end: cout << endl << "Deleting 20\n"; my_BST.delete_node(20); cout << endl << "Updated tree:\n"; my_BST.display_v(cout); cin.get(); return 0; }
18 Deleting Leaf Node
19 Delete Nonexistent Node Add at end of main: cout << "\n\nDeleting nonexistent node, 15\n"; my_BST.delete_node(15); my_BST.display_v(cout); cout << endl << endl;
20 Deleting Nonexistent Node
21 Delete Node with One Child In function delete_node: if ((p->left != 0) || (p->right != 0)) { // Delete node with one child. delete_node_with_one_child(p, prev); return; } Implementation: _03_21_Delete_Node/ _03_21_Delete_Node/ File delete_node_with_one_child.cpp.txt
22 Delete Node with One Child template void BST ::delete_node_with_one_child(BSTNode *p, BSTNode *prev) { cout key << endl; BSTNode * child; if (p->left != 0) { child = p->left; } else { child = p->right; } if (prev->right == p) { prev->right = child; } else { prev->left = child; } delete(p); }
23 main.cpp cout << "\nDeleting node with one child, 31\n"; my_BST.delete_node(31); cout << "\nUpdated tree:\n"; my_BST.display_v(cout); cout << endl << endl; cin.get(); return 0; }
24 Deleting Node with One Child
25 Delete Node with Two Children In Function delete_node(): if ((p->left != 0) && (p->right != 0)) { // Delete node with two children delete_node_with_two_children(p); return; } Copy implementation from: _03_21_Delete_Node/ File delete_node_with_two_children.cpp.txt
26 Delete Node with Two Children template void BST ::delete_node_with_two_children(BSTNode *p) { cout key << endl; // Find inorder successor to node to be deleted. BSTNode * suc = p->right; BSTNode * prev = p; while (suc->left != 0) { prev = suc; suc = suc->left; }
27 Delete Node with Two Children // Copy data from inorder successor as data for the // node to be deleted. p->key = suc->key; // Delete the node that was the inorder successor. if (suc->right != 0) { // Delete node with one child. delete_node_with_one_child(suc, prev); } else { delete_leaf(suc, prev); }
28 In main.cpp cout << "\nDeleting node with two children, 13\n"; my_BST.delete_node(13); cout << "\nUpdated tree:\n"; my_BST.display_v(cout); cout << endl << endl; cin.get(); return 0; }
29 Deleting Node with Two Children