Trees and More Trees 1 Trees and More

  • Slides: 71
Download presentation
Trees, and More Trees 1

Trees, and More Trees 1

Trees, and More Trees By looking at forests of terms, awesome animations, and complete

Trees, and More Trees 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. 2

TREES A binary tree is a set of elements that is either empty or

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

H Left subtree root Right subtree rooted at P P D G A E

H Left subtree root Right subtree rooted at P P D G A E L W R 4

H root level 0 Left subtree P D level 1 level 2 Right subtree

H root level 0 Left subtree P D level 1 level 2 Right subtree G A level 3 E L W R 5

Full Binary Tree of Level n 6

Full Binary Tree of Level n 6

A is the root of the tree. A is the parent of B and

A is the root of the tree. A is the parent of B and C. A is an ancestor of all nodes. A B C D H E I J F K L G M N O 7

B and C are siblings. J is a descendent of B. A B C

B and C are siblings. J is a descendent of B. A B C D H E I J F K L G M N O 8

Height of binary tree : n Number of nodes on the longest path from

Height of binary tree : n Number of nodes on the longest path from the root to a leaf. n The height of the empty tree is 0. n The height of a single node tree is 1. n Note: (not an AP term) the definition of “height” is not from some “cs bible” – some will define other ways 9

Height of binary tree ? A B C D H E I J F

Height of binary tree ? A B C D H E I J F K L G M N O 10

Height of binary tree ? A B C D H E I J F

Height of binary tree ? A B C D H E I J F K L G M N O Height = 4 11

Full Binary : A recursive definition A binary tree is full if the tree

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 12

Binary Search Tree n A binary search tree : n n n every node

Binary Search Tree n A binary search tree : n n n 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. 13

To insert to a binary search tree: if value less than go left else

To insert to a binary search tree: if value less than go left else go right 14

Insert : 15 8 25 6 14 24 20 22 30 13 26 15

Insert : 15 8 25 6 14 24 20 22 30 13 26 15 15

Insert : 14 8 25 6 14 24 20 22 30 13 26 15

Insert : 14 8 25 6 14 24 20 22 30 13 26 15 8 16

Insert : 14 8 25 6 14 24 20 22 30 13 26 15

Insert : 14 8 25 6 14 24 20 22 30 13 26 15 8 25 17

Insert : 15 8 25 6 14 24 20 22 30 13 26 15

Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 6 18

Insert : 15 8 25 6 14 24 20 22 30 13 26 15

Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 6 25 14 19

Insert : 15 8 25 6 14 24 20 22 30 13 26 15

Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 6 25 14 24 20

Insert : 15 8 25 6 14 24 20 22 30 13 26 15

Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 6 25 24 14 20 21

Insert : 15 8 25 6 14 24 20 22 30 13 26 15

Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 6 25 24 14 20 22 22

Insert : 15 8 25 6 14 24 20 22 30 13 26 15

Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 6 25 24 14 30 20 22 23

Insert : 15 8 25 6 14 24 20 22 30 13 26 15

Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 6 25 24 14 13 30 20 22 24

Insert : 15 8 25 6 14 24 20 22 30 13 26 15

Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 6 25 24 14 13 30 26 20 22 25

Tree Traversals See also: animations on web site 26

Tree Traversals See also: animations on web site 26

Inorder traversal of a binary tree left…root…right A B C D H E I

Inorder traversal of a binary tree left…root…right A B C D H E I J F K L G M N O 27

Inorder traversal of a binary tree left…root…right A B C D H E I

Inorder traversal of a binary tree left…root…right A B C D H E I J F K L G M N O HDIBJEKALFMCNGO 28

Preorder traversal of a binary tree: root…left…right A B C D H E I

Preorder traversal of a binary tree: root…left…right A B C D H E I J F K L G M N O 29

Preorder traversal of a binary tree: root…left…right A B C D H E I

Preorder traversal of a binary tree: root…left…right A B C D H E I J F K L G M N O A B D H I E J KC F L M G N O 30

