SlideShare a Scribd company logo
AVL Trees
AVL Trees
AVL Trees
Balance
Balance == height(left subtree) - height(right subtree)
 zero everywhere  perfectly balanced
 small everywhere  balanced enough
Balance between -1 and 1 everywhere
 maximum height of ~1.44 log n
t
5
7
AVL Trees
AVL Tree
Dictionary Data Structure
4
12
10
6
2
11
5
8
14
13
7 9
Binary search tree properties
 binary tree property
 search tree property
Balance property
 balance of every node is:
-1 b  1
 result:
 depth is (log n)
15
AVL Trees
An AVL Tree
15
9
2 12
5
10
20
17
0
0
1
0
0
1 2
3 10
3
data
height
children
30
0
AVL Trees
Not AVL Trees
15
12
5
10
20
17
0
1
0
0 2
3
30
0
15
10
20
0
1
2 (-1)-1 = -2
0-2 = -2
Note: height(empty tree) == -1
AVL Trees
Good Insert Case:
Balance Preserved
Good case: insert middle, then small,then tall
Insert(middle)
Insert(small)
Insert(tall)
M
S T
0
0
1
AVL Trees
Bad Insert Case #1:
Left-Left or Right-Right Imbalance
Insert(small)
Insert(middle)
Insert(tall)
T
M
S
0
1
2
BC#1 Imbalance caused by either:
 Insert into left child’s left subtree
 Insert into right child’s right subtree
AVL Trees
Single Rotation
T
M
S
0
1
2
M
S T
0
0
1
Basic operation used in AVL trees:
A right child could legally have its parent as its left child.
AVL Trees
General Bad Case #1
a
X
Y
b
Z
h h - 1
h + 1 h - 1
h + 2
a
X
Y
b
Z
h-1 h - 1
h h - 1
h + 1
Note: imbalance is left-left
AVL Trees
Single Rotation
Fixes Case #1 Imbalance
 Height of left subtree same as it was before insert!
 Height of all ancestors unchanged
 We can stop here!
a
X
Y
b
Z
a
X
Y
b
Z
h h - 1
h + 1 h - 1
h + 2
h
h - 1
h
h - 1
h + 1
AVL Trees
Bad Insert Case #2:
Left-Right or Right-Left Imbalance
Insert(small)
Insert(tall)
Insert(middle)
M
T
S
0
1
2
Will a single rotation fix this?
BC#2 Imbalance caused by either:
 Insert into left child’s right subtree
 Insert into right child’s left subtree
AVL Trees
Double Rotation
M
S T
0
0
1
M
T
S
0
1
2
T
M
S
0
1
2
AVL Trees
General Bad Case #2
a
X
b
Z
h
h + 1 h - 1
h + 2
a
X
b
Z
h-1 h - 1
h h - 1
h + 1
Note: imbalance is left-right
h-1
Y
Y
AVL Trees
Double Rotation
Fixes Case #2 Imbalance
Initially: insert into either X or Y unbalances tree (root balance goes to 2 or -2)
“Zig zag” to pull up c – restores root height to h+1, left subtree height to h
a
Z
b
W
c
X Y
a
Z
b
W
c
X Y
h
h - 1?
h - 1
h - 1
h + 2
h + 1
h - 1
h - 1
h
h + 1
h
h - 1?
AVL Trees
AVL Insert Algorithm
 Find spot for value
 Hang new node
 Search back up looking for imbalance
 If there is an imbalance:
case #1: Perform single rotation
case #2: Perform double rotation
 Done!
