This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 6.006 Intro to Algorithms Recitation 04 February 11, 2011 AVL Trees Recall the operations (e.g. find , insert , delete ) of a binary search tree. The runtime of these operations were all O ( h ) where h represents the height of the tree, defined as the length of the longest branch. In the worst case, all the nodes of a tree could be on the same branch. In this case, h = n , so the runtime of these binary search tree operations are O ( n ) . However, we can maintain a much better upper bound on the height of the tree if we make efforts to balance the tree and even out the length of all branches. AVL trees are binary search trees that balances itself every time an element is inserted or deleted. Each node of an AVL tree has the property that the heights of the subtree rooted at its children differ by at most one. Upper Bound of AVL Tree Height We can show that an AVL tree with n nodes has O (log n ) height. Let N h represent the minimum number of nodes that can form an AVL tree of height h . If we know N h 1 and N h 2 , we can determine N h . Since this N hnoded tree must have a height h , the root must have a child that has height h 1 . To minimize the total number of nodes in this tree, we would have this subtree contain N h 1 nodes. By the property of an AVL tree, if one child has height h 1 , the minimum height of the other child is h 2 . By creating a tree with a root whose left subtree has N h 1 nodes and whose right subtree has N h 2 nodes, we have constructed the AVL tree of height h with the least nodes possible. This AVL tree has a total of N h 1 + N h 2 +1 nodes ( N h 1 and N h 2 coming from the subtrees at the children of the root, the 1 coming from the root itself). The base cases are N 1 = 1 and N 2 = 2 . From here, we can iteratively construct N h by using the fact that N h = N h 1 + N h 2 + 1 that we figured out above....
View
Full
Document
This note was uploaded on 11/11/2011 for the course MATH 180 taught by Professor Byrns during the Spring '11 term at Montgomery College.
 Spring '11
 byrns
 Math

Click to edit the document details