advanced tree structures · 2020. 11. 6. · modified binary search trees the real world often...
TRANSCRIPT
![Page 1: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/1.jpg)
Advanced Tree Structures
CMSC132
![Page 2: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/2.jpg)
Degenerate Search Trees
● Standard BST only as good as
its insertion order
● Very easy to make
“degenerate”
10
20
15
![Page 3: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/3.jpg)
Degenerate Search Trees
● Standard BST only as good as
its insertion order
● Very easy to make
“degenerate”
10
20
15
Insert: 12
![Page 4: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/4.jpg)
Degenerate Search Trees
● Standard BST only as good as
its insertion order
● Very easy to make
“degenerate”
1210
20
15
Insert: 12
<
![Page 5: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/5.jpg)
Degenerate Search Trees
● Standard BST only as good as
its insertion order
● Very easy to make
“degenerate”
10
20
15
Insert: 12
12 >
![Page 6: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/6.jpg)
Degenerate Search Trees
● Standard BST only as good as
its insertion order
● Very easy to make
“degenerate”
10
20
15
Insert: 12
12 >
![Page 7: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/7.jpg)
Degenerate Search Trees
● Standard BST only as good as
its insertion order
● Very easy to make
“degenerate”
● Congratulations, you have a
linked list
● How long to find/insert/delete?
10
20
15
12
![Page 8: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/8.jpg)
Modified Binary Search Trees
The real world often doesn’t use stock BSTs
Many of these covered in detail in CMSC420: Advanced Data Structures
Strategy Tree Type
Rotation AVL trees, red-black trees
Multi-way 2-3 trees
Rebuild Scapegoat trees
Randomization Treaps, Skiplists
Use case Tree Type
Strings Tries
File systems/db B/B+ trees
Usage frequency Splay trees
Multi-dimensional K-d trees
![Page 9: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/9.jpg)
AVL Trees
● Invented by Adelson-Velsky
and Landis in 1962
● Approach: keep height of
subtrees roughly equal
● Strategy: when unequal,
rebalance tree with rotations
● Outcome: worst case O(logn)
performance
44
17 78
32 50 88
48 62
4
2
11
12
3
1
Inner nodes:
Leaf nodes:
Subtree heights:
x
y
![Page 10: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/10.jpg)
Left Rotation
A moves down
C moves up
![Page 11: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/11.jpg)
Left Rotation
A moves down
C moves up
![Page 12: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/12.jpg)
Left Rotation
A moves down
C moves up
Fixes:
● C too tall
● A too short
![Page 13: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/13.jpg)
Right rotation
A moves up
C moves down
![Page 14: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/14.jpg)
Right rotation
A moves up
C moves down
![Page 15: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/15.jpg)
Right rotation
A moves up
C moves down
Fixes:
● C too short
● A too tall
![Page 16: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/16.jpg)
Problem
Left and right rotation move A & C up & down, but what about B?
![Page 17: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/17.jpg)
Problem
Left and right rotation move A & C up & down, but what about B?
Rotate twice!
![Page 18: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/18.jpg)
Left-Right Rotation
Let’s fix the height of B1/B2 - We’ve already seen this can’t be done with a single rotation
![Page 19: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/19.jpg)
Left-Right Rotation
First, rotate left so that B2 moves up and A moves down
![Page 20: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/20.jpg)
Left-Right Rotation
First, rotate left so that B2 moves up and A moves down
Then, rotate right so that C moves down and the subtree rooted at 1 moves up
![Page 21: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/21.jpg)
Specific Left-Right Rotation Example
Rotated left
around “10”
Rotated right
around “15” to
restore balanceStarting with
unbalanced tree
● Insert 20, then 10, then 15 into an empty tree.
● Insertion of 15 unbalances the tree, so we must perform a LR rotation.
![Page 22: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/22.jpg)
Right-Left Rotation
The same, but in reverse
![Page 23: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/23.jpg)
Rotation Rules
● These 4 rotations allow an AVL tree to self rebalance
● Rotate based off of which grandchild is too tall
1. Left-left: right rotation
2. Left-right: left-right rotation
3. Right-left: right-left rotation
4. Right-right: left rotation
![Page 24: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/24.jpg)
Implementation Details
Code for AVL trees is “relatively” simple:
1. Add extra field for keeping track of height in Node class
2. After modification, update appropriate height fields
3. After modification, rebalance at each level if needed
4. Key search as normal
Extra functions: rebalance, updateHeight, and rotations
![Page 25: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/25.jpg)
Result
● Other trees can be more involved
● AVL isn’t perfect - Java uses red-black trees○ AVL provides faster lookup and slower insert/remove
○ R-B provides faster insertion/removal and slower lookup
○ R-B uses slightly less storage
○ R-B is harder to do in a 30 minute presentation
● AVL Demo:
https://www.cs.usfca.edu/~galles/visualization/AVLtree.html
![Page 26: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/26.jpg)
Resources
These are the notes from Professor David Mount’s CMSC420 class from Fall 2020.
http://www.cs.umd.edu/class/fall2020/cmsc420-0201/Lects/lect05-avl.pdf
http://www.cs.umd.edu/class/fall2020/cmsc420-0201/Slides/lect05-avl-slides.pdf
AVL trees on Wikipedia:
https://en.wikipedia.org/wiki/AVL_tree
![Page 27: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/27.jpg)
References
1. Adel'son-Vel'skii, George M., and Evgenii Mikhailovich Landis. "An algorithm for organization of information." In Doklady
Akademii Nauk, vol. 146, no. 2, pp. 263-266. Russian Academy of Sciences, 1962.
2. Cormen, Thomas H., Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to algorithms. MIT press, 2009.
![Page 28: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/28.jpg)
End of presentation
(Subsequent slides include some notes for TAs & extra material on keeping track of node heights)
![Page 29: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/29.jpg)
Keeping track of height
class Node {
T data;
Node left;
Node right;
int height;
}
int height(Node node) {
if (node == null) return 0;
return Math.max(height(node.left),
height(node.right)) + 1;
}
![Page 30: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/30.jpg)
Keeping track of height
class Node {
T data;
Node left;
Node right;
int height;
}
void updateHeight(Node node) {
if (node == null) return;
node.height = Math.max(height(node.left),
height(node.right)) + 1;
}
int height(Node node) {
return node == null ? 0 : node.height;
}
![Page 31: Advanced Tree Structures · 2020. 11. 6. · Modified Binary Search Trees The real world often doesn’t use stock BSTs Many of these covered in detail in CMSC420: Advanced Data Structures](https://reader033.vdocument.in/reader033/viewer/2022052814/609eaee967b26a685e578c7b/html5/thumbnails/31.jpg)
TAs:
● Subtree heights and the height difference of subtrees is favored over the terminology “balance
factor”
● TAs can theme the presentation however they wish, but colors of diagrams may be adversely
affected