1 Trees, Trees, and More Trees
2 By looking at forests of terms, awesome animations, and complete examples, we hope to get at the root of trees. Hopefully, each of you leaves with something new and exciting learned about trees. Try not to lose sight of the forest through the trees. Sorry, this was all very bad. I hope it didn’t leaf a bad taste in your mouth.
3 TREES A binary tree is a set of elements that is either empty or contains a single element (the root of the tree) and whose remaining elements are partitioned into two disjoint subsets, each of which itself is a binary tree.
4 H D A G E P LW R root Right subtree rooted at P Left subtree
5 H D A G E P LW R root level 0 level 1 level 2 level 3 Right subtree Left subtree
6 Full Binary Tree of Level n
7 A B F C ED JIHK LM G N O A is the root of the tree. A is the parent of B and C. A is an ancestor of all nodes.
8 A B F C ED JIHK LM G N O B and C are siblings. J is a descendent of B.
9 Height of binary tree : Number of nodes on the longest path from the root to a leaf. The height of the empty tree is 0. The height of a single node tree is 1. Note: (not an AP term) the definition of “height” is not from some “cs bible” – some will define other ways
10 A B F C ED JIHK LM G N O Height of binary tree ?
11 A B F C ED JIHK LM G N O Height of binary tree ? Height = 4
12 Full Binary : A recursive definition A binary tree is full if the tree is empty or if the tree's left and right subtrees have the same height and both are full
13 Binary Search Tree A binary search tree : every node in the left subtree is less than or equal to its parent node. Every node in the right subtree is greater than its parent node When the tree is traversed in order, the values of the nodes will be in order.
14 To insert to a binary search tree: if value less than go left else go right
15 Insert :
16 15 Insert :
17 15 Insert :
18 15 Insert :
19 15 Insert :
20 15 Insert :
21 15 Insert :
22 15 Insert :
23 15 Insert :
24 15 Insert :
25 15 Insert :
26 Tree Traversals See also: animations on web site
27 A B F C ED JIHK LM G N O Inorder traversal of a binary tree left…root…right
28 A B F C ED JIHK LM G N O Inorder traversal of a binary tree left…root…right H D I B J E K A L F M C N G O
29 A B F C ED JIHK LM G N O Preorder traversal of a binary tree: root…left…right
30 A B F C ED JIHK LM G N O Preorder traversal of a binary tree: root…left…right A B D H I E J KC F L M G N O
31 A B F C ED JIHK LM G N O Postorder traversal of a binary tree: left…right…ROOT
32 A B F C ED JIHK LM G N O Postorder traversal of a binary tree: left…right…ROOT H I D J K E B L M F N O G C A
33 L B F W EX AQ P G T E Inorder traversal ?
34 L B F W EX AQ P G T E Inorder traversal : XA B E Q L P F W T G E
35 L B F W EX AQ P G T E Preorder traversal ?
36 L B F W EX AQ P G T E Preorder traversal: L B X A E Q W F PG T E
37 L B F W EX AQ P G T E Postorder traversal ?
38 L B F W EX AQ P G T E Postorder traversal : A X Q E B P F T E G W L
39 breadth-first-order tree traversal ROW (or level) order traversal A BC DE FG HI
40 breadth-first-order tree traversal ROW (or level) order traversal A BC DE FG HI A B C D E F G H I
41 15 Inorder Traversal ?
42 15 Inorder Traversal :
43 15 Preorder Traversal ?
44 15 Preorder Traversal :
45 15 Postorder Traversal ?
46 15 Postorder Traversal :
47 15 Our Tree: What is the height of the tree?
48 15 Height : Height = 5
49 Deleting a node from a binary tree L D H C A F J P
50 To delete a leaf... L D H C A F J P
51 To delete a leaf... L D H C A F J P Set appropriate parent to NULL
52 To delete a leaf... L D H C A F P
53 Deleting a node with one child... L D H C A F J P
54 Deleting a node with one child... L D H C A F J P Make appropriate left or right reference of parent skip over the deleted node and reference the child of the node we want to delete
55 Deleting a node with one child... L D H A F J P
56 Deleting a node with 2 children... L D H C A F J P
57 Deleting a node with 2 children... L D H C A F J P Go once left and as far right as you can for node’s replacement.
58 Deleting a node with 2 children... D H C A F J P Go once left and as far right as you can.
59 Binary Search Trees AP Implementation
60 public class TreeNode { private Object value; private TreeNode left; private TreeNode right; public TreeNode(Object initValue) { value = initValue; left = null; right = null; } public TreeNode(Object initValue, TreeNode initLeft, TreeNode initRight) { value = initValue; left =initLeft; right = initRight; }
61 TreeNode…. public Object getValue() { return value; } public TreeNode getLeft() { return left; } public TreeNode getRight() { return right; }
62 TreeNode…. public void setValue(Object theNewValue) { value = theNewValue; } public void setLeft(TreeNode theNewLeft) { left = theNewLeft; } public void setRight(TreeNode theNewRight) { right = theNewRight; } }
63 Implementing Binary Search Tree Class common behaviors (insertions, deletions, traversals, iterators) isEmpty() insert print (inorder traversal) search
64 TreeSet The TreeSet class uses a form of balanced binary trees that guarantees that adding and removing an element takes O(log n) time. We know that a good hashing function can give us a retrieval with efficiency O(1) but if we also wanted to be able to list our data in order, a hash table would not be the appropriate choice. The TreeSet class implements the Set interface.
65 interface java.util.Set MethodMethod Summary boolean add(Object obj) Adds the parameter obj as an element to this set if it is not already in the set and returns true. If the element is already in the set, returns false. boolean contains(Object obj) Returns true if this set contains the element obj, otherwise returns false. boolean remove(Object obj) Removes the element obj from this set and returns true if the obj is in this set; if not present returns false. int size() Returns the number of elements in this set. Iterator iterator() Returns an Iterator that provides access to the elements of this set.
66 TreeSet… A TreeSet requires that its elements be comparable. compareTo is defined for the objects placed in the TreeSet. the elements in a tree are ordered For your own classes, realize the Comparable interface define compareTo or provide a Comparator ( Comparator is not in AP Subset) Since a TreeSet implements Set, a TreeSet contains no duplicates. A TreeSet guarantees reasonable search performance (O(log n)) and allows for visiting the elements in order.
67 Nice practice with iterators. The class TreeSetWithOps has all functionality of TreeSet and also includes the methods setIntersection, setUnion, setDifference, isSubset, and isProperSubset. Implement TreeSetWithOps.
68 Maps A map is a data type that keeps associations between keys and values. Each key in a map has a unique value. But a value may be associated with more than one key. A mapping of your college friends to the university that they attend. ("Owen" maps to "Duke University", "Fran" maps to "Drew University"). A mapping of login ids to passwords. Each association has a key mapped to a value. Associations can be put in, changed, and located in a map.
69 TreeMap MethodMethod Summary boolean put(Object key, Object value) Associates value with key in this map so that get(key ) returns value. boolean get(Object key) Return the value associated with key in this map, or null if there is no value associated with key. boolean containsKey(Object key) Returns true if there is a value associated with key in this map, otherwise returns false. int size() Returns the number of keys in this map. Set keySet() Returns a set of the keys in the maps. Object remove(Object key) Removes the mapping for this key from the map and returns the value previously associated with the key in the map.
70 Map The Map interface requires that the keySet method be implemented. The keySet method produces a Set of keys. We can visit all of the elements of a TreeMap by iterating through the keys in the set that the keyset method produces. The Map method get will return the value associated with a map key. A TreeMap keeps the elements in an order (according to the key) from smallest to largest.
71 Multiple Choice Sample Question: AP Course Description The following integers are inserted into an empty binary search tree in the following order Which traversal of the tree would produce the following output? (A) Preorder (B) Inorder (C) Postorder (D) Reverse postorder (E) Level-by-level