trees, trees, and more trees

71
1 Trees, Trees, and More Trees

Upload: limei

Post on 20-Jan-2016

62 views

Category:

Documents


0 download

DESCRIPTION

Trees, Trees, and More Trees. Trees, Trees, and More Trees. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Trees, Trees, and More Trees

1

Trees, Trees, and More Trees

Page 2: Trees, Trees, and More Trees

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

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

4

H

D

A G

E

P

L W

R

rootRight subtree

rooted at PLeft subtree

Page 5: Trees, Trees, and More Trees

5

H

D

A G

E

P

L W

R

root level 0

level 1

level 2

level 3

Right subtreeLeft subtree

Page 6: Trees, Trees, and More Trees

6

Full Binary Tree of Level n

Page 7: Trees, Trees, and More Trees

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

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

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

10

A

B

F

C

ED

JIH K L M

G

N O

Height of binary tree ?

Page 11: Trees, Trees, and More Trees

11

A

B

F

C

ED

JIH K L M

G

N O

Height of binary tree ?

Height = 4

Page 12: Trees, Trees, and More Trees

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

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

14

To insert to a binary search tree:

if value less than

go left

else

go right

Page 15: Trees, Trees, and More Trees

15

15

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

Page 16: Trees, Trees, and More Trees

16

15

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

8

Page 17: Trees, Trees, and More Trees

17

15

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

8 25

Page 18: Trees, Trees, and More Trees

18

15

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

8 25

6

Page 19: Trees, Trees, and More Trees

19

15

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

8 25

614

Page 20: Trees, Trees, and More Trees

20

15

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

8 25

614 24

Page 21: Trees, Trees, and More Trees

21

15

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

8 25

614 24

20

Page 22: Trees, Trees, and More Trees

22

15

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

8 25

614 24

20

22

Page 23: Trees, Trees, and More Trees

23

15

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

8 25

614 24

20

22

30

Page 24: Trees, Trees, and More Trees

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

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

26

Tree Traversals

See also: animations on web site

Page 27: Trees, Trees, and More Trees

27

A

B

F

C

ED

JIH K L M

G

N O

Inorder traversal of a binary treeleft…root…right

Page 28: Trees, Trees, and More Trees

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

29

A

B

F

C

ED

JIH K L M

G

N O

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

Page 30: Trees, Trees, and More Trees

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

31

A

B

F

C

ED

JIH K L M

G

N O

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

Page 32: Trees, Trees, and More Trees

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

33

L

B

F

W

EX

A Q P

G

T E

Inorder traversal ?

Page 34: Trees, Trees, and More Trees

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

35

L

B

F

W

EX

A Q P

G

T E

Preorder traversal ?

Page 36: Trees, Trees, and More Trees

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

37

L

B

F

W

EX

A Q P

G

T E

Postorder traversal ?

Page 38: Trees, Trees, and More Trees

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

39

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

A

B C

D E F G

H I

Page 40: Trees, Trees, and More Trees

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

41

15

Inorder Traversal ?

8 25

6 14 24

20

22

30

2613

Page 42: Trees, Trees, and More Trees

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

43

15

Preorder Traversal ?

8 25

6 14 24

20

22

30

2613

Page 44: Trees, Trees, and More Trees

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

45

15

Postorder Traversal ?

8 25

6 14 24

20

22

30

2613

Page 46: Trees, Trees, and More Trees

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

47

15

Our Tree:

8 25

6 14 24

20

22

30

2613

What is the height of the tree?

Page 48: Trees, Trees, and More Trees

48

15

Height :

8 25

6 14 24

20

22

30

2613

Height = 5

Page 49: Trees, Trees, and More Trees

49

Deleting a node from a binary tree

L

D

HC

A F J

P

Page 50: Trees, Trees, and More Trees

50

To delete a leaf...

L

D

HC

A F J

P

Page 51: Trees, Trees, and More Trees

51

To delete a leaf...

L

D

HC

A F J

P

Set appropriate

parent to NULL

Page 52: Trees, Trees, and More Trees

52

To delete a leaf...

L

D

HC

A F

P

Page 53: Trees, Trees, and More Trees

53

Deleting a node with one child...

L

D

HC

A F J

P

Page 54: Trees, Trees, and More Trees

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

55

Deleting a node with one child...

L

D

HA

F J

P

Page 56: Trees, Trees, and More Trees

56

Deleting a node with 2 children...

L

D

HC

A F J

P

Page 57: Trees, Trees, and More Trees

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

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

59

Binary Search Trees

AP Implementation

Page 60: Trees, Trees, and More Trees

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

61

TreeNode….

public Object getValue()

{ return value; }

public TreeNode getLeft()

{ return left; }

public TreeNode getRight()

{ return right; }

Page 62: Trees, Trees, and More Trees

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

63

Implementing Binary Search Tree Class

common behaviors (insertions, deletions, traversals, iterators)

isEmpty() insert print (inorder traversal) search

Page 64: Trees, Trees, and More Trees

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

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

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

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

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

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

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

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