Download presentation
Presentation is loading. Please wait.
1
CSC 205 – Java Programming II
Lecture 36 April 19, 2002
2
Deletion Search will be used
A separate deleteNode method will also be called deletItem(rootNode, searchKey) //Deletes from the bst with root rootNode the item //whose search key equals searchKey. If no such item //exists, the operation fails & throws TreeException. Locate (by using the search algorithm) the item whose search key equals searchKey ; it occurs in node i if (item is found in node i) deleteNode(i) //defined next else throws a tree exception
3
Deletion – 3 Scenarios Delete is much more complicated
There are three different cases to consider Assume that item i is in node N N is a leaf – simply delete it N has only one child – replace N w/ its child N has two children – needs more discussion
4
With Two Children After deleting the node N from a BST, the resultant binary tree should still be a BST The following steps are needed in this case Locate another node M that is easier to remove from the BST than the node N. That is, M has at most one child. Copy the item that is in M to N Remove the node M from the BST
5
Example – deleting the root
Nancy Jane Bob Tom Alan Ellen Nancy Wendy M Alan, Bob, Ellen, Jane, Nancy, Tom, Wendy
6
Result – still a BST Alan, Bob, Ellen, Nancy, Tom, Wendy Nancy Bob Tom
7
The deleteNode Algorithm
Recall that the inorder traversal displays the nodes in order according to their values Either the inorder successor or predecessor of N can be used to replace N In our example, either Nancy or Ellen would work They are the left-most descendant in the right subtree (Nancy, inorder successor), and the right-most descendant in the left subtree (Ellen, inorder predecessor), respectively
8
Java Implementation Employ the following mechanism
public void delete(Comparable searchKey) Standard interface for deleting an item by value protected TreeNode deleteItem(TreeNode node, Comparable searchKey) Locate the node and call deleteNode to delete it protected TreeNode deleteNode(TreeNode node, Comparable searchKey) Take care of 4 possible cases Still need help from two more methods
9
The delete Method public void delete(Comparable searchKey)
throws TreeException { root = deleteItem(root, searchKey); }
10
The deleteItem Method protect TreeNode deleteItem(TreeNode tNode,
Comparable key) { TrreeNode newSubtree; if (tNode == null) throw new TreeException(…); else if (/* tNode’s item is key */) tNode = deleteNode(tNode); else if (/* key < tNode’s item */) { newSubtree = deleteItem(tNode.getLeft(), key); tNode.setLeft(newSubtree); } else { newSubtree = deleteItem(tNode.getRight(), key); tNode.setRight(newSubtree); } return tNode; }
11
The deleteNode Method protect TreeNode deleteNode(TreeNode tNode) {
KeyedItem item; if (/*tNode is a leaf */) return null; else if (/* tNode has one child*/) return tNode.getLeft(); //or tNode.getRight(); else { // has both children item = findLeftmost(tNode.getRight()); tNode.setItem(item ); tNode.setRight(deleteLeftmost(tNode.getRight()); } return tNode;
12
The findLeftmost Method
protect KeyedItem findLeftmost(TreeNode tNode) { if (tNode.getLeft() == null) { return (KeyedItem)tNode.getItem(); } else { return findLeftmost(tNode.getLeft());
13
The deleteLeftmost Method
protect TreeNode deleteLeftmost(TreeNode tNode) { if (tNode.getLeft() == null) { return tNode.getRight(); } else { tNode.setLeft(deleteLeftmost(tNode.getLeft())); return tNode;
14
Example
15
Example
16
Example
17
Number of Leaves For a complete tree, at least half of its nodes are leaves L = N/2 N=10 : L=5 N=13 : L=7
18
L = N/2 Tree height is H # of nodes on level H – 1 is 2H–1
Assume there are lH-1 leaves on level H – 1 # of leaves on level H <= 2*(2H-1 – lH-1) # of Total nodes is 2H – 1 + 2*(2H-1 – lH-1) # of leaves is 2*(2H-1– lH-1) + lH-1 = 2H – lH-1 Leaf-Node Ratio is (2H – lH-1)/[2* (2H – lH-1) – 1] ~= 1/2
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.