# CS 206 Introduction to Computer Science II 02 / 20 / 2009 Instructor: Michael Eckmann.

## Presentation on theme: "CS 206 Introduction to Computer Science II 02 / 20 / 2009 Instructor: Michael Eckmann."— Presentation transcript:

CS 206 Introduction to Computer Science II 02 / 20 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 206 - Spring 2009 Today’s Topics Questions/comments Binary Search trees Continue coding the following: –Delete a key –Remove rightmost in a subtree Recursion discussion and examples

Delete a key in a BST Let's reconsider the cases: –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

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

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

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 its 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 talk about fixing removeRightmost (to handle arbitrary subtrees)

removeRightMost in a subtree removeRightmost (in an arbitrary subtree) –Cases – RM is root of subtree and has a left child Is RM also the root of whole tree? Is RM his parent's left or right child? – RM is root of subtree and has no left child Is RM also the root of whole tree? Is RM his parent's left or right child? – RM is not root of subtree and has a left child Do we know the relation between RM and its parent? – RM is not root of subtree and has no left child Do we know the relation between RM and its parent?

Allowing duplicate values One way to allow duplicate values in our BST, is --- if a key = some key then it lives on the right side. Another way would be to change the BSTNode to contain a count value which describes how many are in the tree. When you want to add a node with a key that already exists, instead of inserting the new node, just ++ the count of that node. To delete, decrement the count. When it gets to 0, remove the node completely.

Allowing duplicate values Assuming we implement it in the way that each node keeps track of its count. How will this idea effect traversals? How about delete? Other concerns?

Recursion 1. have at least one base case that is not recursive 2. recursive case(s) must progress towards the base case 3. trust that your recursive call does what it says it will do (without having to unravel all the recursion in your head.)‏ 4. try not to do redundant work. That is, in different recursive calls, don't recalculate the same info.

Recursion some definitions are inherently recursive e.g. sum of the first n integers, n>= 1. S(1) = 1 S(N) = S(N-1) + N recursive code for this iterative code for this simpler code for this (based on our proof) N*(N+1)/2 any problems with the recursive code? n=0 or large n?

Recursion The last example showed that recursion didn't really simplify our lives, but there are times when it does. e.g. If given an integer and you wanted to print the individual digits in order, but you didn't have the ability to easily convert an int >10 to a String. e.g. int n=35672; If we wanted to print 3 first then 5 then 6 then 7 then 2, we need to come up with a way to extract those digits via some mathematical computation. It's easy to get the last digit n%10 gives us that. Notice: 35672 % 10 = 2 also 2 % 10 = 2. Any ideas on a recursive way to display all the numbers in order?

Recursion void printDigits(int n)‏ { if (n>=10)‏ printDigits((n/10)); System.out.println( n%10 ); } // what's the base case here? // what's the recursive step here? Will it always approach the base case?

Recursion Now that last problem was made up sort of, because Java (and most languages) allow us to print ints. However what if we wanted to print the int in a different base? Say base 2, 3, 4, 5, or some other base?

Download ppt "CS 206 Introduction to Computer Science II 02 / 20 / 2009 Instructor: Michael Eckmann."

Similar presentations