Postorder traversal of a binary tree: left…right…ROOT A B C D H E I

Postorder traversal of a binary tree: left…right…ROOT A B C D H E I J F K L G M N O 31

Postorder traversal of a binary tree: left…right…ROOT A B C D H E I

Postorder traversal of a binary tree: left…right…ROOT A B C D H E I J F K L G M N O HIDJKEBLMFNOGCA 32

Inorder traversal ? L B X W E A F Q P G T

Inorder traversal ? L B X W E A F Q P G T E 33

Inorder traversal : L B X W E A F Q P G T

Inorder traversal : L B X W E A F Q P G T E XA B E Q L P F W T G E 34

Preorder traversal ? L B X W E A F Q P G T

Preorder traversal ? L B X W E A F Q P G T E 35

Preorder traversal: L B X W E A F Q P G T E

Preorder traversal: L B X W E A F Q P G T E L B X A E Q W F PG T E 36

Postorder traversal ? L B X W E A F Q P G T

Postorder traversal ? L B X W E A F Q P G T E 37

Postorder traversal : L B X W E A F Q P G T

Postorder traversal : L B X W E A F Q P G T E AXQEBPFTEGWL 38

breadth-first-order tree traversal n ROW (or level) order traversal A B D C E

breadth-first-order tree traversal n ROW (or level) order traversal A B D C E F G H I 39

breadth-first-order tree traversal n ROW (or level) order traversal ABCDEFGHI A B D C

breadth-first-order tree traversal n ROW (or level) order traversal ABCDEFGHI A B D C E F G H I 40

Inorder Traversal ? 15 8 6 25 24 14 13 30 26 20 22

Inorder Traversal ? 15 8 6 25 24 14 13 30 26 20 22 41

Inorder Traversal : 15 8 6 25 24 14 13 30 26 20 22

Inorder Traversal : 15 8 6 25 24 14 13 30 26 20 22 6 8 13 14 15 20 22 24 25 26 30 42

Preorder Traversal ? 15 8 6 25 24 14 13 30 26 20 22

Preorder Traversal ? 15 8 6 25 24 14 13 30 26 20 22 43

Preorder Traversal : 15 8 6 25 24 14 13 30 26 20 22

Preorder Traversal : 15 8 6 25 24 14 13 30 26 20 22 15 8 6 14 13 25 24 20 22 30 26 44

Postorder Traversal ? 15 8 6 25 24 14 13 30 26 20 22

Postorder Traversal ? 15 8 6 25 24 14 13 30 26 20 22 45

Postorder Traversal : 15 8 6 25 24 14 13 30 26 20 22

Postorder Traversal : 15 8 6 25 24 14 13 30 26 20 22 6 13 14 8 22 20 24 26 30 25 15 46

Our Tree: 15 8 6 25 24 14 13 30 26 20 22 What

Our Tree: 15 8 6 25 24 14 13 30 26 20 22 What is the height of the tree? 47

Height : 15 8 6 25 24 14 13 30 26 20 22 Height

Height : 15 8 6 25 24 14 13 30 26 20 22 Height = 5 48

Deleting a node from a binary tree L D H C A P F

Deleting a node from a binary tree L D H C A P F J 49

To delete a leaf. . . L D H C A P F J

To delete a leaf. . . L D H C A P F J 50

To delete a leaf. . . L D H C A P F J

To delete a leaf. . . L D H C A P F J Set appropriate parent to NULL 51

To delete a leaf. . . L D H C A P F 52

To delete a leaf. . . L D H C A P F 52

Deleting a node with one child. . . L D H C A P

Deleting a node with one child. . . L D H C A P F J 53

Deleting a node with one child. . . L D H C A P

Deleting a node with one child. . . L D H C A P F J Make appropriate left or right reference of parent skip over the deleted node and reference the child of the node we want to delete 54

Deleting a node with one child. . . L D P H A F

Deleting a node with one child. . . L D P H A F J 55

Deleting a node with 2 children. . . L D H C A P

