Lecture 9: Intro to Trees CSE 373: Data Structures and ...

35
Lecture 9: Intro to Trees CSE 373: Data Structures and Algorithms CSE 373 19 SP - KASEY CHAMPION 1

Transcript of Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Page 1: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Lecture 9: Intro to Trees CSE 373: Data Structures and Algorithms

CSE 373 19 SP - KASEY CHAMPION 1

Page 2: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Warm Up1. Write a recurrence for this piece of code (assume each node has exactly or 2 children)

private IntTreeNode doublePositivesHelper(IntTreeNode node) {

if (node != null) {

if (node.data > 0) {

node.data *= 2;

}

node.left = doublePositivesHelper(node.left);

node.right = doublePositivesHelper(node.right);

return node;

} else {

return null;

}

}

CSE 373 SP 19 - KASEY CHAMPION 2

Extra Credit:Go to PollEv.com/champkText CHAMPK to 22333 to join session, text “1” or “2” to select your answer

+ C

+ C

T(n/2)

T(n/2)

! " = $%& 'ℎ)" " < 1

%, + 2! "2 /0ℎ)1'23)

+ C

Page 3: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Level (i) Number of Nodes Input to Node Work per Node

0

1

2

3

base

Warm Up Continued

CSE 373 19 SP - KASEY CHAMPION 3

! " = $%& 'ℎ)" " < 1

%, + 2! "2 /0ℎ)1'23)

! "! 4, + ! 4

, + %,

! "2 ! "

2

Input=n'/1; = %,

!"22 + !

"22 + %2 !

"22 + !

"22 + %2

input=n/2'/1; = %2

input = n/2'/1; = %2

… … … … … … … …%1 %1 %1 %1 %1 %1 %1 %1 %1 %1 %1 %1 %1 %1 %1 %1

Page 4: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Warm Up Continued

CSE 373 19 SP - KASEY CHAMPION 4

! " = $%& 'ℎ)" " < 1

%, + 2! "2 /0ℎ)1'23)

Level (i) Number of Nodes Input to Node Work per Node

0 24=1"24 = " 52

1 2&=2"2& =

"2 52

2 2,=4"2, =

"4 52

3 27=8"27 =

"8 52

base ⇒ 2:;<,=+1 0 %1

1. How many nodes on each branch level?

2. How much work for each branch node?

3. How much work per branch level?

4. How many branch levels?

5. How much work for each leaf node?

6. How many leaf nodes?

2>

52

"2> < 1 ⇒ i = log, "

%1

Combining it all together…

2>%2

2:;<C =D& = 2"! " = E

>F4

:;<C =2>%, + 2"%1

Page 5: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Trees

CSE 373 SP 18 - KASEY CHAMPION 5

Page 6: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Storing Sorted Items in an Arrayget() – O(logn)

put() – O(n)

remove() – O(n)

Can we do better with insertions and removals?

CSE 373 SP 18 - KASEY CHAMPION 6

Page 7: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Review: Trees!A tree is a collection of nodes- Each node has at most 1 parent and 0 or more children

Root node: the single node with no parent, “top” of the tree

Branch node: a node with one or more children

Leaf node: a node with no children

Edge: a pointer from one node to another

Subtree: a node and all it descendants

Height: the number of edges contained in the longest path from root node to some leaf node

CSE 373 SP 18 - KASEY CHAMPION 7

1

2 5

3 6 7

4 8

Page 8: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Tree HeightWhat is the height of the following trees?

CSE 373 SP 18 - KASEY CHAMPION 8

1

2 5

7

7

overallRoot overallRoot overallRoot

null

Height = 2 Height = 0 Height = -1 or NA

2 Minutes

Page 9: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Traversalstraversal: An examination of the elements of a tree.– A pattern used in many tree algorithms and methods

Common orderings for traversals:– pre-order: process root node, then its left/right subtrees– 17 41 29 6 9 81 40– in-order: process left subtree, then root node, then right– 29 41 6 17 81 9 40– post-order: process left/right subtrees, then root node– 29 6 41 81 40 9 17

Traversal Trick: Sailboat method– Trace a path around the tree.– As you pass a node on the

proper side, process it.• pre-order: left side• in-order: bottom• post-order: right side

