Upload
sai-avinash-duddupudi
View
219
Download
0
Embed Size (px)
DESCRIPTION
you haven't read an algorithms textbook or done a lot of self-studying of algorithms, you should definitely look through an algorithms textbook.Although most competitive programmers will agree that solving problems is one of the best ways to learn how to be a competitive programmer, it's very hard to get started by just solving problems.I started competitive programming in my sophomore year of high school. I quickly made it to the silver division of USACO, which essentially meant that I could solve ad hoc problems and had a decent grasp of techniques like recursion and so on. In order to proceed to the gold division of USACO, I would need to get a better grasp of more advanced algorithms. I would need to be better at dynamic programming and graph theory, most notably. I spent most of sophomore year being stagnant as I would get hit with problems that were way too hard for me. you haven't read an algorithms textbook or done a lot of self-studying of algorithms, you should definitely look through an algorithms textbook.Although most competitive programmers will agree that solving problems is one of the best ways to learn how to be a competitive programmer, it's very hard to get started by just solving problems.I started competitive programming in my sophomore year of high school. I quickly made it to the silver division of USACO, which essentially meant that I could solve ad hoc problems and had a decent grasp of techniques like recursion and so on. In order to proceed to the gold division of USACO, I would need to get a better grasp of more advanced algorithms. I would need to be better at dynamic programming and graph theory, most notably. I spent most of sophomore year being stagnant as I would get hit with problems that were way too hard for me. you haven't read an algorithms textbook or done a lot of self-studying of algorithms, you should definitely look through an algorithms textbook.Although most competitive programmers will agree that solving problems is one of the best ways to learn how to be a competitive programmer, it's very hard to get started by just solving problems.I started competitive programming in my sophomore year of high school. I quickly made it to the silver division of USACO, which essentially meant that I could solve ad hoc problems and had a decent grasp of techniques like recursion and so on. In order to proceed to the gold division of USACO, I would need to get a better grasp of more advanced algorithms. I would need to be better at dynamic programming and graph theory, most notably. I spent most of sophomore year being stagnant as I would get hit with problems that were way too hard for me. you haven't read an algorithms textbook or done a lot of self-studying of algorithms, you should definitely look through an algorithms textbook.Although most competitive programmers will agree that solving problems is one of the best ways to learn how to be a competitive programmer, it's very hard to get started by just solving problems.I started competitive programming in my sophomore year of high school. I quickly made it to the silver division of USACO, which essentially meant that I could solve ad hoc problems and had a decent grasp of techniques like recursion and so on. In order to proceed to the gold division of USACO, I would need to get a better grasp of more advanced algorithms. I would need to be better at dynamic programming and graph theory, most notably. I spent most of sophomore year being stagnant as I would get hit with problems that were way too hard for me. you haven't read an algorithms textbook or done a lot of self-studying of algorithms, you should definitely look through an algorithms textbook.Although most competitive programmers will agree that solving problems is one of the best ways to learn how to be a competitive programmer, it's very hard to get started by just solving problems.I started competitive programming in my sophomore year of high
Citation preview
AVL Trees
binary tree for every node x, define its balance factor
balance factor of x = height of left subtree of x height of right subtree of x
balance factor of every node x is 1, 0, or 1 log2 (n+1)
Example AVL Search Tree
0 0
0 0
1
0
-1 0
1
0
-1
1
-110
7
83
1 5
30
40
20
25
35
45
60
put(9)
0 0
0 0
1
0
-1 0
1
0
-1
1
-1
90
-1
0
10
7
83
1 5
30
40
20
25
35
45
60
put(29)
0 0
0 0
1
0
0
1
0
-1
1
-110
7
83
1 5
30
40
20
25
35
45
60
29
0
-1
-1-2
RR imbalance => new node is in right subtree of right subtree of white node (node with bf = 2)
0 0
0 0
1
0
0
1
0
-1
1
-110
7
83
1 5
30
40
25 35
45
600
RR rotation.20
029
put(29)
Insert/Put Following insert/put, retrace path towards root
and adjust balance factors as needed. Stop when you reach a node whose balance
factor becomes 0, 2, or 2, or when you reach the root.
The new tree is not an AVL tree only if you reach a node whose balance factor is either 2 or 2.
In this case, we say the tree has become unbalanced.
A-Node
Let A be the nearest ancestor of the newly inserted node whose balance factor becomes +2 or 2 following the insert.
Balance factor of nodes between new node and A is 0 before insertion.
Imbalance Types
RR newly inserted node is in the right subtree of the right subtree of A.
LL left subtree of left subtree of A. RL left subtree of right subtree of A. LR right subtree of left subtree of A.
LL rotation
LL Rotation
Subtree height is unchanged. No further adjustments to be done.
Before insertion.
1
0
A
B
BL BR
AR
h h
h
A
B
BL BR
AR
After insertion.h+1 h
h
B
A
After rotation.
BRh
ARh
BLh+1
0
0
1
2
Example
RR rotation
In an AVL search
After Inserting Node D
RR rotation
Example
LR Rotation (case 1)
Subtree height is unchanged. No further adjustments to be done.
Before insertion.
1
0
A
B
A
B
After insertion.
C
C
A
After rotation.
B
0
-1
2
0 0
0
LR Rotation (case 2)
Subtree height is unchanged. No further adjustments to be done.
C
A
CRh-1
ARh
1
0
A
B
BL
CR
AR
h
h-1
h0
CLh-1
C
A
B
BL
CR
AR
h
h-1
h
CLh
C
B
BLh
CLh
1
-1
2
0 -1
0
LR Rotation (case 3)
Subtree height is unchanged. No further adjustments to be done.
1
0
A
B
BL
CR
AR
h
h-1
h0
CLh-1
C
A
B
BL
CR
AR
h
h
h
CLh-1
C-1
-1
2 C
A
CRh
ARh
B
BLh
CLh-1
1 0
0
Example
RL Rotation
Single & Double Rotations
Single LL and RR
Double LR and RL LR is RR followed by LL RL is LL followed by RR
LR Is RR + LL
A
B
BL
CR
AR
h
h
h
CLh-1
C-1
-1
2
After insertion.
A
C
CL
CR
AR
h
h
BLh
B
2
After RR rotation.
h-1
C
A
CRh
ARh
B
BLh
CLh-1
1 0
0
After LL rotation.
Remove An Element
0 0
0 0
1
0
-1 0
1
0
-1
1
-110
7
83
1 5
30
40
20
25
35
45
60
Remove 8.
Remove An Element
0 0
0
2
0
-1 0
1
0
-1
1
-110
7
3
1 5
30
40
20
25
35
45
60
Let q be parent of deleted node.
Retrace path from q towards root.
q
New Balance Factor Of q
Deletion from left subtree of q => bf--. Deletion from right subtree of q => bf++. New balance factor = 1 or 1 => no change in height of
subtree rooted at q. New balance factor = 0 => height of subtree rooted at q
has decreased by 1. New balance factor = 2 or 2 => tree is unbalanced at q.
q
Imbalance Classification Let A be the nearest ancestor of the deleted
node whose balance factor has become 2 or 2following a deletion.
Deletion from left subtree of A => type L. Deletion from right subtree of A => type R. Type R => new bf(A) = 2. So, old bf(A) = 1. So, A has a left child B.
bf(B) = 0 => R0. bf(B) = 1 => R1. bf(B) = 1 => R-1.
R0 Rotation
Subtree height is unchanged. No further adjustments to be done. Similar to LL rotation.
Before deletion.
1
0
A
B
BL BR
AR
h h
h
B
A
After rotation.
BRh
ARh-1
BLh
1
-1A
B
BL BR
AR
After deletion.h h
h-10
2
R1 Rotation
Subtree height is reduced by 1. Must continue on path to root. Similar to LL and R0 rotations.
Before deletion.
1
1
A
B
BL BR
AR
h h-1
h
B
A
After rotation.
BRh-1
ARh-1
BLh
0
0A
B
BL BR
AR
After deletion.h h-1
h-11
2
R-1 Rotation
New balance factor of A and B depends on b. Subtree height is reduced by 1. Must continue on path to root. Similar to LR.
1
-1
A
B
BL
CR
AR
h-1
hb
CL
C
A
B
BL
CR
AR
h-1
h-1
CL
Cb
-1
2 C
A
CR ARh-1
B
BLh-1
CL
0
Number of Rebalancing Rotations
At most 1 for an insert. O(log n) for a delete.
Rotation Frequency
Insert random numbers. No rotation 53.4% (approx). LL/RR 23.3% (approx). LR/RL 23.2% (approx).