04 trees part i cs 310 photo ©oregon scenics used with permissionoregon scenics all figures labeled...

68
04 Trees Part I CS 310 photo ©Oregon Scenics used with permission All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

Post on 15-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

04 TreesPart I

CS 310

photo ©Oregon Scenics used with permission

All figures labeled with “Figure X.Y”

Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

Page 2: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

2

Trees

• A tree is either empty or consists of a root and zero or more nonempty subtrees.

root

othersubtrees

...

othersubtrees

othersubtrees

Page 3: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

3

Tree anatomy

root

B

G HE F I

C D

KJ

Node – where information is stored

– directed edges join nodes

Page 4: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

4

Tree anatomy

root

B

G HE F I

C D

KJ Parent/child relationships

parent

child

ance

stor

s

desc

ende

nts

Page 5: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

5

Tree anatomy

root

B

G HE F I

C D

KJ

ance

stor

s

desc

ende

nts

A node is an ancestor/descendent of itself, but not a proper ancestor/

descendent

Page 6: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

6

Tree anatomy

root

B

G HE F I

C D

KJ

Nodes with the same parents are called siblings.

Page 7: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

7

Tree anatomy

root

B

G HE F I

C D

KJPath – a unique set of edges from

one node to another

Rootà Bà Eà K is a path of length 3

Page 8: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

8

Tree anatomy

root

B D

dept

h

0

1

2

3

height(B)=2

height(D)=0

Page 9: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

9

First child/next sibling implementation

root

B

G HE F I

C D

KJ

NODE

Child Pointer

Sibling Pointer

Page 10: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

10

Preorder tree traversal

visit tree(node) {if (node == null)

returnelse {

print node for (c in children)

visit tree(c)}

}

root

B

G HE F I

C D

KJ

root B E J K F G C H I D

What’s the complexity?

Page 11: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

11

Postorder tree traversal

visit tree(node) {if (node == null)

returnelse {

for (c in children)visit tree(c)

}print node

}

root

B

G HE F I

C D

KJ

J K E F … ?

Why would we care whether we usepreorder or postorder traversal?

Page 12: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

12

Recursion

• Recursive methods make calls to themselves– directly– indirectly

• Why bother with recursion?– Frequently leads to elegant code– Language’s runtime system does much of the

work for us (we’ll see this later)

Page 13: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

13

An example

n = 273base = 10

273/10 = 27

top of stack

printInt(273, 10) Each time we call a subroutine,an activation record is pushed onto the stack.

Page 14: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

14

An example

n = 273base = 10

27/10 = 2

top of stack

printInt(273, 10)

n = 27base = 10

printInt(27, 10)

Page 15: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

15

An example

n = 273base = 10

2 < 10 no recursion, 2 % 10 = 2

top of stack

printInt(273, 10)

n = 27base = 10

printInt(27, 10)

n = 2base = 10

printInt(2, 10)

Output: “2”

Page 16: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

16

An example

n = 273base = 10

top of stack

printInt(273, 10)

n = 27base = 10

printInt(27, 10)

27 % 10 = 7 Output: “27”

n = 273base = 10

top of stack

printInt(273, 10)

273 % 10 = 3 Output: “273”

How could we have implemented this iteratively?

Page 17: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

17

How to prove by induction

• Start by proving a basis– Show it for the easy case first

• Make an inductive hypothesis– Assume that what you are trying to prove

holds true for an arbitrary base case– Consider what happens for the next case– Typically, we show that if the base case holds,

than the next one must hold as well.

Page 18: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

18

Example, Theorem 7.2

2221 2

