Download - AVL Tree
![Page 1: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/1.jpg)
AVL Tree
27th Mar 2007
![Page 2: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/2.jpg)
AVL Trees Pencarian node pada unbalanced Binary Search
Trees adalah tidak efisien. Worst case: operations take O(n).
AVL (Adelson-Velskii & Landis) trees adalah BST yang diseimbangkan
Untuk setiap node pada tree, beda tinggi antara left subtree dan right subtree maximum 1 saja.
27th Mar 2007
X X
H
H-1H-2
![Page 3: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/3.jpg)
AVL Trees
27th Mar 2007
12
8 16
4 10
2 6
14
![Page 4: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/4.jpg)
Untuk setiap node pada tree, beda tinggi antara left subtree dan right subtree maximum 1 saja.
27th Mar 2007
AVL property violated here
![Page 5: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/5.jpg)
AVL Trees
27th Mar 2007
10
5
3
20
2
1 3
10
5
3
20
1
43
5
![Page 6: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/6.jpg)
Insertion for AVL TreeAfter insert 1
27th Mar 2007
12
8 16
4 10
2 6
14
1
![Page 7: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/7.jpg)
Insertion for AVL Tree
Untuk memastikan AVL-tree dalam kondisi seimbang, setelah suatu node di-insert-kan, perlu dicek keseimbangan untuk tiap node
Jika setelah insertion ada node yang tidak seimbang, maka dilakukan operasi berikut :Single rotation
Double rotation
27th Mar 2007
![Page 8: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/8.jpg)
Insertions Causing Imbalance
Kemungkinan insertion yang menyebabkan ketidakseimbangan Insertion pada P (outside) case1 Insertion pada Q (inside) case2
27th Mar 2007
RP
Q
k1
k2
Q
k2
P
k1
R
AVL TreeHP=HQ=HR
Kemungkinan insertion yang menyebabkan ketidakseimbangan Insertion pada Q (inside) case 3 Insertion pada R (outside) case 4
![Page 9: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/9.jpg)
Single Rotation (case 1)
27th Mar 2007
A
k2
B
k1
CCBA
k1
k2
insertion pada A menyebabkan ketidakseimbangan
![Page 10: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/10.jpg)
Single Rotation (case 4)
27th Mar 2007
C
k1
B
k2
A
A B C
k2
k1
insertion pada C menyebabkan ketidakseimbangan
![Page 11: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/11.jpg)
Problem with Single Rotation Single rotation does not work for case 2 and 3
(inside case)
27th Mar 2007
Q
k2
P
k1
RR
P
Q
k1
k2
Insertion pada Q kemudian single rotation, tetap tidak seimbang
![Page 12: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/12.jpg)
Double Rotation: Stepsama dengan dua kali single rotation
27th Mar 2007
C
k3
A
k1
D
B
k2
C
k3
A
k1
D
B
k2
![Page 13: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/13.jpg)
Double Rotation: Step
27th Mar 2007
C
k3
A
k1
DB
k2
![Page 14: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/14.jpg)
Double Rotation
27th Mar 2007
C
k3
A
k1
D
B
k2
C
k3
A
k1
DB
k2
![Page 15: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/15.jpg)
Double Rotation
27th Mar 2007
B
k1
D
k3
A
C
k2
B
k1
D
k3
A C
k2
![Page 16: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/16.jpg)
ExampleInsert 3 into the AVL tree
27th Mar 2007
3
11
8 20
4 16 27
8
8
11
4 20
3 16 27
![Page 17: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/17.jpg)
ExampleInsert 5 into the AVL tree
27th Mar 2007
5
11
8 20
4 16 27 8
11
5 20
4 16 27
8
![Page 18: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/18.jpg)
AVL Trees: Exercise
Insertion order:10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40,
5, 55
27th Mar 2007
![Page 19: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/19.jpg)
Remove Operation in AVL Tree Removing a node from an AVL Tree is the
same as removing from a binary search tree. However, it may unbalance the tree.
Similar to insertion, starting from the removed node we check all the nodes in the path up to the root for the first unbalance node.
Use the appropriate single or double rotation to balance the tree.
May need to continue searching for unbalanced nodes all the way to the root.
27th Mar 2007
![Page 20: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/20.jpg)
Deletion X in AVL Trees
Deletion:Case 1: if X is a leaf, delete XCase 2: if X has 1 child, use it to replace XCase 3: if X has 2 children, replace X with its
inorder predecessor (and recursively delete it)
Rebalancing
27th Mar 2007
![Page 21: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/21.jpg)
Delete 55 (case 1)
27th Mar 2007
60
20 70
10 40 65 85
5 15 30 50 80 90
55
![Page 22: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/22.jpg)
Delete 55 (case 1)
27th Mar 2007
60
20 70
10 40 65 85
5 15 30 50 80 90
55
![Page 23: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/23.jpg)
Delete 50 (case 2)
27th Mar 2007
60
20 70
10 40 65 85
5 15 30 50 80 90
55
![Page 24: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/24.jpg)
Delete 50 (case 2)
27th Mar 2007
60
20 70
10 40 65 85
5 15 30 50 80 90
55
![Page 25: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/25.jpg)
Delete 60 (case 3)
27th Mar 2007
60
20 70
10 40 65 85
5 15 30 50 80 90
55
prev
![Page 26: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/26.jpg)
Delete 60 (case 3)
27th Mar 2007
55
20 70
10 40 65 85
5 15 30 50 80 90
![Page 27: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/27.jpg)
Delete 55 (case 3)
27th Mar 2007
55
20 70
10 40 65 85
5 15 30 50 80 90
prev
![Page 28: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/28.jpg)
Delete 55 (case 3)
27th Mar 2007
50
20 70
10 40 65 85
5 15 30 80 90
![Page 29: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/29.jpg)
Delete 50 (case 3)
27th Mar 2007
50
20 70
10 40 65 85
5 15 30 80 90
prev
![Page 30: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/30.jpg)
Delete 50 (case 3)
27th Mar 2007
40
20 70
10 30 65 85
5 15 80 90
![Page 31: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/31.jpg)
Delete 40 (case 3)
27th Mar 2007
40
20 70
10 30 65 85
5 15 80 90
prev
![Page 32: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/32.jpg)
Delete 40 : Rebalancing
27th Mar 2007
30
20 70
10 65 85
5 15 80 90
Case ?
![Page 33: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/33.jpg)
Delete 40: after rebalancing
27th Mar 2007
30
7010
20 65 855
15 80 90
Single rotation is preferred!
![Page 34: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/34.jpg)
Minimum Element in AVL Tree An AVL Tree of height H has at least FH+3-1 nodes,
where Fi is the i-th fibonacci number S0 = 1 S1 = 2 SH = SH-1 + SH-2 + 1
27th Mar 2007
SH-1
SH-2
H
H-1H-2
![Page 35: AVL Tree](https://reader035.vdocument.in/reader035/viewer/2022070404/56813b0e550346895da3b687/html5/thumbnails/35.jpg)
Summary
Find element, insert element, and remove element operations all have complexity O(log n) for worst case
Insert operation: top-down insertion and bottom up balancing
27th Mar 2007