Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Today’s Topics Questions? Comments? Binary Search trees –operations

3 Binary Search Trees Let's look at algorithms to do the following – Print keys in ascending order – Search for a key – Find minimum key – Find maximum key – Insert a key – Height of a BST – Delete a key

4 Print the Keys To print the keys in increasing order we use inorder traversal. Recursive description of inorder traversal In-order traversal of a tree Start with x being the root check if x is not null then 1) In-order traversal of left(x)‏ 2) print key(x)‏ 3) In-order traversal of right(x)‏ What order of running time is this algorithm for a tree that has n nodes?

5 Search for a key To search in a binary search tree for a key k, start with x being the root. Here's a recursive description of a search tree_search(x, k)‏ { if (x == null || k == x.key)‏ return x; if (k < x.key)‏ return tree_search(x.left, k)‏ else return tree_search(x.right, k)‏ } What's the running time of this?

6 Search for a key To search in a binary search tree for a key k, start with x being the root. Here's a recursive description of a search tree_search(x, k)‏ { if (x == null || k == x.key)‏ return x; if (k < x.key)‏ return tree_search(x.left, k)‏ else return tree_search(x.right, k)‏ } What's the running time of this? On the order of the height of the tree. What if the binary search tree is complete (or full.)‏

7 Find Minimum key in a BST How might we devise an algorithm to do “find the minimum”? Where in the tree is the minimum value?

8 Find Minimum key in a BST How might we devise an algorithm to do find the minimum? Where in the tree is the minimum value? – It is in the leftmost node x = root; // make sure x is not null before doing this... while (x.left != null)‏ x = x.left; return x; – Is it necessarily a leaf?

9 Find Maximum key in a BST How might we devise an algorithm to do “find the maximum”? Where in the tree is the maximum value?

10 Find Maximum key in a BST How might we devise an algorithm to do find the maximum? Where in the tree is the maximum value? – It is in the rightmost node x = root; // make sure x is not null before doing this... while (x.right != null)‏ x = x.right; return x; Running times of these?

11 Insert a key in a BST How might we devise an algorithm to insert a key into the tree? Can the key go anywhere?

12 Insert a key in a BST How might we devise an algorithm to insert a key into the tree? Can the key go anywhere? No, it has to follow the rules of BST's so the resulting tree after insert must be a BST. Need to keep track of where we are in the tree as we traverse it and the parent of where we are because we might have to go back up the tree. Let's look at the implementation from yesterday's lab.

13 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. –Any ideas?

14 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.

15 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?

16 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))‏

17 Height of BST public int height(BSTNode n)‏ { if (n == null)‏ return -1; else return 1 + Math.max(height(n.left), height(n.right)); } // let's verify this will work with an example.

18 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.

19 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.

20 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.

21 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. There are several cases to consider –1) if searchkey was not found –2) currNode == root and currNode.left == null –3) currNode != root and currNode.left == null –4) currNode.left != null

22 Delete a key in a BST Case 1) if searchkey was not found, do nothing --- done. Case 2) currNode == root and currNode.left == null –Set the root to be root.right, AND then root.parent = null, done. Case 3) currNode != root and currNode.left == null –if the currNode is its parent's left child then currNode.parent.left = currNode.right –if the currNode is its parent's right child then currNode.parent.right = currNode.right Case 4) currNode.left != null –This is the hard one. Why?

23 Delete a key in a BST Case 4) currNode != root and currNode.left != null –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)‏


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

Similar presentations


Ads by Google