Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
data structure on bca.
1. 1Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th
Mar 2007
AVL Tree
Definition
Properties
Operations
Outline
2. 2Ruli Manurung (Fasilkom UI) IKI10100: Lecture
Balanced binary tree
The disadvantage of a binary search tree is that its
height can be as large as N-1
This means that the time needed to perform insertion
and deletion and many other operations can be O(N)
in the worst case
We want a tree with small height
A binary tree with N node has height at least Θ(log
N)
Thus, our goal is to keep the height of a binary
search tree O(log N)
Such trees are called balanced binary search trees.
Examples are AVL tree, red-black tree.
3. 3Ruli Manurung (Fasilkom UI) IKI10100: Lecture
AVL tree
An AVL tree is a binary search tree in which
for every node in the tree, the height of the left and
right subtrees differ by at most 1.
AVL property
violated here
4. 4Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th
Mar 2007
X X
AVL Trees
Unbalanced Binary Search Trees are bad. Worst
case: operations take O(n).
AVL (Adelson-Velskii & Landis) trees maintain
balance.
For each node in tree, height of left subtree and
height of right subtree differ by a maximum of 1.
H
H-1
H-2
7. 7Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th
Mar 2007
12
8 16
4 10
2 6
14
1
Insertion for AVL Tree
After insert 1
8. 8Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th
Mar 2007
Insertion for AVL Tree
To ensure balance condition for AVL-tree, after
insertion of a new node, we back up the path from the
inserted node to root and check the balance
condition for each node.
If after insertion, the balance condition does not hold
in a certain node, we do one of the following
rotations:
Single rotation
Double rotation
9. 9Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th
Mar 2007
Insertions Causing Imbalance
R
P
Q
k1
k2
Q
k2
P
k1
R
An insertion into the subtree:
P (outside) - case 1
Q (inside) - case 2
An insertion into the subtree:
Q (inside) - case 3
R (outside) - case 4
HP=HQ=HR
10. 10Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th
Mar 2007
A
k2
B
k1
C
CB
A
k1
k2
Single Rotation (case 1)
HA=HB+1
HB=HC
11. 11Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th
Mar 2007
Single Rotation (case 4)
C
k1
B
k2
A
A B C
k2
k1
HA=HB
HC=HB+1
12. 12Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th
Mar 2007
Q
k2
P
k1
R
R
P
Q
k1
k2
Problem with Single Rotation
Single rotation does not work for case 2 and 3 (inside
case)
HQ=HP+1
HP=HR
13. 13Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th
Mar 2007
C
k3
A
k1
D
B
k2
Double Rotation: Step
C
k3
A
k1
D
B
k2
HA=HB=HC=HD
20. 20Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th
Mar 2007
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.
21. 21Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th
Mar 2007
Deletion X in AVL Trees
Deletion:
Case 1: if X is a leaf, delete X
Case 2: if X has 1 child, use it to replace X
Case 3: if X has 2 children, replace X with its inorder
predecessor (and recursively delete it)
Rebalancing
34. 34Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th
Mar 2007
Delete 40: after rebalancing
30
7010
20 65 855
15 80 90
Single rotation is preferred!
35. 35Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th
Mar 2007
AVL Tree:
The depth of AVL Trees is at most logarithmic.
So, all of the operations on AVL trees are also
logarithmic.
Find element, insert element, and remove element
operations all have complexity O(log n) for worst
case
The worst-case height is at most 44 percent more
than the minimum possible for binary trees.