powerpoint presentation...theorem: if an insertion occurred in subtrees t 3 or t 4 and a subtree was...

21
CS 225 Data Structures Feb. 28 – AVL Trees Wade Fagen-Ulmschneider

Upload: others

Post on 23-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

CS 225Data Structures

Feb. 28 – AVL TreesWade Fagen-Ulmschneider

Page 2: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

Course Logistics UpdateCBTF exams will go on as-scheduled:• Theory Exam 2 is ongoing• Sample Exam available on PL

MPs and Lab assignments will be due on schedule:• MP4 is released; due March 12, 2018• lab_huffman is released later today

My office hours are cancelled today.

Page 3: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

Lab SectionsAll lab sections are not meeting this week.

Instead, all CAs and non-striking TAs will hold open office hours (using the regular queue, held in the basement):• Feel free to use the room to work with your peers on the

lab. Staff will be available in open office hours in the basement of Siebel.

• An intro video on Huffman trees will be provided.

Page 4: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

13

10 25

38

51

40 84

8966

95

Left Rotation

Page 5: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

13

10 25

38

51

84

89

A

B

C D

Page 6: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

13

10 25

38

51

84

89

A

B

C D

84

51 89

A B C D

Page 7: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

13

10 25

38

51

40 84

8966

95

Page 8: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

13

10 25

37

38

51

Page 9: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

13

10 25

37

38

51

Page 10: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

BST Rotation Summary- Four kinds of rotations (L, R, LR, RL)- All rotations are local (subtrees are not impacted)- All rotations are constant time: O(1)- BST property maintained

GOAL:

We call these trees:

Page 11: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

AVL TreesThree issues for consideration:- Rotations- Maintaining Height- Detecting Imbalance

Page 12: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

AVL Tree RotationsFour templates for rotations:

Page 13: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

t

t1

t2

t3 t4

Theorem:If an insertion occurred in subtrees t3 or t4 and a subtree was detected at t, then a __________ rotation about t restores the balance of the tree.

We gauge this by noting the balance factor of t->right is ______.

Finding the Rotation

Page 14: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

t

t1

t2 t3

t4

Theorem:If an insertion occurred in subtrees t2 or t3 and a subtree was detected at t, then a __________ rotation about t restores the balance of the tree.

We gauge this by noting the balance factor of t->right is ______.

Finding the Rotation

Page 15: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

Insertion into an AVL Tree

5

3 6

4

2

8

10

9 12

111 7struct TreeNode {

T key;

unsigned height;

TreeNode *left;

TreeNode *right;

};

1

2

3

4

5

6

_insert(6.5)

Page 16: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

Insert (pseudo code):1: Insert at proper place2: Check for imbalance3: Rotate, if necessary4: Update height

Insertion into an AVL Tree

5

3 6

4

2

8

10

9 12

111 7struct TreeNode {

T key;

unsigned height;

TreeNode *left;

TreeNode *right;

};

1

2

3

4

5

6

_insert(6.5)

Page 17: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

template <class T> void AVLTree<T>::_insert(const T & x, treeNode<T> * & t ) {

if( t == NULL ) {

t = new TreeNode<T>( x, 0, NULL, NULL);

}

else if( x < t->key ) {

_insert( x, t->left );

int balance = height(t->right) - height(t->left);

int leftBalance = height(t->left->right) - height(t->left->left);

if ( balance == -2 ) {

if ( leftBalance == -1 ) { rotate_____________( t ); }

else { rotate_____________( t ); }

}

}

else if( x > t->key ) {

_insert( x, t->right );

int balance = height(t->right) - height(t->left);

int rightBalance = height(t->right->right) - height(t->right->left);

if( balance == 2 ) {

if( rightBalance == 1 ) { rotate_____________( t ); }

else { rotate_____________( t ); }

}

}

t->height = 1 + max(height(t->left), height(t->right));

}

12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

Page 18: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

Height-Balanced TreeHeight balance: b = height(TR) - height(TL)

Page 19: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

5

3 6

4

2

8

10

9 12

111 7

Page 20: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

AVL Tree AnalysisWe know: insert, remove and find runs in: __________.

We will argue that: h = _________.

Page 21: PowerPoint Presentation...Theorem: If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t, then a _____ rotation about t restores the balance of the tree

AVL Tree AnalysisDefinition of big-O:

…or, with pictures: