Presentation is loading. Please wait.

Presentation is loading. Please wait.

Podcast Ch18c Title: BST delete operation

Similar presentations


Presentation on theme: "Podcast Ch18c Title: BST delete operation"— Presentation transcript:

1 Podcast Ch18c Title: BST delete operation
Description: Deleting a leaf; deleting a node with one nonnull child; deleting a node with two nonnull children Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

2 Deleting a Node The private method removeNode() erases a node from the tree by finding a replacement node somewhere else in the tree and using it as a substitute for the deleted node. Choose the node so that, when it takes the place of the deleted node, its value maintains the structure of the tree. Subtrees for the deleted node and the replacement node must be reconnected in such a way that the new tree maintains search tree ordering.

3 Deleting a Node (continued)
Notation: dNode identifies the deleted node D. pNode, identifies the parent P of the deleted node. When pNode is null, we are deleting the root. The removeNode() method sets out to find a replacement node R with reference rNode. The algorithm for finding a replacement node considers two cases that depend on the number of children attached to node D.

4 Deleted Node has Empty Subtree
The other child becomes the replacement node R. If the deleted node is a leaf node, it has two null children and the other node R is null.

5 Deleted Node has an Empty Subtree (continued)

6 Deleted Node has Empty Subtree (cont)
The algorithm must handle two special cases, one in which the deleted node is a leaf node and the other in which the deleted node is the root.

7 removeNode() (one null child)
private void removeNode(STNode<T> dNode){ if (dNode == null) return; // dNode = reference to node D that is deleted // pNode = reference to parent P of node D // rNode = reference to node R that replaces D STNode<T> pNode, rNode; // assign pNode as a reference to P pNode = dNode.parent; // if D has a null child, the // replacement node is the other child if (dNode.left == null || dNode.right == null){ if (dNode.right == null) rNode = dNode.left; else rNode = dNode.right;

8 removeNode() (one null child)
if (rNode != null) // the parent of R is now the parent of D rNode.parent = pNode; // deleting the root node; assign new root if (pNode == null) root = rNode; // attach R to the correct branch of P else if (((Comparable<T>)dNode.nodeValue). compareTo(pNode.nodeValue) < 0) pNode.left = rNode; else pNode.right = rNode; } ...

9 Deleted Node has Two Nonnull Children
A node with two children has some elements in its subtrees that are less than and some elements that are greater than its value. Find a replacement node that maintains the correct ordering among the items. Rather than removing the deleted node, update its value with that of the replacement node. Splice the replacement node out of the tree.

10 Deleted Node has Two Nonnull Children (continued)
Select R as the node with the smallest value that is greater than the value of the deleted node. R is the leftmost node in the right subtree of D.

11 Deleted Node has Two Nonnull Children (continued)
Move to the right child of D and then far-left to the replacement node R, maintaining a reference, PofR, to the parent of R. Splice out R from the tree

12 removeNode() (two nonnull children)
public void removeNode(STNode<T> dNode) { if (dNode.left == null || dNode.right == null) { . . . } // deleted node has two nonnull children else { // pOfRNode is reference to parent of // replacement node STNode<T> pOfRNode = dNode; // first possible replacement is right // child of D; the reference to PofR, // pOfRNode, is the deleted node rNode = dNode.right; pOfRNode = dNode;

13 removeNode() (two nonnull children continued)
// descend down path of left children, keeping // a record of the current node and its parent; // stop at the replacement node while(rNode.left != null) { pOfRNode = rNode; rNode = rNode.left; } . . .

14 removeNode() (two nonnull children continued)
Complete the process by copying the value of the replacement node to D and then connect the right subtree of R to the tree. If PofR is the deleted node, then connect the right subtree of R as the rightchild of D; otherwise connect the right child of R as the left child of PofR. Assign the parent of a nonnull right subtree of R to be pOfRNode, the parent of R.

15 removeNode() (two nonnull children concluded)
// copy the value in R to D dNode.nodeValue = rNode.nodeValue; if (pOfRNode == dNode) dNode.right = rNode.right; else pOfRNode.left = rNode.right; // the parent of the right child of R // becomes the parent of R if (rNode.right != null) rNode.right.parent = pOfRNode;

16 The method remove() // if item is in the tree, remove it
// and return true; otherwise, return false public boolean remove(Object item) { // search tree for item STNode<T> dNode = findNode(item); if (dNode == null) return false; removeNode(dNode); treeSize--; modCount++; return true; }

17 Student Question Given the tree
Draw the tree after each of the following sequential deletions: 20, 25, 35

18 Practice Problem Given the tree
Draw the tree after each of the following sequential deletions: 26, 33, 65, 40


Download ppt "Podcast Ch18c Title: BST delete operation"

Similar presentations


Ads by Google