cs 206 introduction to computer science ii 02 / 13 / 2009 instructor: michael eckmann

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

Upload: calvin-butler

Post on 04-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

CS 206Introduction to Computer Science II

02 / 13 / 2009

Instructor: Michael Eckmann

Page 2: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 206 - Spring 2009

Today’s Topics• Questions/comments• Binary trees

– preOrder, inOrder, postOrder traversals using recursion

– ways to implement a binary tree

• Binary Search trees– properties and operations

Page 3: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 206 - Spring 2009

Implementation of binary trees• Similar to how we implemented a linked list of nodes, we can

implement a binary tree of nodes where each node contains a reference to a left child and a right child (each of which could be null.)

public class BTNode{

private int data;public BTNode left;public BTNode right;

// constructor that sets left and right to nullpublic BTNode(int i){

data = i; left = null; right = null;}

}

Page 4: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 206 - Spring 2009

Implementation of binary trees

public class BinaryTree{

private BTNode root;

// constructor that sets left and right to nullpublic BinaryTree(){

root = null;}

// plenty more methods here to insert nodes, etc...}

Page 5: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 206 - Spring 2009

Traversals of binary trees• There are three typical ways to traverse a binary tree –

preOrder, postOrder and inOrder.• preOrder

– root, left, right• postOrder

– left, right, root• inOrder

– left, root, right

• Applet:

http://www.cosc.canterbury.ac.nz/mukundan/dsal/BTree.html

Page 6: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 206 - Spring 2009

binary tree representation• Besides representing a binary tree as a structure of nodes

where each node has

– data and

– references to other nodes (left and right).

we can store a binary tree in an array. The 0th index will hold the root data, the 1st and 2nd will hold the root's left and right children's data respectively. The root's left child's left and right children's data are in 3rd and 4th indices etc.

• Each level of the binary tree is stored in contiguous indices.

Page 7: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 206 - Spring 2009

binary tree representation• Node i's children are at 2i + 1 and 2i + 2.• Example: root is at 0, it's left child is at 1 and right is at 2.• i=0, 2i+1 = 1, 2i+2 = 2• Another example: the root's right child is at 2 and that node's

children are at 5 and 6.• i=2, 2i+1 = 5, 2i+2 = 6

• Now, given an index of a node, how can we determine where the parent lives?

Page 8: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 206 - Spring 2009

binary tree representation• This scheme only works really well for perfect binary trees or

complete binary trees.• Why? Because if we know the number of nodes in the tree,

that is the number of elements in the array.• Problem when we want to add to the tree.

– Possible solution is to create a really large array and keep track of the index of the deepest, right node and never allow any code to look at any indices higher than that except when adding nodes.

• Problem if the tree is not complete.

– We'll have holes in the array.

Page 9: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 206 - Spring 2009

binary tree representation• Problem if the tree is not complete.

– We'll have holes in the array.

• Why is this a problem?

• What possible solutions are there to storing an incomplete

binary tree in an array?

Page 10: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 206 - Spring 2009

binary tree representation• What possible solutions are there to storing an incomplete

binary tree in an array?

– Could keep another array of booleans whose elements are true if the index holds a node, false if the index does not hold a node.

Page 11: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 206 - Spring 2009

Abstract data types• Now is a good time to bring up the term abstract data type

(ADT).• We just discussed a few different ways of storing /

representing a binary tree. • But these different representations were assumed to allow the

same exact operations, such as traversing the tree in some order, or finding the children of a node, or finding all the ancestors of a node, etc.

• So, to a user of the binary tree data type, the implementation is transparent. Only the “interface” to the data type is needed to be known (e.g. What methods can be called and the parameters they take). How the data type was implemented is not needed to be known by the user of the data type.

Page 12: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 206 - Spring 2009

Abstract data types• This is a kind of information hiding. Hide how it's

implemented, while still providing a way to use it.• For example:

– The first programming assignment required a way to store a list of the students' names and scores.

• Different implementations were with a linked list, an Arraylist, and an array.

• Ideally the class containing your main method shouldn't have had to know anything about which way the list was implemented. It should have just needed to use the list by calling addstudent, changescore etc.

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

Michael Eckmann - Skidmore College - CS 206 - Spring 2009

Abstract data types• Information hiding is a good thing to keep in mind while

coding. • Think about what operations/methods are needed to be

provided for users of your class and provide them, but hide everything else.

• This will help you to keep your classes separated well.• Anyone have any comments?