Deleting a node with 2 children. . . L D H C A P F J 56

Deleting a node with 2 children. . . L D Go once left and

Deleting a node with 2 children. . . L D Go once left and as far right as you can for node’s replacement. H C A P F J 57

Deleting a node with 2 children. . . J D H C A P

Deleting a node with 2 children. . . J D H C A P Go once left and as far right as you can. F 58

Binary Search Trees AP Implementation 59

Binary Search Trees AP Implementation 59

public class Tree. Node { private Object value; private Tree. Node left; private Tree.

public class Tree. Node { private Object value; private Tree. Node left; private Tree. Node right; public Tree. Node(Object init. Value) { value = init. Value; left = null; right = null; } public Tree. Node(Object init. Value, Tree. Node init. Left, Tree. Node init. Right) { value = init. Value; left = init. Left; right = init. Right; } 60

Tree. Node…. public Object get. Value() { return value; } public Tree. Node get.

Tree. Node…. public Object get. Value() { return value; } public Tree. Node get. Left() { return left; } public Tree. Node get. Right() { return right; } 61

Tree. Node…. public void set. Value(Object the. New. Value) { value = the. New.

Tree. Node…. public void set. Value(Object the. New. Value) { value = the. New. Value; } public void set. Left(Tree. Node the. New. Left) { left = the. New. Left; } public void set. Right(Tree. Node the. New. Right) { right = the. New. Right; } } 62

Implementing Binary Search Tree Class n common behaviors n (insertions, deletions, traversals, iterators) is.

Implementing Binary Search Tree Class n common behaviors n (insertions, deletions, traversals, iterators) is. Empty() n insert n print (inorder traversal) n search n 63

Tree. Set n The Tree. Set class uses a form of balanced binary trees

Tree. Set n The Tree. Set class uses a form of balanced binary trees that guarantees that adding and removing an element takes O(log n) time. n 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. n The Tree. Set class implements the Set interface. 64

interface java. util. Set Method Summary boolean add(Object obj) Adds the parameter obj as

interface java. util. Set Method 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. 65

Tree. Set… n A Tree. Set requires that its elements be comparable. n compare.

Tree. Set… n A Tree. Set requires that its elements be comparable. n compare. To is defined for the objects placed in the Tree. Set. n n the elements in a tree are ordered For your own classes, realize the Comparable interface n define compare. To or provide a Comparator (Comparator is not in AP Subset) n n Since a Tree. Set implements Set, a Tree. Set contains no duplicates. n A Tree. Set guarantees reasonable search performance (O(log n)) and allows for visiting the elements in order. 66

Nice practice with iterators. n The class Tree. Set. With. Ops has all functionality

Nice practice with iterators. n The class Tree. Set. With. Ops has all functionality of Tree. Set and also includes the methods set. Intersection, set. Union, set. Difference, is. Subset, and is. Proper. Subset. Implement Tree. Set. With. Ops. 67

Maps n A map is a data type that keeps associations between keys and

Maps n 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. n n 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. n Each association has a key mapped to a value. n Associations can be put in, changed, and located in a map. 68

Tree. Map Method Summary boolean put(Object key, Object value) Associates value with key in

Tree. Map Method 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 contains. Key(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 key. Set() 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 69 the map.

Map n The Map interface requires that the key. Set method n n be

Map n The Map interface requires that the key. Set method n n be implemented. The key. Set method produces a Set of keys. We can visit all of the elements of a Tree. Map 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 Tree. Map keeps the elements in an order (according to the key) from smallest to largest. 70

Multiple Choice Sample Question: AP Course Description The following integers are inserted into an

Multiple Choice Sample Question: AP Course Description The following integers are inserted into an empty binary search tree in the following order. 26 20 37 31 22 18 25 29 19 Which traversal of the tree would produce the following output? 26 20 37 18 22 31 19 25 29 (A) Preorder (B) Inorder (C) Postorder (D) Reverse postorder (E) Level-by-level 71