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

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

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

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Today’s Topics Questions? Comments? Program 1 comments Binary trees –preOrder, inOrder, postOrder traversals using recursion –ways to implement a binary tree Binary Search trees –properties and operations

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Program 1 1. I ran every one of your programs and for at least a few tests that I ran, I found that your program either blew up OR displayed a non-existent student. 2. I suggest you all test out your programs with the list of tests I sent out in email today to verify that they work. If they don't, fix them and retest.... I will allot 1.5 hours of tomorrow's lab to do this. 3. I will read your code in the mean time and hopefully have suggestions for each of you on what you may be able to improve in terms of either readability or efficiency etc.

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Tree terminology definitions (corrected)‏ A full binary tree is a binary tree where node has either 0 or 2 children. A perfect binary tree is a binary tree where every leaf has the same depth AND all internal nodes have degree 2. A complete binary tree is a binary tree where the leaves with the maximum depth are all on the left (and any other leaves are only one depth less). In other words, every level of the tree except the deepest level, must contain as many nodes as possible. At the deepest level the nodes must be as far left as possible.

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 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

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Implementing a binary tree Let's create a BTNode class which represents a node in a binary tree and a BinaryTree class which represents a binary tree. Then we'll add code to it to add a node as a left child, add a node as a right child, etc. Let's try to store the binary tree I drew on the board last class period.

Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Implementing binary tree traversals // this method lives inside the BTNode class and we call it // on the root node like: root.preOrderPrint(); public void preOrderPrint()‏ { System.out.println(data); if (left != null)‏ left.preOrderPrint(); if (right != null)‏ right.preOrderPrint(); }

Michael Eckmann - Skidmore College - CS 206 - Spring 2006 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 0 th index will hold the root data, the 1 st and 2 nd 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 3 rd and 4 th indices etc. Each level of the binary tree is stored in contiguous indices.

Michael Eckmann - Skidmore College - CS 206 - Spring 2006 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?

Michael Eckmann - Skidmore College - CS 206 - Spring 2006 binary tree representation This scheme only works really well for full 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.

Michael Eckmann - Skidmore College - CS 206 - Spring 2006 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?

Michael Eckmann - Skidmore College - CS 206 - Spring 2006 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.

Michael Eckmann - Skidmore College - CS 206 - Spring 2006 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.

Michael Eckmann - Skidmore College - CS 206 - Spring 2006 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.

Michael Eckmann - Skidmore College - CS 206 - Spring 2006 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?

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

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.

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)‏http://www.nist.gov/

Binary Search Trees Each node in a BST has – key – left reference – right reference – parent reference

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?

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

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?

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 – Delete a key – Height of a BST

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

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?

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

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?

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;

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?

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?

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

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

Insert a key in a BST insert(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

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?

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.

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.

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

Similar presentations