PowerPoint Presentation · Prove a Theorem V. Using a proof by induction, we have shown that:...
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/1.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/2.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/3.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/4.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/5.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/6.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/7.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/8.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/9.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/10.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/11.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/12.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/13.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/14.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/15.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/16.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/17.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/18.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/19.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022040410/5ecad4235402dc3f3815793f/html5/thumbnails/20.jpg)
Iterators
ImageTraversal *traversal = /* ... */;
for ( const Point & p : traversal ) {
std::cout << p << std::endl;
}
12
3
4