Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 206 Introduction to Computer Science II 09 / 29 / 2008 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 206 Introduction to Computer Science II 09 / 29 / 2008 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 206 Introduction to Computer Science II 09 / 29 / 2008 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Today’s Topics Questions? Comments? Binary Search trees –Height – Remove rightmost – Remove leftmost – Delete a key

3 Binary Search Trees Let's look at algorithms to do the following – Height of a BST – Remove rightmost – Remove leftmost – Delete a key

4 Height of BST determining the depth (aka height) of the BST –Recall, the depth (height) of any tree is the maximum depth of any of its leaves. The depth of a node n, is the number of “steps” from the root to the node n. –Let's try to figure out an algorithm to determine the height of a tree recursively. –To do recursion we need a base case and a recursive step. –If a tree has 1 node, then it's height = 0. –If a tree has 0 nodes, then it's height should be less than 0, say - 1. –Also, given a node n (that is not null), that node n is the root of a subtree, and the height of this subtree is what?

5 Height of BST determining the depth (aka height) of the BST –Recall, the depth (height) of any tree is the maximum depth of any of its leaves. The depth of a node n, is the number of “steps” from the root to the node n. –Let's try to figure out an algorithm to determine the height of a tree recursively. –To do recursion we need a base case and a recursive step. –If a tree only has 1 node, the root, what's it's height? 0 –If a tree has 2 nodes, a root and a left (or right) node, then it has height 1. –Also, given a node n (that is not null), that node n is the root of a subtree, and the height of this subtree is 1 + Math.max(height(n.left), height(n.right))‏

6 Height of BST public int height(BSTNode n)‏ { if (n == null)‏ return -1; else return 1 + Math.max(height(n.left), height(n.right)); }

7 Remove Maximum Key in a BST Let's consider how to do this. This is equivalent to finding the rightmost node and removing it. Consider several cases.

8 Remove rightmost node in a binary tree To remove the rightmost node in any binary tree –Case 0) if the rightmost node is the root of the tree, do what? –Case 1) If the rightmost node has no left child, then it is a leaf, so you can simply do rightmost.parent.right = null to remove it –Case 2) If the rightmost node has a left child i.e. (rightmost.left != null), then move the whole left subtree to where the current rightmost node is. That is, do rightmost.parent.right = rightmost.left AND set the parent rightmost.left.parent = rightmost.parent We will have to do more than this if we are looking to remove the RM in an arbitrary subtree of a tree.

9 Remove leftmost node in a binary tree To remove the leftmost node in any binary tree –Case 0) if the leftmost node is the root of the tree, do what? –Case 1) If the leftmost node has no right child, then it is a leaf, so you can simply do leftmost.parent.left = null to remove it –Case 2) If the leftmost node has a right child, then move the whole right subtree to where the current leftmost node is. That is, do leftmost.parent.left = leftmost.right AND set the parent leftmost.right.parent = leftmost.parent; Again, we will have to do more than this if we are looking to remove the LM in an arbitrary subtree of a tree.

10 Delete a key in a BST First, start with currNode = root Go down the tree until currNode.key == searchkey. Each step down the tree change currNode to be either the left or right child. So, when the above step is done, we have currNode.parent as the parent and currNode as the node to delete.

11 Delete a key in a BST There are several cases to consider –1) if searchkey was not found –2) currNode == root and currNode.left == null –3) currNode == root and currNode.right == null –4) currNode != root and currNode.left == null –5) currNode != root and currNode.right == null –6) currNode == root and both right and left are not null –7) currNode != root and both right and left are not null

12 Delete a key in a BST There are several cases to consider –1) if searchkey was not found –done –2) currNode == root and currNode.left == null –make root.right be the root and new root's parent be null –3) currNode == root and currNode.right == null –make root.left be the root and new root's parent be null

13 Delete a key in a BST There are several cases to consider –4) currNode != root and currNode.left == null –Make currNode's parent now point to currNode.right Change parent's left or right depending on whether currNode was the left or right child –5) currNode != root and currNode.right == null –Make currNode's parent now point to currNode.left Change parent's left or right depending on whether currNode was the left or right child

14 Delete a key in a BST There are several cases to consider –6) currNode == root and both right and left are not null –7) currNode != root and both right and left are not null –These are the hard ones because the node to delete has two children. We need to maintain the properties of the BST after deletion. –We'll look at the left subtree of currNode and find it's rightMost node, store it in lstrmn (left subtree's rightmost node). –Then we can a) set currNode.key = lstrmn.key b) remove the node lstrmn from the tree by calling removeRightmost(currNode)‏ –Which means to remove the RM node in the subtree that has currNode as the root Let's fix removeRightmost to handle arbitrary subtrees


Download ppt "CS 206 Introduction to Computer Science II 09 / 29 / 2008 Instructor: Michael Eckmann."

Similar presentations


Ads by Google