1 2

)2()1( )1( Note

2

)1()1( Prove

NNNi

NNi

Ni

iN

Ni

iN

Page 19: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

19

Basis

basis for the trueis thisso

12

)11(1

2

)1(

as same theis but this

1

1)1(

)1(

1NLet

211

1

1

21

NN

ii

i

Page 20: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

20

Inductive hypothesis

1kkfor trueisit that show tohave weNow

2

)1()1(

k somefor holds theorem that theAssume

1 2

kki

ki

ik

Page 21: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

21

Induction step

2221 2

22221

1

21

1

1

2)1(

)2()1()( )1(

k.for expect would what welikelook thisofpart make try to weNow

)2()1()()1( )1(

sum... theexpandingby start usLet

2

)2)(1(

2

)1)1)((1()1(

show

kkki

kkkki

kkkki

ki

ik

ki

ik

ki

ik

Page 22: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

22

Induction step

2

)2)(1(2

23

2

242

2

)1()1(

)2()1()()1(

)2()1()()1( )1(

2

22

2

2222

almost...

22221

1

21

kk

kk

kkkk

kkk

kkkk

kkkkiki

ik

Page 23: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

23

Induction step

1 allfor trueis that thisinduction by shown have we

,2

)2)(1( )1( As

1

1

21

N

kki

ki

ik

Page 24: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

24

Your turn…

induction.by 2

)1(21 Prove

1

NNNi

N

i

Page 25: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

25

binary trees

• A binary tree is a tree that has exactly 0, 1, or 2 subtrees.

• We name the subtrees the left and right subtrees.

• One application of binary trees: expression trees

Page 26: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

26

operations on a binary tree

• traversals (may be implemented by iterators)

• size() – Number of nodes in tree

• height() – Height of tree

• isEmpty() – Any nodes in tree?

• makeEmpty() – remove all nodes

• merge(root, leftSubtree, rightSubtree)

Page 27: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

27

Implementation decisions

• Where should we put the functionality?– tree object?– node object?

• Should the node be a nested class?

Page 28: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

28

BinaryTreeskeleton

Page 29: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

29

BinaryNodeskeleton

Page 30: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

30

Merging trees

• Desired semantics of

t.merge(item, leftSubTree, rightSubtree):

Page 31: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

31

Naïve merge

• We might be tempted to write merge:

public void merge(T newRoot, BinaryTree<T> left, BinaryTree<T> right) {

// Set the root node to a new node, effectively deleting whatever

// we had before. Make left & right the children.

root = new BinaryNode<T>(newRoot, left.root, right.root);

}

Note: T substituted for AnyType due to space constraints.

Page 32: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

32

Trouble in paradise…

• t.merge(x, t1, t2)• Suppose we had an alias to either t1 or t2 before the

merge, what happens if we modify these now?

break binding

oldtree

Page 33: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

33

Is this any better?

public void merge(T newRoot, BinaryTree<T> left, BinaryTree<R> right) {// Set the root node to a new node, effectively deleting whatever// we had before. Make left & right the children.root = new BinaryNode<T>(newRoot, left.root, right.root);// avoid aliasing by making the merge destructiveleft.root = null;right.root = null;

}Consider: t1.merge(x, t1, t2)

Page 34: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

34

Another case to consider

• What about: t1.merge(x, t3, t3);

• Should we allow this?

• What are our options for allowing/disallowing?

Page 35: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

35

Page 36: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

36

Recursion with trees

Note that here we do this with a methodwhich operates on an object.

Page 37: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

37An example of doing this with a static method.Note how we terminate the recursion.

Page 38: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

38

Page 39: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

39

pre/in/post-order traversal

Page 40: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

40

Iterators

• Your text shows that we can create iterators by using a stack 18.4.1 – 18.4.3.

• Read these on your own. The key concept is that in some traversals we need to visit an item multiple times.

Page 41: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

41

Iterators

• As an example, the postorder iterator has to visit a node three times:

1. before visiting the left subtree

2. before visiting the right subtree

3. ready to process the current node

• When written recursively, this is simple

• When written iteratively, we have to do extra bookkeeping.

Page 42: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

42

Iterators

• Your text solves the problem by pusing a class onto the stack that remembers both the node and the number of times it has been visited.

• By looking at the number of times it has been visited, we can determine which state we are in.

Page 43: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

43

Level order traversal

• This is a little bit trickier… root

B

G HE F I

C D

KJ

desired output:root, B, C, D, E, F, G, H, I, J, KHow do we do this?

Page 44: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

44

Page 45: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

45

Page 46: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

46

Page 47: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

47

binary search trees

• Up to now, tree access O(N)

• When N large, too expensive

• Binary search trees– average case: O(log N)– worst case: O(N)

• Later, we will see ways to eliminate the linear worst case

Page 48: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

48

Ordering property

• Binary trees must select a key (value) on which to order.

• Binary search tree order propertyFor any node n in the tree

All nodes l in the left subtree have key(l)<key(n)

All nodes r in the right subtree have key(r)>key(n)

• Duplicates? Keep a count or disallow.

Page 49: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

49

Operations

• find – easy

• insert – pretty easy

• remove – more difficult

• Other operations– findMin, findMax– isEmpty, makeEmpty

Page 50: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

50

Page 51: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

51

Page 52: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

52

Page 53: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

53

Page 54: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

54

insertion

• into empty tree special case

• into nonempty tree– determine which subtree– insert into appropriate subtree

Page 55: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

55

insert

Page 56: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

56

remove

# children

0 – easy

1 – move child up

2 – replace node with smallest child in right subtree (left most node in right subtree)

Page 57: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

57

finding the smallest child

removeMin(t.right)t points here

Page 58: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

58

Page 59: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

59

Complexity analysis

• insertion & find– worst case: What do you think?– average case:

We need to search to the leaves of the tree.

If we know the average path length, we can figure out how many nodes we can have a measure of this.

Recall: path length is the number of edges between the root and a node.

Page 60: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

60

Path length

• Internal path length: Sum of the depths of a tree’s nodes

Page 61: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

61

Path length

• External path length: Sum of the depths of the null links (treating them as external nodes)

• Thm 19.2: The external path length for any tree is the internal path length + the number of external nodes.

• Thm: Any tree with N nodes has N+1 external links.

Page 62: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

62

A successful find

• When we find a node, we have traveled down one of the paths of the tree.

• So, the average case– travels the length of an average path– plus one additional node to account for the node itself.

• If we can determine the internal path length, we can divide by the number of paths (one for each node) to get the average path length.

Page 63: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

63

Page 64: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

64

Page 65: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

65

Page 66: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

66

Average successful find

• Since the average internal path length is of order O(N log N) and there are N paths, each path is on average of length

• A sucessful find will thus visit log(N) + 1 nodes and is of order O(log N).

)(loglog

NON

NNO

Page 67: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

67

Average unsuccessful find or insertion

• When we are unable to find a node or want to insert, we will eventually visit one of the N+1 null links.

• The average number of links we visit is:

• so this is also O(N log N)

nodes externalnumber

lengthpath external

Page 68: 04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley

68

Average case insertion/find

• We assumed that the tree was built with a randomly ordered sequence.

• Given our strategy of always deleting the leftmost child in the right subtree, will deletions cause skew in the tree?– In practice this is not too bad.– However, we will learn later about methods to

keep the tree balanced.