PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that:...

20
CS 225 Data Structures March 2 – AVL Analysis Wade Fagen-Ulmschneider

Transcript of PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that:...

Page 1: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

CS 225Data Structures

March 2 – AVL AnalysisWade Fagen-Ulmschneider

Page 2: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

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 3: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

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 4: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

5

3 6

4

2

8

10

9 12

111 7

Page 5: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

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

We will argue that: h is _________.

Page 6: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

AVL Tree AnalysisDefinition of big-O:

…or, with pictures:

n, number of nodes

h, h

eig

ht

n, n

um

be

r o

f n

od

es

h, height

Page 7: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

AVL Tree Analysis

An upper bound on the height h for a tree of n nodes…is the same as…

A lower bound on the number of nodes n in a tree of height h

n, number of nodes

h, h

eig

ht

n, n

um

be

r o

f n

od

es

h, height

Page 8: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

Plan of ActionSince our goal is to find the lower bound on n given h, we can begin by defining a function given h which describes the smallest number of nodes in an AVL tree of height h:

Page 9: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

Simplify the RecurrenceN(h) = 1 + N(h - 1) + N(h - 2)

Page 10: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

State a TheoremTheorem: An AVL tree of height h has at least __________.

Proof:I. Consider an AVL tree and let h denote its height.

II. Case: ______________

An AVL tree of height ____ has at least ____ nodes.

Page 11: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

Prove a TheoremIII. Case: ______________

An AVL tree of height ____ has at least ____ nodes.

Page 12: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

Prove a TheoremIV. Case: ______________By an Inductive Hypothesis (IH):

We will show that:

An AVL tree of height ____ has at least ____ nodes.

Page 13: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

Prove a TheoremV. Using a proof by induction, we have shown that:

…and inverting:

Page 14: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

Summary of Balanced BSTRed-Black Trees- Max height: 2 * lg(n)- Constant number of rotations on insert, remove, and find

AVL Trees- Max height: 1.44 * lg(n)- Rotations:

Page 15: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

Summary of Balanced BSTPros:- Running Time:

- Improvement Over:

- Great for specific applications:

Page 16: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

Summary of Balanced BSTCons:- Running Time:

- In-memory Requirement:

Page 17: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

IteratorsWhy do we care?

DFS dfs(...);

for ( ImageTraversal::Iterator it = dfs.begin(); it != dfs.end(); ++it ) {

std::cout << (*it) << std::endl;

}

12

3

4

Page 18: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

IteratorsWhy do we care?

DFS dfs(...);

for ( ImageTraversal::Iterator it = dfs.begin(); it != dfs.end(); ++it ) {

std::cout << (*it) << std::endl;

}

12

3

4

DFS dfs(...);

for ( const Point & p : dfs ) {

std::cout << p << std::endl;

}

12

3

4

Page 19: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

IteratorsWhy do we care?

DFS dfs(...);

for ( ImageTraversal::Iterator it = dfs.begin(); it != dfs.end(); ++it ) {

std::cout << (*it) << std::endl;

}

12

3

4

DFS dfs(...);

for ( const Point & p : dfs ) {

std::cout << p << std::endl;

}

12

3

4

ImageTraversal & traversal = /* ... */;

for ( const Point & p : traversal ) {

std::cout << p << std::endl;

}

12

3

4

Page 20: PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that: …and inverting: Summary of Balanced BST Red-Black Trees - Max height: 2 * lg(n) - Constant

Iterators

ImageTraversal *traversal = /* ... */;

for ( const Point & p : traversal ) {

std::cout << p << std::endl;

}

12

3

4