Binary Search Trees III Delete
-
Upload
timothy-barber -
Category
Documents
-
view
32 -
download
1
description
Transcript of Binary Search Trees III Delete
![Page 1: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/1.jpg)
1
Binary Search Trees IIIDelete
Chapter 6
![Page 2: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/2.jpg)
2
Objectives
You will be able to write code to delete a node from a Binary Search Tree.
![Page 3: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/3.jpg)
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 >=.
![Page 4: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/4.jpg)
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.
![Page 5: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/5.jpg)
5
Deleting a Leaf
Case 1. The node, x, is a leaf.
![Page 6: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/6.jpg)
6
Deleting a Node with One Child
Case 2. The node, x, has one child.
![Page 7: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/7.jpg)
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
Replace contents of x with the contents of its inorder successor
K
Note that inorder successor will have no left child. Why?
![Page 8: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/8.jpg)
8
Delete the former inorder successor node as described for cases 1 and 2
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.
![Page 9: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/9.jpg)
9
Implementing Deletion
Download most recent version of BST.
http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/2011_03_21_Delete_Node/
BST_Vertical_Display
Expand. Build and run. Rename folder BST_with_Delete
![Page 10: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/10.jpg)
10
Downloaded Program Running
![Page 11: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/11.jpg)
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<T> *p,
BSTNode<T> *prev);
void delete_node_with_one_child(BSTNode<T> *p,
BSTNode<T> *prev) ;
void delete_node_with_two_children(BSTNode<T> *p) ;
![Page 12: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/12.jpg)
12
Implementation
Copy: http://www.cse.usf.edu/~turnerr/Data_Structures/
Downloads/2011_03_21_Delete_Node/ File BST_Delete_Node.cpp.txt
![Page 13: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/13.jpg)
13
Below Class Defintion
template<class T>
void BST<T>::delete_node(const T& el)
{
BSTNode<T> *p = root;
BSTNode<T> *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;
}
}
![Page 14: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/14.jpg)
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
}
![Page 15: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/15.jpg)
15
Delete Node Method (continued)
// Delete leaf;
delete_leaf(p, prev);
}
![Page 16: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/16.jpg)
16
delete_leaf()
template<class T>
void BST<T>::delete_leaf(BSTNode<T> *p,
BSTNode<T> *prev)
{
cout << "Deleting leaf " << p->key << endl;
if (prev->left == p)
{
prev->left = 0;
}
else
{
prev->right = 0;
}
delete(p);
}
![Page 17: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/17.jpg)
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;
}
![Page 18: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/18.jpg)
18
Deleting Leaf Node
![Page 19: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/19.jpg)
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;
![Page 20: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/20.jpg)
20
Deleting Nonexistent Node
![Page 21: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/21.jpg)
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: http://www.cse.usf.edu/~turnerr/Data_Structures/
Downloads/2011_03_21_Delete_Node/ File delete_node_with_one_child.cpp.txt
![Page 22: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/22.jpg)
22
Delete Node with One Child
template<class T>
void BST<T>::delete_node_with_one_child(BSTNode<T> *p,
BSTNode<T> *prev)
{
cout << "Deleting node with one child, " << p->key << endl;
BSTNode<T>* 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);
}
![Page 23: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/23.jpg)
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;
}
![Page 24: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/24.jpg)
24
Deleting Node with One Child
![Page 25: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/25.jpg)
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:http://www.cse.usf.edu/~turnerr/Data_Structures/
Downloads/2011_03_21_Delete_Node/ File delete_node_with_two_children.cpp.txt
![Page 26: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/26.jpg)
26
Delete Node with Two Children
template<class T>
void BST<T>::delete_node_with_two_children(BSTNode<T> *p)
{
cout << "Deleting node with two children, " << p->key << endl;
// Find inorder successor to node to be deleted.
BSTNode<T>* suc = p->right;
BSTNode<T>* prev = p;
while (suc->left != 0)
{
prev = suc;
suc = suc->left;
}
![Page 27: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/27.jpg)
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);
}
}
![Page 28: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/28.jpg)
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;
}
![Page 29: Binary Search Trees III Delete](https://reader035.fdocuments.net/reader035/viewer/2022062517/56812d18550346895d920baf/html5/thumbnails/29.jpg)
29
Deleting Node with Two Children