CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add...
Transcript of CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add...
![Page 1: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/1.jpg)
CS 261 – Data Structures
AVL Trees
![Page 2: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/2.jpg)
AVL Implementation
struct AVLNode {
TYPE val;
struct AVLNode *left;
struct AVLNode *rght;
int hght; /* Height of node*/
};
![Page 3: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/3.jpg)
Add
Alex
Abner Angela
Adela
Adam
Abigail
![Page 4: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/4.jpg)
Add – Insert at the Leaf Level
Adam
Alex
Abner Angela
Adela
Adam
Abigail
Alex
Abner Angela
AdelaAbigail
height = 0height = 2
![Page 5: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/5.jpg)
Add – Insert at the Leaf Level
Adam
Alex
Abner Angela
Adela
Adam
Abigail
Alex
Abner Angela
AdelaAbigail
height = 0height = 2
Rotation right
Is it double rotation?
![Page 6: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/6.jpg)
Add – Insert at the Leaf Level
Adam
Alex
Abner Angela
Adela
Adam
Abigail
Alex
Abner Angela
AdelaAbigail
height = 0height = 2
rotation right
height = 1height = 0
left child is heavy on the right
![Page 7: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/7.jpg)
Add – Insert at the Leaf Level
Adam
Alex
Abner Angela
Adela
Adam
Abigail
Alex
Abner Angela
AdelaAbigail
Double rotation right
![Page 8: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/8.jpg)
Add – Insert at the Leaf Level
Adam
Alex
Angela
Adam
Abner
Adela
Abigail
Alex
Abner Angela
AdelaAbigail
Double rotation right
![Page 9: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/9.jpg)
Add – Insert at the Leaf Level
Adam
Alex
AngelaAdam
Abner
Adela
Abigail
Alex
Abner Angela
AdelaAbigail
Double rotation right
height = 1height = 1
![Page 10: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/10.jpg)
Add – Recursive Functionstruct AVLNode *_addAVLNode(struct AVLNode* current, TYPE e){
...
if (current == 0) {/* stop recursion */
...
/*allocate memory for newnode; add newnode to tree*/
return newnode;
}
else {
/* recursively move down the tree */
}
return /* ???? */;
}
![Page 11: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/11.jpg)
Add – Recursive Functionstruct AVLNode *_addAVLNode(struct AVLNode* current, TYPE e){
...
if (current == 0) {/* stop recursion */
...
/*allocate memory for newnode; add newnode to tree*/
return newnode; /* why not call balance() here? */
}
else {
/* recursively move down the tree */
}
return balance(current);
}
![Page 12: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/12.jpg)
Add – Recursive Functionstruct AVLNode *_addAVLNode(struct AVLNode* current, TYPE e){
...
if (current == 0) {/* stop recursion */
... /*allocate memory for newnode; add newnode to tree*/
return newnode;
}
else {/* recursively move down the tree */
if( LT(e, current->value) )
current->left = _addAVLNode(current->left, e);
else
current->right = _addAVLNode(current->right, e);
}
return balance(current);
}
![Page 13: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/13.jpg)
Rotation
13
1(0)
2(1)
6(0)3(0)
4(2)
5(1)
1(0)
2(3)
6(0)
3(0)
4(2)
5(1)
Rotateleft
2(0)
3(1)
7(0)5(0)
4(2)
6(1)7(0)
6(3)
2(0)
3(1)
4(2)
5(0)
Rotateright
![Page 14: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/14.jpg)
Double Rotation
14
7(0)
6(3)
4(0)
5(1)
4(2)
Rotateleft the
left child 7(0)
6(3)
5(2)
4(1)
4(0)
Rotateright
7(0)
6(3)
5(2)
4(1)
4(0)
heavyon right
![Page 15: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/15.jpg)
Balancestruct AVLNode * balance (struct AVLNode * current){
int rotation = _height(current->right) - _height(current->left);
if (rotation < -1) {
/* (double) rotation right */
} else if (rotation > 1) {
/* (double) rotation left */
}
_setHeight(current);
return current;
}
![Page 16: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/16.jpg)
Balance – (Double) Rotation Right...
if (rotation < -1) { /* (double) rotation right */
int drotation = _height(current->left->right) - _height(current->left->left);
if (drotation > 0){ /* double rotation */
/* left child is heavy on the right */
current->left = _rotateLeft(current->left);
}
return _rotateRight(current);
}
else { ...
![Page 17: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/17.jpg)
Balance – (Double) Rotation Left...
}else if (rotation > 1) { /* (double) rotation left */
int drotation = _height(current->right->right)
- _height(current->right->left);
if (drotation < 0) {/* double rotation */
/* right child is heavy on the left */
current->right = _rotateRight(current->right);
}
return _rotateLeft(current);
} ...
![Page 18: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/18.jpg)
Rotation Leftstruct AVLNode * _rotateLeft (struct AVLNode * current){
struct AVLNode * newtop = current->right;
...
}
1(0)
2(3)
6(0)
3(0)
4(2)
5(1)
current
newtop
![Page 19: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/19.jpg)
Rotation Leftstruct AVLNode * _rotateLeft (struct AVLNode * current){
struct AVLNode * newtop = current->right;
current->right = newtop->left;
newtop->left = current;
...
}
1(0)
2(3)
6(0)
3(0)
4(2)
5(1) 1(0)
2(3)
6(0)3(0)
4(2)
5(1)
current
newtop
![Page 20: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/20.jpg)
Rotation Leftstruct AVLNode * _rotateLeft (struct AVLNode * current){
struct AVLNode * newtop = current->right;
current->right = newtop->left;
newtop->left = current;
_setHeight(current);
_setHeight(newtop);
return newtop;
}
1(0)
2(3)
6(0)
3(0)
4(2)
5(1) 1(0)
2(1)
6(0)3(0)
4(2)
5(1)
![Page 21: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/21.jpg)
Rotation Rightstruct AVLNode * _rotateRight (struct AVLNode * current){
struct AVLNode * newtop = current->left;
current->left = newtop->right;
newtop->right = current;
_setHeight(current);
_setHeight(newtop);
return newtop;
}
7(0)
6(3)
2(0)
3(1)
4(2)
5(0) 2(0)
3(1)
7(0)5(0)
4(2)
6(1)
current
newtop
![Page 22: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/22.jpg)
Remove: Who fills the hole in the tree?
Answer: the leftmost child of the right child (smallest element in right subtree)
![Page 23: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/23.jpg)
Remove
void removeAVLTree(struct AVLTree *tree, TYPE val) {
if (containsAVLTree(tree, val)) {
tree->root = _removeNode(tree->root, val);
tree->cnt--;
}
}
![Page 24: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/24.jpg)
Removestruct AVLNode *_removeNode(struct AVLNode *current, TYPE e) {
struct AVLNode *temp;
assert(current);
if(EQ(e, current->val)){
/* replace current with the leftmost descendant
of the right child */
}
else if(LT(e, current->val))
current->left = _removeNode(current->left, e);
else
current->rght = _removeNode(current->rght, e);
return balance(current);
}
![Page 25: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/25.jpg)
AVL Trees: Sorting
• An AVL tree can sort a collection of values:
1.Copy data into the AVL tree: O(??)
2.Copy them out using the ?? traversal: O(??)
25
![Page 26: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/26.jpg)
AVL Trees: Sorting
• An AVL tree can sort a collection of values:Copy data into the AVL tree:
O(n log2n)Copy them out using the in-order traversal:
O(n)
26
![Page 27: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner](https://reader031.fdocuments.in/reader031/viewer/2022030409/5a8fb3207f8b9a78648dc7a9/html5/thumbnails/27.jpg)
AVL Trees: Sorting
• Execution time à O(n log n):– Matches that of quick sort in benchmarks– Unlike quick sort, AVL trees don’t have problems if data is
already sorted or almost sorted (which degrades quick sort to O(n2))
• However, requires extra storage to maintain both the original data buffer (e.g., a DynArr) and the tree structure
27