ps4sol

ps4sol - Introduction to Algorithms October 23, 2006...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Algorithms October 23, 2006 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Madhu Sudan Problem Set 4 Solutions Problem Set 4 Solutions Problem 4-1. AVL Trees. An AVL tree is a binary search tree that is height balanced , i.e., for each node x , the heights of the left and right subtrees of x differ by at most 1 . To implement an AVL tree, we maintain an extra field in each node: h [ x ] is the height of the node x . Our first goal is to prove that BST-SEARCH runs in O (lg n ) time on an AVL tree. (a) Prove that an AVL tree with n nodes has height Θ(lg n ) . ( Hint: Prove that an AVL tree of height h has at least F h nodes, where F h is the h th Fibonacci number.) Solution: Let N h denote the smallest possible number of nodes in an AVL tree of height h . We shall show by induction that N h ≥ F h . Base: Clearly, N = 1 ≥ 0 = F , and N 1 = 2 ≥ 1 = F 1 . Induction step: Assume h ≥ 2 and that for all k < h , N k ≥ F k . Now, for any AVL tree of height h , the height of one of the two subtrees of the root must be h- 1 ; the other subtree of the root must have height at least h- 2 . Therefore, the number of nodes in the tree is at least N h- 1 + N h- 2 + 1 , which by the induction hypothesis is at least F h- 1 + F h- 2 . Since this is true for all AVL trees of height h , we have that N h ≥ F h- 1 + F h- 2 = F h . Thus N h ≥ F h for all h . Now recall that F i +2 ≥ φ i , where φ > 1 is the golden ratio (see CLRS Exercise 3.2-7). This gives us that for h ≥ 2 , N h ≥ φ h- 2 , or h ≤ log φ N h + 2 . Thus for n ≥ 4 , the height h of an AVL tree with n nodes satisfies h ≤ log φ N h + 2 ≤ log φ n + 2 , which implies that an AVL tree with n nodes has height O (lg n ) . Since any binary tree on n nodes has height at least lg n , we also have that an AVL tree with n nodes has height Ω(lg n ) . This proves that the height of an AVL tree on n nodes is Θ(lg n ) . Our remaining goal is to show how to insert a node into an AVL tree while maintaining the height- balance invariant. To insert a key into an AVL tree, we first place a node with that key in the appropriate place in the binary search tree order, using BST-INSERT. After this insertion, the tree may no longer be height balanced. Specifically, the heights of the left and right children of some node may differ by 2 . (b) Describe a procedure BALANCE ( x ) that takes a node x , whose left and right child subtrees are height balanced and have heights that differ by at most 2 , i.e., | h [ right [ x ]]- h [ left [ x ]] | ≤ 2 , and alters the subtree rooted at x to be height balanced. ( Hint: Use rotations.) 2 Problem Set 4 Solutions Solution: We sketch how BALANCE ( x ) works rather than giving pseudocode....
View Full Document

This note was uploaded on 01/20/2012 for the course CS 6.006 taught by Professor Erikdemaine during the Fall '08 term at MIT.

Page1 / 7

ps4sol - Introduction to Algorithms October 23, 2006...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online