71
1 Trees, Trees, and More Trees

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,

Embed Size (px)

Citation preview

Page 1: 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,

1

Trees, Trees, and More Trees

Page 2: 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,

2

Trees, 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.

Page 3: 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,

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.

Page 4: 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,

4

H

D

A G

E

P

L W

R

rootRight subtree

rooted at PLeft subtree

Page 5: 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,

5

H

D

A G

E

P

L W

R

root level 0

level 1

level 2

level 3

Right subtreeLeft subtree

Page 6: 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,

6

Full Binary Tree of Level n

Page 7: 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,

7

A

B

F

C

ED

JIH K L M

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.

Page 8: 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,

8

A

B

F

C

ED

JIH K L M

G

N O

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

Page 9: 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,

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

Page 10: 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,

10

A

B

F

C

ED

JIH K L M

G

N O

Height of binary tree ?

Page 11: 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,

11

A

B

F

C

ED

JIH K L M

G

N O

Height of binary tree ?

Height = 4

Page 12: 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,

12

Full Binary : A recursive definition

A binary tree is full if the tree is emptyor if the tree's left and right subtrees have the same height and both are full

Page 13: 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,

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.

Page 14: 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,

14

To insert to a binary search tree:

if value less than

go left

else

go right

Page 15: 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,

15

15

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

Page 16: 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,

16

15

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

8

Page 17: 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,

17

15

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

8 25

Page 18: 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,

18

15

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

8 25

6

Page 19: 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,

19

15

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

8 25

614

Page 20: 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,

20

15

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

8 25

614 24

Page 21: 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,

21

15

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

8 25

614 24

20

Page 22: 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,

22

15

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

8 25

614 24

20

22

Page 23: 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,

23

15

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

8 25

614 24

20

22

30

Page 24: 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,

24

15

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

8 25

6 14 24

20

22

30

13

Page 25: 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,

25

15

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

8 25

6 14 24

20

22

30

2613

Page 26: 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,

26

Tree Traversals

See also: animations on web site

Page 27: 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,

27

A

B

F

C

ED

JIH K L M

G

N O

Inorder traversal of a binary treeleft…root…right

Page 28: 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,

28

A

B

F

C

ED

JIH K L M

G

N O

Inorder traversal of a binary treeleft…root…right

H D I B J E K A L F M C N G O

Page 29: 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,

29

A

B

F

C

ED

JIH K L M

G

N O

Preorder traversal of a binary tree:root…left…right

Page 30: 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,

30

A

B

F

C

ED

JIH K L M

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

Page 31: 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,

31

A

B

F

C

ED

JIH K L M

G

N O

Postorder traversal of a binary tree:left…right…ROOT

Page 32: 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,

32

A

B

F

C

ED

JIH K L M

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

Page 33: 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,

33

L

B

F

W

EX

A Q P

G

T E

Inorder traversal ?

Page 34: 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,

34

L

B

F

W

EX

A Q P

G

T E

Inorder traversal :

XA B E Q L P F W T G E

Page 35: 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,

35

L

B

F

W

EX

A Q P

G

T E

Preorder traversal ?

Page 36: 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,

36

L

B

F

W

EX

A Q P

G

T E

Preorder traversal:

L B X A E Q W F PG T E

Page 37: 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,

37

L

B

F

W

EX

A Q P

G

T E

Postorder traversal ?

Page 38: 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,

38

L

B

F

W

EX

A Q P

G

T E

Postorder traversal :

A X Q E B P F T E G W L

Page 39: 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,

39

breadth-first-order tree traversal ROW (or level) order traversal

A

B C

D E F G

H I

Page 40: 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,

40

breadth-first-order tree traversal ROW (or level) order traversal

A

B C

D E F G

H I

A B C D E F G H I

Page 41: 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,

41

15

Inorder Traversal ?

8 25

6 14 24

20

22

30

2613

Page 42: 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,

42

15

Inorder Traversal :

8 25

6 14 24

20

22

30

2613

6 8 13 14 15 20 22 24 25 26 30

Page 43: 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,

43

15

Preorder Traversal ?

8 25

6 14 24

20

22

30

2613

Page 44: 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,

44

15

Preorder Traversal :

8 25

6 14 24

20

22

30

2613

15 8 6 14 13 25 24 20 22 30 26

Page 45: 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,

45

15

Postorder Traversal ?

8 25

6 14 24

20

22

30

2613

Page 46: 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,

46

15

Postorder Traversal :

8 25

6 14 24

20

22

30

2613

6 13 14 8 22 20 24 26 30 25 15

Page 47: 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,

47

15

Our Tree:

8 25

6 14 24

20

22

30

2613

What is the height of the tree?

Page 48: 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,

48

15

Height :

8 25

6 14 24

20

22

30

2613

Height = 5

Page 49: 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,

49

Deleting a node from a binary tree

L

D

HC

A F J

P

Page 50: 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,

50

To delete a leaf...

L

D

HC

A F J

P

Page 51: 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,

51

To delete a leaf...

L

D

HC

A F J

P

Set appropriate

parent to NULL

Page 52: 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,

52

To delete a leaf...

L

D

HC

A F

P

Page 53: 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,

53

Deleting a node with one child...

L

D

HC

A F J

P

Page 54: 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,

54

Deleting a node with one child...

L

D

HC

A F J

PMake appropriate

left or right reference of parent

skip over the deleted node and reference

the child of the node we want to delete

Page 55: 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,

55

Deleting a node with one child...

L

D

HA

F J

P

Page 56: 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,

56

Deleting a node with 2 children...

L

D

HC

A F J

P

Page 57: 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,

57

Deleting a node with 2 children...

L

D

HC

A F J

P

Go once left and as far right as you can for node’s replacement.

Page 58: 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,

58

Deleting a node with 2 children...

D

HC

A F

J

P

Go once left and as far right as you can.

Page 59: 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,

59

Binary Search Trees

AP Implementation

Page 60: 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,

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;

}

Page 61: 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,

61

TreeNode….

public Object getValue()

{ return value; }

public TreeNode getLeft()

{ return left; }

public TreeNode getRight()

{ return right; }

Page 62: 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,

62

TreeNode….

public void setValue(Object theNewValue){ value = theNewValue; }

public void setLeft(TreeNode theNewLeft){ left = theNewLeft; }

public void setRight(TreeNode theNewRight){ right = theNewRight; }

}

Page 63: 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,

63

Implementing Binary Search Tree Class

common behaviors (insertions, deletions, traversals, iterators)

isEmpty() insert print (inorder traversal) search

Page 64: 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,

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.

Page 65: 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,

65

interface java.util.Set

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

Page 66: 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,

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.

Page 67: 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,

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.

Page 68: 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,

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.

Page 69: 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,

69

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

Page 70: 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,

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.

Page 71: 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,

71

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