Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.

Similar presentations


Presentation on theme: "CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees."— Presentation transcript:

1 CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees

2 2 Binary Search Trees - Summary Operations on binary search trees: ◦ SEARCH O(h) ◦ PREDECESSOR O(h) ◦ SUCCESSOR O(h) ◦ MINIMUM O(h) ◦ MAXIMUM O(h) ◦ INSERT O(h) ◦ DELETE O(h) 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) ?

3 Balance 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. 2 k, because each op takes O(log(2 k )) = O(k)) In general, we refer to BSTs that achieve this performance through some kinds of balancing rules or actions as balanced search trees.

4 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:

5 AVL Trees A non-AVL Tree Example:

6 Theorem: The height of an AVL tree, T, storing n items is O(log n). Proof: Let’s call n h to be the minimum number of internal nodes of an AVL tree with height h. Base cases: n 1 = 1, an AVL tree of height 1 must have at least one internal node n 2 = 2, an AVL tree of height 2 must have at least two internal nodes. General case for height, h ≥ 3: n h = 1 + n h−1 + n h−2 Note that n h values are strictly increasing as h increases (similar to the Fibonacci sequence). In other words, n h−1 > n h−2, for h ≥ 3, which allows us to simplify the above formula as: n h > 2n h−2 i.e. n h at least doubles each time h increases by 2. Which implies that n h > 2 k *n h-2k (choose h-2k = 2) n h > 2 h/2 By taking logarithms of both sides, we obtain log n h > h/2  h < 2 log n h < 2 log n Therefore, an AVL tree with n keys has height h < 2 log n  h = O(log n)

7 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.

8

9 Algorithm restructure(x): Input: A node x of a BST T that has both a parent y and a grandparent z Output: 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.

10 Rotations 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.

11 Trinode restructuring example for an AVL Tree involves a node, x, which has a parent, y, and a grandparent, z.


Download ppt "CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees."

Similar presentations


Ads by Google