CSE 373 SP 17 – ZORA FUNG 9

4081

941

17

629

overallRoot

Page 10: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Binary Search TreesA binary search tree is a binary tree that contains comparable items such that for every node, all children to the left contain smaller data and all children to the right contain larger data.

CSE 373 SP 18 - KASEY CHAMPION 10

10

9 15

7 12 18

8 17

Page 11: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Implement DictionaryBinary Search Trees allow us to:- quickly find what we’re looking for- add and remove values easily

Dictionary Operations:Runtime in terms of height, “h”get() – O(h)put() – O(h)remove() – O(h)

What do you replace the node with?Largest in left sub tree or smallest in right sub tree

CSE 373 SP 18 - KASEY CHAMPION 11

10

“foo”

7

“bar”

12

“baz”

9

“sho”

5

“fo”

15

“sup”

13

“boo”

8

“poo”

1

“burp”

Page 12: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Height in terms of NodesFor “balanced” trees h ≈ logc(n) where c is the maximum number of children

Balanced binary trees h ≈ log2(n)

Balanced trinary tree h ≈ log3(n)

Thus for balanced trees operations take Θ(logc(n))

CSE 373 SP 18 - KASEY CHAMPION 12

Page 13: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Unbalanced TreesIs this a valid Binary Search Tree?

Yes, but…

We call this a degenerate treeFor trees, depending on how balanced they are,

Operations at worst can be O(n) and at best

can be O(logn)

How are degenerate trees formed?- insert(10)- insert(9)- insert(7)- insert(5)

CSE 373 SP 18 - KASEY CHAMPION 13

10

9

7

5

Page 14: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Measuring BalanceMeasuring balance:

For each node, compare the heights of its two sub trees

Balanced when the difference in height between sub trees is no greater than 1

CSE 373 SP 18 - KASEY CHAMPION 14

10

15

12 18

8

7

78

7 9

Balanced

Unbalanced

Balanced

Balanced

2 Minutes

Page 15: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Meet AVL TreesAVL Trees must satisfy the following properties: - binary trees: all nodes must have between 0 and 2 children- binary search tree: for all nodes, all keys in the left subtree must be smaller and all keys in the right subtree must be

larger than the root node- balanced: for all nodes, there can be no more than a difference of 1 in the height of the left subtree from the right.

Math.abs(height(left subtree) – height(right subtree)) ≤ 1

AVL stands for Adelson-Velsky and Landis (the inventors of the data structure)

CSE 373 SP 18 - KASEY CHAMPION 15

Page 16: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Is this a valid AVL tree?

CSE 373 SP 18 - KASEY CHAMPION 16

7

4 10

3 9 125

8 11 13

14

2 6

Is it…- Binary- BST- Balanced?

yesyesyes

Page 17: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Is this a valid AVL tree?

CSE 373 SP 18 - KASEY CHAMPION 17

6

2 8

1 7 124

9

10 13

11

3 5

Is it…- Binary- BST- Balanced?

yesyesno

Height = 2Height = 0

2 Minutes

Page 18: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Is this a valid AVL tree?

CSE 373 SP 18 - KASEY CHAMPION 18

8

6 11

2 157

-1 9

Is it…- Binary- BST- Balanced?

yesnoyes

9 > 85

2 Minutes

Page 19: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Implementing an AVL tree dictionaryDictionary Operations:

get() – same as BST

containsKey() – same as BST

put() - ???

remove() - ???

CSE 373 SP 18 - KASEY CHAMPION 19

Add the node to keep BST, fix AVL property if necessary

Replace the node to keep BST, fix AVL property if necessary

1

2

3

Unbalanced!

2

1 3

Page 20: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

CSE 373 SP 18 - KASEY CHAMPION 20

Page 21: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Warm Up

CSE 373 SP 18 - KASEY CHAMPION 21

Page 22: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Meet AVL TreesAVL Trees must satisfy the following properties: - binary trees: all nodes must have between 0 and 2 children- binary search tree: for all nodes, all keys in the left subtree must be smaller and all keys in the right subtree must be

larger than the root node- balanced: for all nodes, there can be no more than a difference of 1 in the height of the left subtree from the right.

Math.abs(height(left subtree) – height(right subtree)) ≤ 1