Page 14: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Binary Search Trees

• Binary Search Trees (BSTs)

–Definition

– Example of a BST and verification that it is one

–Are BST's unique for a given set of data?

–Algorithms• print keys in ascending order• Search for a key• Find minimum key• Find maximum key• Insert a key

Page 15: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Binary Search Trees

• Binary Search Trees (BSTs)– A tree that is both a binary tree and a search tree.

– we know the definition of a tree and a binary tree so:

– We just need to define search tree.

• A search tree is a tree where – every subtree of a node has data (aka keys) less than any

other subtree of the node to its right. – the keys in a node are conceptually between subtrees and

are greater than any keys in subtrees to its left and less than any keys in subtrees to its right.

Page 16: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Binary Search Trees

• A binary search tree is a tree that is a binary tree and is a search tree. In other words it is a tree that has

– at most two children for each node and

– every node's left subtree has keys less than the node's key, and every right subtree has keys greater than the node's key.

– Definitions taken from http://www.nist.gov/ (The National Institute of Standards and Technology)

Page 17: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Binary Search Trees

• Each node in a BST has – key

– left reference

– right reference

– parent reference

Page 18: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Binary Search Trees F

/ \

B H

/ \ \

A D K

• Assume Alphabet ordering of letters.

• Let's verify whether it is indeed a binary search tree.

– What properties does it need to have again?

– Does it satisfy those properties?

Page 19: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Binary Search Trees• Are BST's unique for a given set of keys?

• Let's build a tree for the list of keys

– 13, 45, 10, 9, 54, 11, 42

– Choose 13 to put in the root and go from there

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

Binary Search Trees• Are BST's unique for a given set of keys?

• Let's build a tree for the list of keys

– 13, 45, 10, 9, 54, 11, 42

– What if we change the order that we insert the keys?

– What if we choose a different key as the root to start?

Page 21: CS 206 Introduction to Computer Science II 02 / 13 / 2009 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

–Delete a key

–Height of a BST

Page 22: CS 206 Introduction to Computer Science II 02 / 13 / 2009 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)

•Let's apply this algorithm to the tree and see what it does.

•What order is this algorithm for a tree that has n nodes?

Page 23: CS 206 Introduction to Computer Science II 02 / 13 / 2009 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 == key(x)) return x; if (k < key(x)) return tree_search(left(x), k) else return tree_search(right(x), k)}

What's the running time of this?

Page 24: CS 206 Introduction to Computer Science II 02 / 13 / 2009 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 == key(x)) return x; if (k < key(x)) return tree_search(left(x), k) else return tree_search(right(x), 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 25: CS 206 Introduction to Computer Science II 02 / 13 / 2009 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 26: CS 206 Introduction to Computer Science II 02 / 13 / 2009 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

while (left(x) != null)

x = left(x);

return x;

Page 27: CS 206 Introduction to Computer Science II 02 / 13 / 2009 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 28: CS 206 Introduction to Computer Science II 02 / 13 / 2009 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

while (right(x) != null)

x = right(x);

return x;

• Running times of these?

Page 29: CS 206 Introduction to Computer Science II 02 / 13 / 2009 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 30: CS 206 Introduction to Computer Science II 02 / 13 / 2009 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.

• z is the node to insert and key[z] is its key and its left, right and parents are null.

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

• par will be a pointer to the parent of x as we go through the tree

Page 31: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Insert a key in a BSTinsert(T, z) // insert node z in tree T

{

x = root(T);

par = null;

// search the tree to find the place it should go

while (x != null)

{

par = x;

if (key(z) < key(x))

x = left(x);

else

x = right(x);

}

// continued on next slide

Page 32: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Insert a key in a BST// now we know x is null and the parent of x is par so,

parent(z) = par;

// insert the key either at the root or to the left or right of par

if (par = null)

root(T) = z;

else

if (key(z) < key(par))

left(par) = z;

else

right(par) = z;

}

// any fear of overwriting a node? What if left(par) or right(par)

// contain a node?

Page 33: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

Insert a key in a BST// any fear of overwriting a node? What if left(par) or right(par)

// contain a node?

They can't contain a node because of the first half of the algorithm guarantees it

Examples:

Let's insert C into the original tree.

Then let's insert E.

Page 34: CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann

other operations on BST• determining the height of the BST

• deleting a key in a BST (and taking care of fixing the tree so that there are no

holes)

• given a node, find it's successor

• etc.

• We'll take a look at ideas for these and others next week.