tree balancing: avl trees - seattle...
TRANSCRIPT
![Page 1: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/1.jpg)
Tree Balancing: AVL Trees
Dr. Yingwu Zhu
![Page 2: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/2.jpg)
Recall in BST
• The insertion order of items determine the shape of BST– Balanced: search T(n)=O(logN)
– Unbalanced: T(n) = O(n)
• Key issue:– Need to keep a BST balanced!
– Tree balancing techniques
![Page 3: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/3.jpg)
AVL Tree Definition
• First, a BST
• Second, height-balance property: balance factor of each node is 0, 1, or -1
• Question: what is balance factor?
![Page 4: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/4.jpg)
AVL Tree Definition
• First, a BST
• Second, height-balance property: balance factor of each node is 0, 1, or -1
• Question: what is balance factor?
BF = Height of the left subtree – height of the right subtree
Height: # of levels in a subtree/tree
![Page 5: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/5.jpg)
Determine balance factor
A
B D
GE
HF
C
![Page 6: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/6.jpg)
ADT: AVL Trees
• Data structure to implement
Balance factor
Data
Left Right
![Page 7: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/7.jpg)
ADT: AVL Trees
• Basic operations– Constructor, search, traversal, empty
– Insert: keep balanced!
– Delete: keep balanced!
– See P842 class declaration
– Similar to BST
![Page 8: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/8.jpg)
Example
RI
PA
Insert “DE”, what happens? Need rebalancing?
![Page 9: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/9.jpg)
Basic Rebalancing Rotation
• Single rotation:
– Right rotation: the inserted item is on the Left subtree of Left child
of the nearest ancestor with BF of 2
– Left rotation: the inserted item is on the Right subtree of Right child
of the nearest ancestor with BF of -2
• Double rotation
– Left-right rotation: the inserted item is on the Right subtree of Left
child of the nearest ancestor with BF of 2
– Right-left rotation: the inserted item is on the Left subtree of Right
child of the nearest ancestor with BF of -2
![Page 10: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/10.jpg)
How to perform rotations
• Rotations are carried out by resetting links
• Two steps:
– Determine which rotation
– Perform the rotation
![Page 11: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/11.jpg)
Right Rotation
• Key: identify the nearest ancestor of inserted item with BF +2
• A: the nearest ancestor.
• B: left child
• How? (clockwise rotation)
– Step1: reset the link from parent of A to B (promote B)
– Step2: set the left link of A equal to the right link of B
– Step3: set the right link of B to A (demote A)
![Page 12: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/12.jpg)
Example
A
B
L(A)
L(B) R(B)
new
h
![Page 13: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/13.jpg)
Exercise #1
10
8
96
20
How about insert 4
![Page 14: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/14.jpg)
Left Rotation
• How? (counter-clockwise rotation)
– Step1: reset the link from parent of A to B (promote B)
– Step2: set the right link of A to the left link of B
– Step3: set the left link of B to A (demote A)
![Page 15: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/15.jpg)
Example
A
B
L(A)
L(B) R(B)
new
h
![Page 16: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/16.jpg)
Exercise #2
7
5 15
10 20
18What if insert
![Page 17: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/17.jpg)
Exercise #3
12
8 16
4 10 14
2 6
1How about insert
![Page 18: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/18.jpg)
Double Rotation
• Left-right rotation: the inserted item is on
the Right subtree of Left child of the nearest ancestor with BF of 2
• Right-left rotation: the inserted item is on
the Left subtree of Right child of the nearest ancestor with BF of -2
![Page 19: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/19.jpg)
Double Rotations
• How to perform?
– Step 1: Rotate child and grandchild nodes of the ancestor (grandchild < -- > child)
– Step 2: Rotate the ancestor and its new child node
![Page 20: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/20.jpg)
Example
AC
B
What if insert
![Page 21: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/21.jpg)
Example
A
B
R(A)
L(B)R(C)
new
h+1
C
L(C)
h
h+1
![Page 22: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/22.jpg)
Exercise #4
12
8 16
4 10 14
2 6
7How about insert
![Page 23: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/23.jpg)
Exercise #5
4
2 6
1 3 5
16
7
15What if insert
![Page 24: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/24.jpg)
Exercise #6
4
2 6
1 3 5
16
15
14What if insert
7
![Page 25: Tree Balancing: AVL Trees - Seattle Universityfac-staff.seattleu.edu/.../Fall10/CPSC250/AVL_trees.pdf · 2010. 10. 19. · •AVL tree definition •The key is you need to identify](https://reader035.vdocument.in/reader035/viewer/2022062609/60f8c863025372524113a12e/html5/thumbnails/25.jpg)
Summary on Rebalancing
• AVL tree definition
• The key is you need to identify the nearest ancestor of inserted item
• Determine the rotation by definition
• Perform the rotation
• Implementation of AVL trees is not required in this class