AVLTrees01 - AVL Trees In order to have a worst case...

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

View Full Document Right Arrow Icon
AVL Trees In order to have a worst case running time for insert and delete operations to be O(log n), we must make it impossible for there to be a very long path in the binary search tree. The first balanced binary tree is the AVL tree, named after it's inventors, Adelson-Velskii and Landis. A binary search tree is an AVL tree iff each node in the tree satisfies the following property: The height of the left subtree can differ from the height of the right subtree by at most 1. Based on this property, we can show that the height of an AVL tree is logarithmic with respect to the number of nodes stored in the tree. In particular, for an AVL tree of height H, we find that it must contain at least F H+3 -1 nodes. (F i is the ith Fibonacci number.) To prove this, notice that the number of nodes in an AVL tree is the 1 plus the number of notes in the left subtree plus the number of nodes in the right subtree. If we let S H represent the minimum number of nodes in an AVL tree with height H, we get the following recurrence relation: S H = S H-1 + S H-2 + 1 We also know that S 0 =1 and S 1 =2. Now we can prove the assertion above through induction. Problem: Prove that S H = F H+3 -1.
Background image of page 1

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

View Full DocumentRight Arrow Icon
We will use induction on H, the height of the AVL tree. Base Cases H=0: LHS = 1, RHS = F 3 - 1 = 2 - 1 = 1 H=1: LHS = 2, RHS = F 4 - 1 = 3 - 1 = 2 Inductive hypothesis: For an arbitrary integer k <= H, assume that S k = F k+3 -1. Inductive step: Under the assumption above, prove for H=k+1 that S k+1 = F k+1+3 -1. S k+1 = S k + S k-1 + 1 = (F k+3 -1) + (F k+2 -1) +1, using the I.H. twice = (F k+3 + F k+2 ) - 1 = F k+4 -1, using the defn. of Fibonacci numbers, to complete proof. It can be shown through recurrence relations, that F n 1/ 5 [(1 + 5)/2] n So now, we have the following: S n 1/ 5 [(1 + 5)/2] n+3 This says that when the height of an AVL tree is n, the minimum number of nodes it contains is 1/ 5 [(1 + 5)/2] n+3 . So, in order to find the height of a tree with n nodes, we must replace S n with n and replace n with h? Why is this the case? n 1/ 5 [(1 + 5)/2] h+3 n (1.618) h
Background image of page 2
h log 1.618 n h = O(log 2 n) Now the question remains, how do we maintain an AVL tree? What extra work do we have to do to make sure that the AVL property is maintained? Basically whenever an insertion or deletion is done, it is possible that the new node added or taken away destroys the AVL property. In these sitiuations, we have to "rework" the tree so that the binary search tree and AVL properties are satisfied. When an imbalance is introduced to a tree, it is localized to
Background image of page 3

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

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

This document was uploaded on 11/09/2009.

Page1 / 12

AVLTrees01 - AVL Trees In order to have a worst case...

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

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