PowerPoint Presentation...Oct. 16 – AVL Operations . AVL Tree Rotations Four templates for...

13
CS 225 Data Structures Oct. 16 – AVL Operations

Transcript of PowerPoint Presentation...Oct. 16 – AVL Operations . AVL Tree Rotations Four templates for...

Page 1: PowerPoint Presentation...Oct. 16 – AVL Operations . AVL Tree Rotations Four templates for rotations: Height-Balanced Tree ... •Iterators •Functors •MP 3 •Huffman Encoding

CS 225 Data Structures

Oct. 16 – AVL Operations

Page 2: PowerPoint Presentation...Oct. 16 – AVL Operations . AVL Tree Rotations Four templates for rotations: Height-Balanced Tree ... •Iterators •Functors •MP 3 •Huffman Encoding

AVL Tree Rotations Four templates for rotations:

Page 3: PowerPoint Presentation...Oct. 16 – AVL Operations . AVL Tree Rotations Four templates for rotations: Height-Balanced Tree ... •Iterators •Functors •MP 3 •Huffman Encoding

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

Page 4: PowerPoint Presentation...Oct. 16 – AVL Operations . AVL Tree Rotations Four templates for rotations: Height-Balanced Tree ... •Iterators •Functors •MP 3 •Huffman Encoding

Exams Current Exam: Exam 6 (Programming) Next Week: Exam 7 (Theory) • Topics:

• Trees: Binary, Binary Search, AVL Rotations

• Iterators

• Functors

• MP 3

• Huffman Encoding

• A study guide will be released in the next few days

Page 5: PowerPoint Presentation...Oct. 16 – AVL Operations . AVL Tree Rotations Four templates for rotations: Height-Balanced Tree ... •Iterators •Functors •MP 3 •Huffman Encoding

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 6: PowerPoint Presentation...Oct. 16 – AVL Operations . AVL Tree Rotations Four templates for rotations: Height-Balanced Tree ... •Iterators •Functors •MP 3 •Huffman Encoding

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 7: PowerPoint Presentation...Oct. 16 – AVL Operations . AVL Tree Rotations Four templates for rotations: Height-Balanced Tree ... •Iterators •Functors •MP 3 •Huffman Encoding

Insert (pseudo code): 1: Insert at proper place 2: Check for imbalance 3: Rotate, if necessary 4: Update height

Insertion into an AVL Tree

5

3 6

4

2

8

10

9 12

11 1 7 struct TreeNode {

T key;

unsigned height;

TreeNode *left;

TreeNode *right;

};

1

2

3

4

5

6

_insert(6.5)

Page 8: PowerPoint Presentation...Oct. 16 – AVL Operations . AVL Tree Rotations Four templates for rotations: Height-Balanced Tree ... •Iterators •Functors •MP 3 •Huffman Encoding

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));

}

1 2

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 9: PowerPoint Presentation...Oct. 16 – AVL Operations . AVL Tree Rotations Four templates for rotations: Height-Balanced Tree ... •Iterators •Functors •MP 3 •Huffman Encoding

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

Page 10: PowerPoint Presentation...Oct. 16 – AVL Operations . AVL Tree Rotations Four templates for rotations: Height-Balanced Tree ... •Iterators •Functors •MP 3 •Huffman Encoding

5

3 6

4

2

8

10

9 12

11 1 7

Page 11: PowerPoint Presentation...Oct. 16 – AVL Operations . AVL Tree Rotations Four templates for rotations: Height-Balanced Tree ... •Iterators •Functors •MP 3 •Huffman Encoding

AVL Tree Analysis We know: insert, remove and find runs in: __________. We will argue that: h = _________.

Page 12: PowerPoint Presentation...Oct. 16 – AVL Operations . AVL Tree Rotations Four templates for rotations: Height-Balanced Tree ... •Iterators •Functors •MP 3 •Huffman Encoding

AVL Tree Analysis Definition of big-O: …or, with pictures:

Page 13: PowerPoint Presentation...Oct. 16 – AVL Operations . AVL Tree Rotations Four templates for rotations: Height-Balanced Tree ... •Iterators •Functors •MP 3 •Huffman Encoding

CS 225 – Things To Be Doing Exam 6 (Programming, Lists/Trees) is ongoing! More Info: https://courses.engr.illinois.edu/cs225/fa2017/exams/

MP4: Available now! Due: Monday, Oct. 23 at 11:59pm

Labs New lab on Wednesday

POTD Every Monday-Friday – Worth +1 Extra Credit /problem (up to +40 total)