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

Post on 22-May-2022

1 views 0 download

Transcript of 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

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

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

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

Trees

CSE 373 SP 18 - KASEY CHAMPION 5

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

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

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

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

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

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”

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

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

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

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

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

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

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

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

CSE 373 SP 18 - KASEY CHAMPION 20

Warm Up

CSE 373 SP 18 - KASEY CHAMPION 21

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

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

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

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

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

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

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!

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

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

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

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

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

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

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