download report

Transcript Lec-13-AVL-Trees

CS 253: Algorithms
Chapter 13
Balanced Binary Search Trees (Balanced BST)
AVL Trees
Binary Search Trees - Summary
Operations on binary search trees:
These operations are fast if the height of the tree is small
Theorem 12.4
The expected height of a randomly built binary search tree
on n distinct keys is O(lgn)
Can we make sure that
h = O(lgn)
• To achieve logarithmic performance for dictionary operations
(Search, Predecessor, Successor, Insert/Delete, Min/Max), we need
to guarantee that we have a balanced Binary Search Tree (BST).
• In a balanced BST, we can afford to increase the number of nodes
exponentially (e.g. 2k, because each op takes O(log(2k)) = O(k))
• In general, we refer to BSTs that achieve this performance through
some kinds of balancing rules or actions as balanced search trees.
AVL Trees
Height-balance Property:
For every internal node v of T,
the heights of the children of v can differ by at most 1.
An AVL Tree Example:
AVL Trees
A non-AVL Tree Example:
The height of an AVL tree, T, storing n items is O(log n).
Let’s call nh to be the minimum number of internal nodes of an AVL tree with height h.
Base cases:
n1 = 1, an AVL tree of height 1 must have at least one internal node
n2 = 2, an AVL tree of height 2 must have at least two internal nodes.
General case for height, h ≥ 3:
nh = 1 + nh−1 + nh−2
Note that nh values are strictly increasing as h increases (similar to the Fibonacci sequence).
In other words, nh−1 > nh−2, for h ≥ 3,
which allows us to simplify the above formula as:
nh > 2nh−2
nh at least doubles each time h increases by 2. Which implies that
nh > 2k *nh-2k
(choose h-2k = 2)
nh > 2h/2
By taking logarithms of both sides, we obtain
log nh > h/2
h < 2 log nh < 2 log n
Therefore, an AVL tree with n keys has height h < 2 log n  h = O(log n)
Trinode restructuring
• involves a node, x, which has a parent, y, and a grandparent, z.
• A trinode restructure temporarily renames the nodes x, y, and z as a, b,
and c, so that a precedes b and b precedes c in an inorder traversal of T.
Algorithm restructure(x):
A node x of a BST T that has both a parent y and a grandparent z
Tree T after a trinode restructuring (which corresponds to a single or
double rotation) involving nodes x, y, and z
1: Let (a, b, c) be a left-to-right (inorder) listing of the nodes x, y, and z, and let (T0, T1, T2, T3)
be a left-to-right (inorder) listing of the four subtrees of x, y, and z not rooted at x, y, or z.
2: Replace the subtree rooted at z with a new subtree rooted at b.
3: Let a be the left child of b and let T0 and T1 be the left and right subtrees of a, respectively.
4: Let c be the right child of b and let T2 and T3 be the left and right subtrees of c, respectively.
• The trinode restructure method modifies parent-child relationships of O(1) nodes
in T, while preserving the inorder traversal ordering of all the nodes in T.
• In addition to its order-preserving property, a trinode restructuring operation
changes the heights of nodes in T, so as to restore balance.
• restructure(x) is typically executed when z, the grandparent of x, is unbalanced.
Moreover, this unbalance is due to one of the children of x now having too large a
height relative to the height of z’s other child.
• Move up the “tall” child of x while pushing down the “short” child of z. Thus,
after performing restructure(x), all the nodes in the subtree now rooted at the node
b are more balanced.
Trinode restructuring example for an AVL Tree
• involves a node, x, which has a parent, y, and a grandparent, z.