(There can only be one imbalance!)
AVL Trees
Easy Insert
20
9
2
15
5
10
30
17
Insert(3)
12
0
0
1
0
0
1 2
3
0
AVL Trees
Hard Insert (Bad Case #1)
20
9
2
15
5
10
30
17
Insert(33)
3
12
1
0
1
0
0
2 2
3
0
0
AVL Trees
Single Rotation
20
9
2
15
5
10
30
17
3
12
33
1
0
2
0
0
2 3
3
1
0
0
30
9
2
20
5
10
33
3
15
1
0
1
1
0
2 2
3
0
0
17
12
0
AVL Trees
Hard Insert (Bad Case #2)
Insert(18)
20
9
2
15
5
10
30
17
3
12
1
0
1
0
0
2 2
3
0
0
AVL Trees
Single Rotation (oops!)
20
9
2
15
5
10
30
17
3
12
1
1
2
0
0
2 3
3
0
0
30
9
2
20
5
10
3
15
1
1
0
2
0
2 3
3
0
17
12
0
18
0
18
0
AVL Trees
Double Rotation (Step #1)
20
9
2
15
5
10
30
17
3
12
1
1
2
0
0
2 3
3
0
0
18
0
17
9
2
15
5
10
20
3
12
1 2
0
0
2 3
3
1
0
30
0
Look familiar? 18
0
AVL Trees
Double Rotation (Step #2)
17
9
2
15
5
10
20
3
12
1 2
0
0
2 3
3
1
0
30
0
18
0
20
9
2
17
5
10
30
3
15
1
0
1
1
0
2 2
3
0
0
12
0
18
AVL Trees
AVL Insert Algorithm Revisited
 Recursive
1. Search downward for
spot
2. Insert node
3. Unwind stack,
correcting heights
a. If imbalance #1,
single rotate
b. If imbalance #2,
double rotate
 Iterative
1. Search downward for
spot, stacking
parent nodes
2. Insert node
3. Unwind stack,
correcting heights
a. If imbalance #1,
single rotate and
exit
b. If imbalance #2,
double rotate and
exit
AVL Trees
Single Rotation Code
void RotateRight(Node *& root) {
Node * temp = root->right;
root->right = temp->left;
temp->left = root;
root->height =
max(root->right->height,
root->left->height) + 1;
temp->height =
max(temp->right->height,
temp->left->height) + 1;
root = temp;
}
X
Y
Z
root
temp
AVL Trees
Double Rotation Code
void DoubleRotateRight(Node *& root) {
RotateLeft(root->right);
RotateRight(root);
}
a
Z
b
W
c
X
Y
a
Z
c
b
X
Y
W
First Rotation
AVL Trees
Double Rotation Completed
a
Z
c
b
X
Y
W
a
Z
c
b
X
Y
W
First Rotation Second Rotation
AVL Trees
Deletion: Really Easy Case
20
9
2
15
5
10
30
17
3
12
1
0
1
0
0
2 2
3
0
0
Delete(17)
AVL Trees
Deletion: Pretty Easy Case
20
9
2
15
5
10
30
17
3
12
1
0
1
0
0
2 2
3
0
0
Delete(15)
AVL Trees
Deletion: Pretty Easy Case (cont.)
20
9
2
17
5
10
30
3
12
1 1
0
0
2 2
3
0
0
Delete(15)
AVL Trees
Deletion (Hard Case #1)
20
9
2
17
5
10
30
3
12
1 1
0
0
2 2
3
0
0
Delete(12)
AVL Trees
Single Rotation on Deletion
20
9
2
17
5
10
30
3
1 1
0
2 2
3
0
0
30
9
2
20
5
10
17
3
1 0
0
2 1
3
0
0
Deletion can differ from insertion – How?
AVL Trees
Deletion (Hard Case)
Delete(9)
20
9
2
17
5
10
30
3
12
1 2
2
0
2 3
4
0
33
15
13
0 0
1
0
20
30
12
33
15
13
1
0 0
11
0
18
0
AVL Trees
Double Rotation on Deletion
2
3
0
20
2
17
5
10
30
12
1 2
2
2 3
4
33
15
13
1
0 0
1
11
0
18
0
20
5
2
17
3
10
30
12
0 2
2
0
1 3
4
33
15
13
1
0 0
1
11
0
18
0
0
Not
finished!
AVL Trees
Deletion with Propagation
We get to choose whether
to single or double rotate!
20
5
2
17
3
10
30
12
0 2
2
0
1 3
4
33
15
13
1
0 0
1
11
0
18
0
What different about this case?
AVL Trees
Propagated Single Rotation
0
30
20
17
33
12
15
13
1
0
5
2
3
10
4
3 2
1 2 1
0 0 0
11
0
20
5
2
17
3
10
30
12
0 2
2
0
1 3
4
33
15
13
1
0
1
11
0
18
0
18
0
AVL Trees
Propagated Double Rotation
0
17
12
11
5
2
3
10
4
2 3
1 0
0 0
20
5
2
17
3
10
30
12
0 2
2
0
1 3
4
33
15
13
1
0
1
11
0
18
0
15
1
0
20
30
33
1
18
0
13
0
2
AVL Trees
AVL Deletion Algorithm
 Recursive
1. Search downward for
node
2. Delete node
3. Unwind, correcting
heights as we go
a. If imbalance #1,
single rotate
b. If imbalance #2
(or don’t care),
double rotate
 Iterative
1. Search downward for
node, stacking
parent nodes
2. Delete node
3. Unwind stack,
correcting heights
a. If imbalance #1,
single rotate
b. If imbalance #2
(or don’t care)
double rotate
AVL Trees
Building an AVL Tree
Input: sequence of n keys (unordered)
19 3 4 18 7
Insert each into initially empty AVL tree
But, suppose input is already sorted …
3 4 7 18 19
Can we do better than O(n log n)?
1 1
( lo
l g )
og log
n n
i i
O n n
i n
 
 
 
AVL Trees
AVL BuildTree
8 10 15 20 30 35 40
5
17
17
8 1015
5 20303540
Divide & Conquer
 Divide the problem into
parts
 Solve each part recursively
 Merge the parts into a
general solution
How long does
divide & conquer take?
AVL Trees
BuildTree Example
35
17
15
5
8
10
3
2 2
1 0
0
30
1
40
20
0
0
8 10 15 20 30 35 40
5 17
8 10 15
5
8
5
30 35 40
20
30
20
AVL Trees
BuildTree Analysis
(Approximate)
T(1) = 1
T(n) = 2T(n/2) + 1
T(n) = 2(2T(n/4)+1) + 1
T(n) = 4T(n/4) + 2 + 1
T(n) = 4(2T(n/8)+1) + 2 + 1
T(n) = 8T(n/8) + 4 + 2 + 1
T(n) = 2kT(n/2k) +
let 2k = n, log n = k
T(n) = nT(1) +
T(n) = (n)


k
i
i
1
2
2
1


n
i
i
log
1
2
2
1
AVL Trees
Precise Analysis: T(0) = b
T(n) = T( ) + T( ) + c
By induction on n:
T(n) = (b+c)n + b
Base case:
T(0) = b = (b+c)0 + b
Induction step:
T(n) = (b+c) + b +
(b+c) + b + c
= (b+c)n + b
QED: T(n) = (b+c)n + b = (n)





 
2
1
n





 
2
1
n





 
2
1
n





 
2
1
n
BuildTree Analysis
(Exact)
1
2
1
2
1







 






 
n
n
n
AVL Trees
Thinking About AVL
Observations
+ Worst case height of an AVL tree is about 1.44 log n
+ Insert, Find, Delete in worst case O(log n)
+ Only one (single or double) rotation needed on
insertion
+ Compatible with lazy deletion
- O(log n) rotations needed on deletion
- Height fields must be maintained (or 2-bit balance)
Coding complexity?
AVL Trees
Alternatives to AVL Trees
 Weight balanced trees
 keep about the same number of nodes in each subtree
 not nearly as nice
 Splay trees
 “blind” adjusting version of AVL trees
 no height information maintained!
 insert/find always rotates node to the root!
 worst case time is O(n)
 amortized time for all operations is O(log n)
 mysterious, but often faster than AVL trees in practice
(better low-order terms)

More Related Content

Similar to AVL Tree.ppt

Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
banupriyar5
 
Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptx
MalligaarjunanN
 
Avl trees final
Avl trees finalAvl trees final
Avl trees final
PRAKASH RANJAN SINGH
 
AVL-TREE.ppt
AVL-TREE.pptAVL-TREE.ppt
AVL-TREE.ppt
Pran K Mohanty
 
Ie
IeIe
Design data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sirDesign data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sir
22001003058
 
Avl tree detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
Dr Sandeep Kumar Poonia
 
4. avl
4. avl4. avl
Avl trees
Avl treesAvl trees
Avl trees
amna izzat
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
ecomputernotes
 
Lectures on Digital Dommunication by Prof.Dr.A.Abbas Khan
Lectures on Digital Dommunication by Prof.Dr.A.Abbas KhanLectures on Digital Dommunication by Prof.Dr.A.Abbas Khan
Lectures on Digital Dommunication by Prof.Dr.A.Abbas Khan
ProfArshadAbbas
 
Lectures on digital communication by prof.dr.a.abbas
Lectures on digital communication by prof.dr.a.abbasLectures on digital communication by prof.dr.a.abbas
Lectures on digital communication by prof.dr.a.abbas
ProfArshadAbbas
 
AVL_Trees.ppt
AVL_Trees.pptAVL_Trees.ppt
AVL_Trees.ppt
SaqibShigri
 
AVL TREE java dalam penerapannya dan aplikasinya
AVL TREE java dalam penerapannya dan aplikasinyaAVL TREE java dalam penerapannya dan aplikasinya
AVL TREE java dalam penerapannya dan aplikasinya
bagusciptapratama
 
Review session2
Review session2Review session2
Review session2
NEEDY12345
 
Jurnal informatika
Jurnal informatika Jurnal informatika
Jurnal informatika
MamaMa28
 
AVL Trees
AVL TreesAVL Trees
AVL Trees
Shreyans Pathak
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
9 chapter4 trees_avl
9 chapter4 trees_avl9 chapter4 trees_avl
9 chapter4 trees_avl
SSE_AndyLi
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
zukun
 

Similar to AVL Tree.ppt (20)

Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
 
Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptx
 
Avl trees final
Avl trees finalAvl trees final
Avl trees final
 
AVL-TREE.ppt
AVL-TREE.pptAVL-TREE.ppt
AVL-TREE.ppt
 
Ie
IeIe
Ie
 
Design data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sirDesign data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sir
 
Avl tree detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
 
4. avl
4. avl4. avl
4. avl
 
Avl trees
Avl treesAvl trees
Avl trees
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
 
Lectures on Digital Dommunication by Prof.Dr.A.Abbas Khan
Lectures on Digital Dommunication by Prof.Dr.A.Abbas KhanLectures on Digital Dommunication by Prof.Dr.A.Abbas Khan
Lectures on Digital Dommunication by Prof.Dr.A.Abbas Khan
 
Lectures on digital communication by prof.dr.a.abbas
Lectures on digital communication by prof.dr.a.abbasLectures on digital communication by prof.dr.a.abbas
Lectures on digital communication by prof.dr.a.abbas
 
AVL_Trees.ppt
AVL_Trees.pptAVL_Trees.ppt
AVL_Trees.ppt
 
AVL TREE java dalam penerapannya dan aplikasinya
AVL TREE java dalam penerapannya dan aplikasinyaAVL TREE java dalam penerapannya dan aplikasinya
AVL TREE java dalam penerapannya dan aplikasinya
 
Review session2
Review session2Review session2
Review session2
 
Jurnal informatika
Jurnal informatika Jurnal informatika
Jurnal informatika
 
AVL Trees
AVL TreesAVL Trees
AVL Trees
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
9 chapter4 trees_avl
9 chapter4 trees_avl9 chapter4 trees_avl
9 chapter4 trees_avl
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 

Recently uploaded

按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理
按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理
按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理
evnum
 
Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdfSwitching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
SocMediaFin - Joyce Sullivan
 
A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024
Bruce Bennett
 
欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】
欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】
欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】
mukeshomran942
 
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
GabrielleSinaga
 
Khushi Saini, An Intern from The Sparks Foundation
Khushi Saini, An Intern from The Sparks FoundationKhushi Saini, An Intern from The Sparks Foundation
Khushi Saini, An Intern from The Sparks Foundation
khushisaini0924
 
办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样
办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样
办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样
kkkkr4pg
 
体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】
体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】
体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】
waldorfnorma258
 
欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】
欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】
欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】
karimimorine448
 
美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】
美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】
美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】
balliuvilla512
 
按照学校原版(UofT文凭证书)多伦多大学毕业证快速办理
按照学校原版(UofT文凭证书)多伦多大学毕业证快速办理按照学校原版(UofT文凭证书)多伦多大学毕业证快速办理
按照学校原版(UofT文凭证书)多伦多大学毕业证快速办理
evnum
 
LinkedIn for Your Job Search June 17, 2024
LinkedIn for Your Job Search June  17, 2024LinkedIn for Your Job Search June  17, 2024
LinkedIn for Your Job Search June 17, 2024
Bruce Bennett
 
一比一原版坎特伯雷大学毕业证(UC毕业证书)学历如何办理
一比一原版坎特伯雷大学毕业证(UC毕业证书)学历如何办理一比一原版坎特伯雷大学毕业证(UC毕业证书)学历如何办理
一比一原版坎特伯雷大学毕业证(UC毕业证书)学历如何办理
cenaws
 
在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样
在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样
在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样
yhkox
 
欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】
欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】
欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】
ahmedendrise81
 
Community Skills Building Workshop | PMI Silver Spring Chapter | June 12, 2024
Community Skills Building Workshop | PMI Silver Spring Chapter  | June 12, 2024Community Skills Building Workshop | PMI Silver Spring Chapter  | June 12, 2024
Community Skills Building Workshop | PMI Silver Spring Chapter | June 12, 2024
Hector Del Castillo, CPM, CPMM
 
一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理
一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理
一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理
gnokue
 
Learnings from Successful Jobs Searchers
Learnings from Successful Jobs SearchersLearnings from Successful Jobs Searchers
Learnings from Successful Jobs Searchers
Bruce Bennett
 
美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】
美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】
美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】
ahmedendrise81
 
一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理
一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理
一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理
aweuwyo
 

Recently uploaded (20)

按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理
按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理
按照学校原版(ArtEZ文凭证书)ArtEZ艺术学院毕业证快速办理
 
Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdfSwitching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
 
A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024
 
欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】
欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】
欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】
 
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
 
Khushi Saini, An Intern from The Sparks Foundation
Khushi Saini, An Intern from The Sparks FoundationKhushi Saini, An Intern from The Sparks Foundation
Khushi Saini, An Intern from The Sparks Foundation
 
办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样
办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样
办理阿卡迪亚大学毕业证(uvic毕业证)本科文凭证书原版一模一样
 
体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】
体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】
体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】
 
欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】
欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】
欧洲杯外围-欧洲杯外围赛程-欧洲杯外围压注|【​网址​🎉ac99.net🎉​】
 
美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】
美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】
美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】
 
按照学校原版(UofT文凭证书)多伦多大学毕业证快速办理
按照学校原版(UofT文凭证书)多伦多大学毕业证快速办理按照学校原版(UofT文凭证书)多伦多大学毕业证快速办理
按照学校原版(UofT文凭证书)多伦多大学毕业证快速办理
 
LinkedIn for Your Job Search June 17, 2024
LinkedIn for Your Job Search June  17, 2024LinkedIn for Your Job Search June  17, 2024
LinkedIn for Your Job Search June 17, 2024
 
一比一原版坎特伯雷大学毕业证(UC毕业证书)学历如何办理
一比一原版坎特伯雷大学毕业证(UC毕业证书)学历如何办理一比一原版坎特伯雷大学毕业证(UC毕业证书)学历如何办理
一比一原版坎特伯雷大学毕业证(UC毕业证书)学历如何办理
 
在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样
在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样
在线办理(UOIT毕业证书)安大略省理工大学毕业证在读证明一模一样
 
欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】
欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】
欧洲杯买球-欧洲杯买球买球推荐-欧洲杯买球买球推荐网站|【​网址​🎉ac10.net🎉​】
 
Community Skills Building Workshop | PMI Silver Spring Chapter | June 12, 2024
Community Skills Building Workshop | PMI Silver Spring Chapter  | June 12, 2024Community Skills Building Workshop | PMI Silver Spring Chapter  | June 12, 2024
Community Skills Building Workshop | PMI Silver Spring Chapter | June 12, 2024
 
一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理
一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理
一比一原版(surrey毕业证书)英国萨里大学毕业证成绩单修改如何办理
 
Learnings from Successful Jobs Searchers
Learnings from Successful Jobs SearchersLearnings from Successful Jobs Searchers
Learnings from Successful Jobs Searchers
 
美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】
美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】
美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】
 
一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理
一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理
一比一原版(uwm毕业证书)美国威斯康星大学密尔沃基分校毕业证如何办理
 

AVL Tree.ppt

  • 2. AVL Trees Balance Balance == height(left subtree) - height(right subtree)  zero everywhere  perfectly balanced  small everywhere  balanced enough Balance between -1 and 1 everywhere  maximum height of ~1.44 log n t 5 7
  • 3. AVL Trees AVL Tree Dictionary Data Structure 4 12 10 6 2 11 5 8 14 13 7 9 Binary search tree properties  binary tree property  search tree property Balance property  balance of every node is: -1 b  1  result:  depth is (log n) 15
  • 4. AVL Trees An AVL Tree 15 9 2 12 5 10 20 17 0 0 1 0 0 1 2 3 10 3 data height children 30 0
  • 5. AVL Trees Not AVL Trees 15 12 5 10 20 17 0 1 0 0 2 3 30 0 15 10 20 0 1 2 (-1)-1 = -2 0-2 = -2 Note: height(empty tree) == -1
  • 6. AVL Trees Good Insert Case: Balance Preserved Good case: insert middle, then small,then tall Insert(middle) Insert(small) Insert(tall) M S T 0 0 1
  • 7. AVL Trees Bad Insert Case #1: Left-Left or Right-Right Imbalance Insert(small) Insert(middle) Insert(tall) T M S 0 1 2 BC#1 Imbalance caused by either:  Insert into left child’s left subtree  Insert into right child’s right subtree
  • 8. AVL Trees Single Rotation T M S 0 1 2 M S T 0 0 1 Basic operation used in AVL trees: A right child could legally have its parent as its left child.
  • 9. AVL Trees General Bad Case #1 a X Y b Z h h - 1 h + 1 h - 1 h + 2 a X Y b Z h-1 h - 1 h h - 1 h + 1 Note: imbalance is left-left
  • 10. AVL Trees Single Rotation Fixes Case #1 Imbalance  Height of left subtree same as it was before insert!  Height of all ancestors unchanged  We can stop here! a X Y b Z a X Y b Z h h - 1 h + 1 h - 1 h + 2 h h - 1 h h - 1 h + 1
  • 11. AVL Trees Bad Insert Case #2: Left-Right or Right-Left Imbalance Insert(small) Insert(tall) Insert(middle) M T S 0 1 2 Will a single rotation fix this? BC#2 Imbalance caused by either:  Insert into left child’s right subtree  Insert into right child’s left subtree
  • 12. AVL Trees Double Rotation M S T 0 0 1 M T S 0 1 2 T M S 0 1 2
  • 13. AVL Trees General Bad Case #2 a X b Z h h + 1 h - 1 h + 2 a X b Z h-1 h - 1 h h - 1 h + 1 Note: imbalance is left-right h-1 Y Y
  • 14. AVL Trees Double Rotation Fixes Case #2 Imbalance Initially: insert into either X or Y unbalances tree (root balance goes to 2 or -2) “Zig zag” to pull up c – restores root height to h+1, left subtree height to h a Z b W c X Y a Z b W c X Y h h - 1? h - 1 h - 1 h + 2 h + 1 h - 1 h - 1 h h + 1 h h - 1?
  • 15. AVL Trees AVL Insert Algorithm  Find spot for value  Hang new node  Search back up looking for imbalance  If there is an imbalance: case #1: Perform single rotation case #2: Perform double rotation  Done! (There can only be one imbalance!)
  • 17. AVL Trees Hard Insert (Bad Case #1) 20 9 2 15 5 10 30 17 Insert(33) 3 12 1 0 1 0 0 2 2 3 0 0
  • 18. AVL Trees Single Rotation 20 9 2 15 5 10 30 17 3 12 33 1 0 2 0 0 2 3 3 1 0 0 30 9 2 20 5 10 33 3 15 1 0 1 1 0 2 2 3 0 0 17 12 0
  • 19. AVL Trees Hard Insert (Bad Case #2) Insert(18) 20 9 2 15 5 10 30 17 3 12 1 0 1 0 0 2 2 3 0 0
  • 20. AVL Trees Single Rotation (oops!) 20 9 2 15 5 10 30 17 3 12 1 1 2 0 0 2 3 3 0 0 30 9 2 20 5 10 3 15 1 1 0 2 0 2 3 3 0 17 12 0 18 0 18 0
  • 21. AVL Trees Double Rotation (Step #1) 20 9 2 15 5 10 30 17 3 12 1 1 2 0 0 2 3 3 0 0 18 0 17 9 2 15 5 10 20 3 12 1 2 0 0 2 3 3 1 0 30 0 Look familiar? 18 0
  • 22. AVL Trees Double Rotation (Step #2) 17 9 2 15 5 10 20 3 12 1 2 0 0 2 3 3 1 0 30 0 18 0 20 9 2 17 5 10 30 3 15 1 0 1 1 0 2 2 3 0 0 12 0 18
  • 23. AVL Trees AVL Insert Algorithm Revisited  Recursive 1. Search downward for spot 2. Insert node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2, double rotate  Iterative 1. Search downward for spot, stacking parent nodes 2. Insert node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate and exit b. If imbalance #2, double rotate and exit
  • 24. AVL Trees Single Rotation Code void RotateRight(Node *& root) { Node * temp = root->right; root->right = temp->left; temp->left = root; root->height = max(root->right->height, root->left->height) + 1; temp->height = max(temp->right->height, temp->left->height) + 1; root = temp; } X Y Z root temp
  • 25. AVL Trees Double Rotation Code void DoubleRotateRight(Node *& root) { RotateLeft(root->right); RotateRight(root); } a Z b W c X Y a Z c b X Y W First Rotation
  • 26. AVL Trees Double Rotation Completed a Z c b X Y W a Z c b X Y W First Rotation Second Rotation
  • 27. AVL Trees Deletion: Really Easy Case 20 9 2 15 5 10 30 17 3 12 1 0 1 0 0 2 2 3 0 0 Delete(17)
  • 28. AVL Trees Deletion: Pretty Easy Case 20 9 2 15 5 10 30 17 3 12 1 0 1 0 0 2 2 3 0 0 Delete(15)
  • 29. AVL Trees Deletion: Pretty Easy Case (cont.) 20 9 2 17 5 10 30 3 12 1 1 0 0 2 2 3 0 0 Delete(15)
  • 30. AVL Trees Deletion (Hard Case #1) 20 9 2 17 5 10 30 3 12 1 1 0 0 2 2 3 0 0 Delete(12)
  • 31. AVL Trees Single Rotation on Deletion 20 9 2 17 5 10 30 3 1 1 0 2 2 3 0 0 30 9 2 20 5 10 17 3 1 0 0 2 1 3 0 0 Deletion can differ from insertion – How?
  • 32. AVL Trees Deletion (Hard Case) Delete(9) 20 9 2 17 5 10 30 3 12 1 2 2 0 2 3 4 0 33 15 13 0 0 1 0 20 30 12 33 15 13 1 0 0 11 0 18 0
  • 33. AVL Trees Double Rotation on Deletion 2 3 0 20 2 17 5 10 30 12 1 2 2 2 3 4 33 15 13 1 0 0 1 11 0 18 0 20 5 2 17 3 10 30 12 0 2 2 0 1 3 4 33 15 13 1 0 0 1 11 0 18 0 0 Not finished!
  • 34. AVL Trees Deletion with Propagation We get to choose whether to single or double rotate! 20 5 2 17 3 10 30 12 0 2 2 0 1 3 4 33 15 13 1 0 0 1 11 0 18 0 What different about this case?
  • 35. AVL Trees Propagated Single Rotation 0 30 20 17 33 12 15 13 1 0 5 2 3 10 4 3 2 1 2 1 0 0 0 11 0 20 5 2 17 3 10 30 12 0 2 2 0 1 3 4 33 15 13 1 0 1 11 0 18 0 18 0
  • 36. AVL Trees Propagated Double Rotation 0 17 12 11 5 2 3 10 4 2 3 1 0 0 0 20 5 2 17 3 10 30 12 0 2 2 0 1 3 4 33 15 13 1 0 1 11 0 18 0 15 1 0 20 30 33 1 18 0 13 0 2
  • 37. AVL Trees AVL Deletion Algorithm  Recursive 1. Search downward for node 2. Delete node 3. Unwind, correcting heights as we go a. If imbalance #1, single rotate b. If imbalance #2 (or don’t care), double rotate  Iterative 1. Search downward for node, stacking parent nodes 2. Delete node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2 (or don’t care) double rotate
  • 38. AVL Trees Building an AVL Tree Input: sequence of n keys (unordered) 19 3 4 18 7 Insert each into initially empty AVL tree But, suppose input is already sorted … 3 4 7 18 19 Can we do better than O(n log n)? 1 1 ( lo l g ) og log n n i i O n n i n      
  • 39. AVL Trees AVL BuildTree 8 10 15 20 30 35 40 5 17 17 8 1015 5 20303540 Divide & Conquer  Divide the problem into parts  Solve each part recursively  Merge the parts into a general solution How long does divide & conquer take?
  • 40. AVL Trees BuildTree Example 35 17 15 5 8 10 3 2 2 1 0 0 30 1 40 20 0 0 8 10 15 20 30 35 40 5 17 8 10 15 5 8 5 30 35 40 20 30 20
  • 41. AVL Trees BuildTree Analysis (Approximate) T(1) = 1 T(n) = 2T(n/2) + 1 T(n) = 2(2T(n/4)+1) + 1 T(n) = 4T(n/4) + 2 + 1 T(n) = 4(2T(n/8)+1) + 2 + 1 T(n) = 8T(n/8) + 4 + 2 + 1 T(n) = 2kT(n/2k) + let 2k = n, log n = k T(n) = nT(1) + T(n) = (n)   k i i 1 2 2 1   n i i log 1 2 2 1
  • 42. AVL Trees Precise Analysis: T(0) = b T(n) = T( ) + T( ) + c By induction on n: T(n) = (b+c)n + b Base case: T(0) = b = (b+c)0 + b Induction step: T(n) = (b+c) + b + (b+c) + b + c = (b+c)n + b QED: T(n) = (b+c)n + b = (n)        2 1 n        2 1 n        2 1 n        2 1 n BuildTree Analysis (Exact) 1 2 1 2 1                  n n n
  • 43. AVL Trees Thinking About AVL Observations + Worst case height of an AVL tree is about 1.44 log n + Insert, Find, Delete in worst case O(log n) + Only one (single or double) rotation needed on insertion + Compatible with lazy deletion - O(log n) rotations needed on deletion - Height fields must be maintained (or 2-bit balance) Coding complexity?
  • 44. AVL Trees Alternatives to AVL Trees  Weight balanced trees  keep about the same number of nodes in each subtree  not nearly as nice  Splay trees  “blind” adjusting version of AVL trees  no height information maintained!  insert/find always rotates node to the root!  worst case time is O(n)  amortized time for all operations is O(log n)  mysterious, but often faster than AVL trees in practice (better low-order terms)