Presentation is loading. Please wait.

Presentation is loading. Please wait.

AITI Lecture 20 Trees, Binary Search Trees Adapted from MIT Course 1.00 Spring 2003 Lecture 28 and Tutorial Note 10 (Teachers: Please do not erase the.

Similar presentations


Presentation on theme: "AITI Lecture 20 Trees, Binary Search Trees Adapted from MIT Course 1.00 Spring 2003 Lecture 28 and Tutorial Note 10 (Teachers: Please do not erase the."— Presentation transcript:

1 AITI Lecture 20 Trees, Binary Search Trees Adapted from MIT Course 1.00 Spring 2003 Lecture 28 and Tutorial Note 10 (Teachers: Please do not erase the above note)

2 Binary Search Trees We define the concept of binary search tree as a binary tree of nodes containing an ordered key with the following additional property. The left subtree of every node (if it exists) must only contain nodes with keys less than or equal to the parent and the right subtree (if it exists) must only contain nodes with keys greater than or equal to the parent.

3 Goals We are going to implement a binary search tree with the usual data structure operations and then critique the implementation. Generalizations of the binary search tree drive many important applications, including commercial databases. The implementation will give you a feel for tree methods that tend to be more "visual" or "topological" than array or list implementations.

4 Binary tree example g bx dwz v root c

5 Implementing a Binary Tree We’ll build a Tree class: –One data member: root –One constructor: Tree() –Methods: insert: build a tree, node by node inorder traversal postorder traversal (we omit preorder)

6 Implementing, p.2 We also build a Node inner class: –Three data members: data, left, right –Three methods, all used by corresponding methods in the Tree class: insertNode traverseInorder traversePostorder –Methods are invoked on root node and then traverse the tree as needed

7 Exercise 1 Draw the tree that results from: public class TreeMain { public static void main(String[] args) { Tree z= new Tree(); z.insert("b"); z.insert("q"); z.insert("t"); z.insert("d"); z.insert("a"); // Four more lines to appear in exercise 2 }

8 Solution b aq dt

9 Exercise 2 What is the output if main() then contains the following 4 lines? System.out.println("Inorder"); z.inorder(); System.out.println("Postorder"); z.postorder(); Inorder is: –traverse left, visit (print) current, traverse right Postorder is: –Traverse left, traverse right, visit (print) current

10 Solution Inorder: –a b d q t Postorder: –a d t q b

11 Tree class class Tree { private Node root; public Tree() { root= null; } public void inorder() { if (root != null) root.traverseInorder(root); } public void postorder() { if (root != null) root.traversePostorder(root); } public void insert(Comparable o) { Node t= new Node(o); if (root==null) root= t; else root.insertNode(t); }

12 Node class: data, constructor private class Node { public Comparable data; public Node left; public Node right; public Node(Comparable o) { data= o; left= null; right= null; }

13 Node class, insertNode public void insertNode(Node n) { if (n.data.compareTo(data) < 0) { if (left==null) left= n; else left.insertNode(n); } else { if (right == null) right= n; else right.insertNode(n); }

14 insert() in Action 18 1125 71619 1217 32 29 3327 insert(20) null parent at end of failed search 20 new node 8

15 Exercise 3: traversal Write the two traversal methods in Node: public void traverseInorder( Node n) { if ( n != null ) { // Traverse left subtree // Print current Node // Traverse right subtree } public void traversePostorder( Node n) { if ( n != null ) { // Traverse left subtree // Traverse right subtree // Print current Node }

16 Solution public void traverseInorder( Node n) { if ( n != null ) { traverseInorder( n.left); System.out.println( n.data); traverseInorder( n.right); } public void traversePostorder( Node n) { if ( n != null ) { traversePostorder( n.left); traversePostorder( n.right); System.out.println( n.data); }

17 Exercise 4: Find Node This is very similar to insertNode: –In class Tree, add: public boolean find(Comparable o) { if (root== null) return false; else return root.findNode(o); } –In class Node, add: public boolean findNode(Comparable o) { if (o.compareTo(data) < 0) { // Add code here } else if (o.compareTo(data) > 0) { // Add code here } else // Equal return true; return false; } // Never reached

18 find() in Action 18 1125 71619 1217 32 29 3327 1 st iteration 2 nd iteration find(19) 8

19 Solution public boolean findNode(Comparable o) { if (o.compareTo(data) < 0) { if (left== null) return false; else left.findNode(o); } else if (o.compareTo(data) > 0) { if (right == null) return false; else right.findNode(o); } else // Equal return true; return false; // Never reached }

20 Keys and Values If binary search trees are ordered, then they must be ordered on some key possessed by every tree node. A node might contain nothing but the key, but it's often useful to allow each node to contain a key and a value. The key is used to look up the node. The value is extra data contained in the node indexed by the key.

21 Maps Such data structures with key/value pairs are usually called maps. As an example, consider the entries in a phone book as they might be entered in a binary search tree. The subscriber name, last name first, serves as the key, and the phone number serves as the value.

22 Key and Value Type Sometimes you may not need the value. But if one node has a value, you probably want to supply one for every node. We also haven't specified the type of the key and value. They can be of any class you want, but all nodes should possess keys that are instances of the same class and values that are instances of the same class. Keys and values do not have to be the same type. Keys and values will be handled as Object s by our tree implementation except when we need to compare them.

23 Duplicate Keys Do we allow the tree to contain nodes with identical keys? There is nothing in the concept of a binary search tree that prevents duplicate keys. In the interface that we will implement, we use the keys to access the values. If we allowed duplicate keys, we wouldn't be able to distinguish between multiple nodes possessing the same key. Thus, in this implementation, duplicate keys will not be allowed. Duplicate values associated with different keys are legal.

24 Maps Implementing tree structures with keys and values is a straightforward extension to what we just did. The Node contains: –Key –Value –Left –Right

25 The Efficiency of Binary Search It makes intuitive sense that the basic operations on a binary search tree should require O(h) time where h is the height of the tree. It turns out that the height of a balanced binary tree is roughly log 2 (n) where n is the number of elements if the tree remains approximately balanced. It can be proved that if keys are randomly inserted in a binary search tree, this condition will be met, and the tree will remain adequately enough balanced so that search and insertion time will approximate O(log n).

26 Tree Balance There are some extremely simple and common cases, however, where keys will not be inserted in random order. Consider what will happen if you insert keys into a search tree from a sorted list. The tree will assume a degenerate form equivalent to the source list and search and insertion times will degrade to O(n). There are many variants of trees, e.g., red-black trees, AVL trees, B-trees, that try to solve this problem by rebalancing the tree after operations that unbalance it.

27 Keys Inserted in Order 1 2 3 4

28 delete() Strategy The last slides show what happens to delete nodes from a tree. It’s messy, and we cover it only in overview form. When we delete a node from a binary search tree, we must ensure that the resulting structure (1) is still a tree, and (2) the tree still obeys the rule of a binary search tree. The rule requires that for every node, the keys in the left subtree if present precede the key of the node which must precede the keys in the right subtree.

29 delete() Cases, 1 There are three deletion cases we must consider: 1.The deleted node has no children, e.g., node 29 below. 2.The deleted node has one child, e.g., node 7 below. 3.The deleted node has two children, e.g., node 25 below 18 1125 71619 1217 32 29 33278

30 delete(), No Children 18 1125 71619 1217 32 3327 29 8

31 delete(), 1 Child 18 1125 71619 1217 32 3327 29 8 8 8

32 delete(), 2 Children 18 1125 71619 1217 32 3327 29 8

33 delete(), 2 Children Strategy In the last case, that of two children, the problem is more serious since the tree is now in three parts. The solution starts by realizing that we can minimize the ordering problem by stitching the tree back together using the node immediately preceding or following the deleted node in inorder sequence. These are known as the predecessor and successor nodes.

34 Using the Successor Node Let's choose the successor node. The successor of a node with a right subtree will be the first node of that subtree. If we replace the deleted node by its successor, then all ordering conditions will be met. But what about the successor's children. The successor of a node with two children can have at most one child (a right child). If it had a left subtree, the node couldn't be the successor since members of the left subtree follow the deleted node but precede the successor. Since the successor can have at most one subtree, we can move the successor up to replace the node we want to remove and relink its right subtree, if present, as in the second case above.

35 delete(), find successor 18 1125 71619 1217 32 3327 29 8 first of right subtree

36 delete(), replace successor 1 18 11 25 71619 1217 32 3327 29 8

37 delete(), replace successor 2 18 11 25 71619 1217 32 3327 29 8 27

38 delete(), replace successor 3 18 11 25 71619 1217 32 33 29 8 27 29


Download ppt "AITI Lecture 20 Trees, Binary Search Trees Adapted from MIT Course 1.00 Spring 2003 Lecture 28 and Tutorial Note 10 (Teachers: Please do not erase the."

Similar presentations


Ads by Google