Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.

Similar presentations


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

1 CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 206 - Fall 2009 Today’s Topics Comments/Questions? More on Trees / Binary trees –preOrder, inOrder, postOrder traversals using recursion –Binary tree representation/implementation Binary Search trees –properties and operations

3 Michael Eckmann - Skidmore College - CS 206 - Fall 2009 Traversals of binary trees There are three typical ways to traverse a binary tree – preOrder, postOrder and inOrder. preOrder –process root –process nodes in left subtree with a recursive call –process nodes in right subtree with a recursive call Example on the board of a preOrder traversal.

4 Michael Eckmann - Skidmore College - CS 206 - Fall 2009 Traversals of binary trees postOrder –process nodes in left subtree with a recursive call –process nodes in right subtree with a recursive call –process root Example on the board of a postOrder traversal.

5 Michael Eckmann - Skidmore College - CS 206 - Fall 2009 Traversals of binary trees inOrder –process nodes in left subtree with a recursive call –process root –process nodes in right subtree with a recursive call Example on the board of an inOrder traversal. Applet: http://www.cosc.canterbury.ac.nz/mukundan/dsal/BTree.html

6 Michael Eckmann - Skidmore College - CS 206 - Fall 2009 Implementing the traversals of binary trees Let's assume the processing we're doing to each node is just printing the data in that node. So, for preOrder traversal we need to do the following –print the root's data –do a preorder traversal of the left subtree –do a preorder traversal of the right subtree Notice the recursion?

7 Michael Eckmann - Skidmore College - CS 206 - Fall 2009 Implementing the traversals of binary trees The only issue is when to stop the recursion. To do a preOrder traversal of the left subtree there has to be a left subtree. If there is no left subtree (that is, left == null) then don't traverse that anymore. Same for a preOrder traversal of the right subtree - there has to be a right subtree to traverse. If there is no right subtree (that is, right == null) then don't traverse that anymore.

8 Michael Eckmann - Skidmore College - CS 206 - Fall 2009 Implementing the traversals of binary trees So, for preOrder traversal we need to do the following –print the root's data –if (left != null)‏ do a preorder traversal of the left subtree –if (right != null)‏ do a preorder traversal of the right subtree

9 Michael Eckmann - Skidmore College - CS 206 - Fall 2009 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(); }

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

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

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

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

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

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

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

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

18 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

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

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

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

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

23 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

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

25 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

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

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

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

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

30 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;

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

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

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

34 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

35 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

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

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

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


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

Similar presentations


Ads by Google