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

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

Embed Size (px)

Citation preview

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

CS 206Introduction to Computer Science II

09 / 26 / 2008

Instructor: Michael Eckmann

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

Michael Eckmann - Skidmore College - CS 206 - Fall 2008

Today’s Topics• Questions? Comments?• Binary Search trees

– operations

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

Binary Search TreesLet'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

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

Print the KeysTo 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?

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

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?

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

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

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

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?

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

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?

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

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?

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

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?

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

Insert a key in a BST• How might we devise an algorithm to insert a key into the

tree?

• Can the key go anywhere?

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

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.

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

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?

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

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.

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

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?

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

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

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

Height of BSTpublic int height(BSTNode n){

if (n == null)return -1;

elsereturn 1 + Math.max(height(n.left), height(n.right));

}

// let's verify this will work with an example.

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

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.

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

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.

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

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.

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

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

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

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?

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

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)