1 / 11

CS 253: Algorithms

CS 253: Algorithms. Chapter 13 Balanced Binary Search Trees (Balanced BST ) AVL Trees. 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 )

charis
Download Presentation

CS 253: Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Binary Search Trees - Summary • Operations on binary search trees: • SEARCHO(h) • PREDECESSORO(h) • SUCCESSORO(h) • MINIMUMO(h) • MAXIMUMO(h) • INSERTO(h) • DELETEO(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. 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.

  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 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 nhvalues 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 i.e. nhat 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)

  7. Trinode restructuring • involves a node, x, which has a parent, y, and a grandparent, z. • A trinoderestructure 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. 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 subtreesof c, respectively.

  9. 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 subtreenow rooted at the node b are more balanced.

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

More Related