lecture 10: bst and avl

27
Lecture 10: BST and AVL Trees CSE 373: Data Structures and Algorithms CSE 373 19 SP - KASEY CHAMPION 1

Upload: others

Post on 22-May-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 10: BST and AVL

Lecture 10: BST and AVL Trees

CSE 373: Data Structures and Algorithms

CSE 373 19 SP - KASEY CHAMPION 1

Page 2: Lecture 10: BST and AVL

Warm Up

CSE 373 19 SP - KASEY CHAMPION 2

8

6 11

157

9 9 > 8

6

2 8

1 7 124

10 13

Is valid BST?Height?

Is valid BST?Height?

No

Yes

2

3

Page 3: Lecture 10: BST and AVL

Administrivia

CSE 373 19 SP - KASEY CHAMPION 3

Page 4: Lecture 10: BST and AVL

Trees

CSE 373 SP 18 - KASEY CHAMPION 4

Page 5: Lecture 10: BST and AVL

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 5

10

9 15

7 12 18

8 17

Page 6: Lecture 10: BST and AVL

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 6

10

“foo”

7

“bar”

12

“baz”

9

“sho”

5

“fo”

15

“sup”

13

“boo”

8

“poo”

1

“burp”

Page 7: Lecture 10: BST and AVL

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 7

Page 8: Lecture 10: BST and AVL

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 8

10

9

7

5

Page 9: Lecture 10: BST and AVL

Implementing Dictionary with BSTpublic boolean contains(K key, BSTNode node) {

if (node == null) {

return false;

}

int compareResult = compareTo(key, node.data);

if (compareResult < 0) {

returns contains(key, node.left);

} else if (compareResult > 0) {

returns contains(key, node.right);

} else {

returns true;

}

}

CSE 373 SP 18 - KASEY CHAMPION 9

+C1

+C3

+C2+T(n/2) best+ T(n-1) worst+T(n/2) best+ T(n-1) worst ! " = $

% &'(" " < * +, -(. /+0"1! "2 + % +4'(,&56(

! " = 7% &'(" " < * +, -(. /+0"1! " − 9 + % +4'(,&56(

Best Case (assuming key is at the bottom)

Worst Case (assuming key is at the bottom)

2 Minutes

Page 10: Lecture 10: BST and AVL

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 10

10

15

12 18

8

7

78

7 9

Balanced

Unbalanced

Balanced

Balanced

2 Minutes

Page 11: Lecture 10: BST and AVL

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 11

Page 12: Lecture 10: BST and AVL

Is this a valid AVL tree?

CSE 373 SP 18 - KASEY CHAMPION 12

7

4 10

3 9 125

8 11 13

14

2 6

Is it…- Binary- BST- Balanced?

yesyesyes

Page 13: Lecture 10: BST and AVL

Is this a valid AVL tree?

CSE 373 SP 18 - KASEY CHAMPION 13

6

2 8

1 7 124

9

10 13

11

3 5

Is it…- Binary- BST- Balanced?

yesyesno

Height = 2Height = 0

2 Minutes

Page 14: Lecture 10: BST and AVL

Is this a valid AVL tree?

CSE 373 SP 18 - KASEY CHAMPION 14

8

6 11

2 157

-1 9

Is it…- Binary- BST- Balanced?

yesnoyes

9 > 85

2 Minutes

Page 15: Lecture 10: BST and AVL

Implementing an AVL tree dictionaryDictionary Operations:

get() – same as BST

containsKey() – same as BST

put() - ???

remove() - ???

CSE 373 SP 18 - KASEY CHAMPION 15

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 16: Lecture 10: BST and AVL

Rotations!

CSE 373 SP 18 - KASEY CHAMPION 16

a

b

X

c

Y Z

a

b

X

Y Z c

a

b

X Y

Z

Insert ‘c’

Balanced!Unbalanced!

Page 17: Lecture 10: BST and AVL

Rotate Left

CSE 373 SP 18 - KASEY CHAMPION 17

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 18: Lecture 10: BST and AVL

Rotate Right

CSE 373 SP 18 - KASEY CHAMPION 18

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 19: Lecture 10: BST and AVL

CSE 373 SP 18 - KASEY CHAMPION 19

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 20: Lecture 10: BST and AVL

So much can go wrong

CSE 373 SP 18 - KASEY CHAMPION 20

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 21: Lecture 10: BST and AVL

Two AVL Cases

CSE 373 SP 18 - KASEY CHAMPION 21

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 22: Lecture 10: BST and AVL

Double Rotations 1

CSE 373 SP 18 - KASEY CHAMPION 22

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 23: Lecture 10: BST and AVL

Double Rotations 2

CSE 373 SP 18 - KASEY CHAMPION 23

a

d

W

Y

ZX

e

c

Unbalanced!

a

d

W Y ZX

e

c

Page 24: Lecture 10: BST and AVL

Implementing Dictionary with AVLpublic boolean contains(K key, AVLNode node) {

if (node == null) {

return false;

}

int compareResult = compareTo(key, node.data);

if (compareResult < 0) {

returns contains(key, node.left);

} else if (compareResult > 0) {

returns contains(key, node.right);

} else {

returns true;

}

}

CSE 373 SP 18 - KASEY CHAMPION 24

+C1

+C3

+C2

+T(n/2)

+T(n/2)

! " = $% &'(" " < * +, -(. /+0"1

! "2 + % +4'(,&56(

Worst Case Improvement Guaranteed with AVL

2 Minutes

Page 25: Lecture 10: BST and AVL

How long does AVL insert take?

AVL insert time = BST insert time + time it takes to rebalance the tree

= O(log n) + time it takes to rebalance the tree

How long does rebalancing take?- Assume we store in each node the height of its subtree.- How long to find an unbalanced node:

- Just go back up the tree from where we inserted.

- How many rotations might we have to do?- Just a single or double rotation on the lowest unbalanced node.

AVL insert time = O(log n)+ O(log n) + O(1) = O(log n)

ß O(log n)

ß O(1)

CSE 373 AU 18 – SHRI MARE

Page 26: Lecture 10: BST and AVL

Pros:

- O(logn) worst case for find, insert, and delete operations.

- Reliable running times than regular BSTs (because trees are balanced)

Cons:

- Difficult to program & debug [but done once in a library!]

- (Slightly) more space than BSTs to store node heights.

AVL wrap up

CSE 373 AU 18 – SHRI MARE 26

Page 27: Lecture 10: BST and AVL

Lots of cool Self-Balancing BSTs out there!

Popular self-balancing BSTs include:

AVL tree

Splay tree

2-3 tree

AA tree

Red-black tree

Scapegoat tree

Treap

(From https://en.wikipedia.org/wiki/Self-balancing_binary_search_tree#Implementations)

(Not covered in this class, but several are in the textbook and all of them are online!)

CSE 373 SU 17 – LILIAN DE GREEF