Professional Documents
Culture Documents
An AVL Tree (Adelson-Velskii and Landis Tree) is a binary search tree with a balance condition. It must be easy to maintain, and it ensures that the depth of the tree is O (log N).
Concept
Lets try to insert 6 into the AVL tree below. This would destroy the AVL property of the tree. Then this property has to be restored before the insertion step is considered over.
It turns out that this can always be done with a simple modification to the tree known as rotation. After an insertion, only the nodes that are on the path from the insertion point to the root might have their balance altered. As we follow the path up to the root and update the balancing information there may exist nodes whose new balance violates the AVL condition.
Single Rotation(Left-Left)
Let k2 be the first node on the path up violating AVL balance property.Subtree X has grown an extra level (2 levels deeper than Z now). Y cannot be at the same level as X (k2 then out of balance before insertion) and Y cannot be at the same level as Z (then k1 would be the first to violate).
Single Rotation(Right-Right)
Let k1 be the first node on the path up violating AVL balance property.Subtree Z has grown an extra level (2 levels deeper than X now). Y cannot be at the same level as X (k1 then out of balance before insertion) and Y cannot be at the same level as Z (then k2 would be the first to violate).
10
Double Rotation(Left-Right)
Exactly one of tree B or C is 2 levels deeper than D (unless all empty). To rebalance, k3 cannot be root and a rotation between k1 and k3 was shown not to work. So the only alternative is to place k2 as the new root. This forces k1 to be k2s left child and k3 to be its right child. It also completely determines the locations of all 4 subtrees. AVL balance property is now satisfied. Old height of the tree is restored.
11
12
Double Rotation(Right-Left)
Exactly one of tree B or C is 2 levels deeper than A (unless all empty). To rebalance, k1 cannot be root and a rotation between k1 and k3 was shown not to work. So the only alternative is to place k2 as the new root. This forces k1 to be k3s right child and k2 to be its left child. It also completely determines the locations of all 4 subtrees. AVL balance property is now satisfied. Old height of the tree is restored.
13
14
15
Basic Operations
Searching Insertion Deletion
16
Searching
17
Insertion
18
19
Deletion
20
21
Applications
Searching in O(log n) time. Compression of Numerical data.
22