AVL stands for Adelson-Velsky and Landis (the inventors of the data structure)

CSE 373 SP 18 - KASEY CHAMPION 22

Page 23: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Measuring BalanceMeasuring balance:

For each node, compare the heights of its two sub trees

Balanced when the difference in height between sub trees is no greater than 1

CSE 373 SP 18 - KASEY CHAMPION 23

10

15

12 18

8

7

78

7 9

Balanced

Unbalanced

Balanced

Balanced

Page 24: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Is this a valid AVL tree?

CSE 373 SP 18 - KASEY CHAMPION 24

7

4 10

3 9 125

8 11 13

14

2 6

Is it…- Binary- BST- Balanced?

yesyesyes

Page 25: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Is this a valid AVL tree?

CSE 373 SP 18 - KASEY CHAMPION 25

6

2 8

1 7 124

9

10 13

11

3 5

Is it…- Binary- BST- Balanced?

yesyesno

Height = 2Height = 0

2 Minutes

Page 26: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Is this a valid AVL tree?

CSE 373 SP 18 - KASEY CHAMPION 26

8

6 11

2 157

-1 9

Is it…- Binary- BST- Balanced?

yesnoyes

9 > 85

2 Minutes

Page 27: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Implementing an AVL tree dictionaryDictionary Operations:

get() – same as BST

containsKey() – same as BST

put() - ???

remove() - ???

CSE 373 SP 18 - KASEY CHAMPION 27

Add the node to keep BST, fix AVL property if necessary

Replace the node to keep BST, fix AVL property if necessary

1

2

3

Unbalanced!

2

1 3

Page 28: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Rotations!

CSE 373 SP 18 - KASEY CHAMPION 28

a

b

X

c

Y Z

a

b

X

Y Z c

a

b

X Y

Z

Insert ‘c’

Balanced!Unbalanced!

Page 29: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Rotate Left

CSE 373 SP 18 - KASEY CHAMPION 29

a

b

X

c

Y Z

a

b

X

Y Z c

a

b

X Y

Z

Insert ‘c’

Unbalanced!Balanced!

parent’s right becomes child’s left, child’s left becomes its parent

Page 30: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Rotate Right

CSE 373 SP 18 - KASEY CHAMPION 30

15

8 22

4 2410

3

19

6 2017

put(16);

16

0 0

1 0

2

0 0

01

2

3 height

0

1

2

3

4

Unbalanced!

parent’s left becomes child’s right, child’s right becomes its parent

Page 31: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

CSE 373 SP 18 - KASEY CHAMPION 31

15

8

224

24

10

3

19

6 20

17

put(16);

160 0

1 0

2

3

1

0 0 0

1

2

height

Rotate Rightparent’s left becomes child’s right, child’s right becomes its parent

Page 32: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

So much can go wrong

CSE 373 SP 18 - KASEY CHAMPION 32

1

3

2

Unbalanced!3

1

2

Rotate Left

Unbalanced!

Rotate Right

1

3

2

Unbalanced!

Parent’s left becomes child’s rightChild’s right becomes its parent

Parent’s right becomes child’s leftChild’s left becomes its parent

Page 33: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Two AVL Cases

CSE 373 SP 18 - KASEY CHAMPION 33

1

3

2

1

2

3

Line CaseSolve with 1 rotation

Kink CaseSolve with 2 rotations

3

2

1

Rotate RightParent’s left becomes child’s rightChild’s right becomes its parent

Rotate LeftParent’s right becomes child’s leftChild’s left becomes its parent

3

1

2

Right Kink ResolutionRotate subtree leftRotate root tree right

Left Kink ResolutionRotate subtree rightRotate root tree left

Page 34: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Double Rotations 1

CSE 373 SP 18 - KASEY CHAMPION 34

a

e

Wd

Y

Z

a

e

X

X

Z

Insert ‘c’

Unbalanced!

d

X

Y

c

a

d

W

Y

ZX

e

c

Page 35: Lecture 9: Intro to Trees CSE 373: Data Structures and ...

Double Rotations 2

CSE 373 SP 18 - KASEY CHAMPION 35

a

d

W

Y

ZX

e

c

Unbalanced!

a

d

W Y ZX

e

c