Download presentation
Presentation is loading. Please wait.
Published byRaul Eagerton Modified over 9 years ago
1
1 AVL-Trees (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst case: (n). Can be better! Idea: Balanced trees. Definition: An AVL-tree is a binary search tree such that for each sub-tree T ' = | h(L) - h(R) | 1 holds (balanced sub-trees is a characteristic of AVL-trees). The balance factor or height is often annotated at each node h(.)+1.
2
2 |Height(I) – hight(D)| < = 1 This is an AVL tree
3
3 This is NOT an AVL tree (node * does not hold the required condition)
4
4 Goals 1. How can the AVL-characteristics be kept when inserting and deleting nodes? 2. We will see that for AVL-trees the complexity of the operations is in the worst case = O(height of the AVL-tree) = O(log n)
5
5 Preservation of the AVL-characteristics After inserting and deleting nodes from a tree we must procure that new tree preserves the characteristics of an AVL-tree: Re-balancing. How ?: simple and double rotations
6
Only 2 cases (an their mirrors) Let’s analyze the case of insertion – The new element is inserted at the right (left) sub- tree of the right (left) child which was already higher than the left (right) sub-tree by 1 – The new element is inserted at the left (right) sub- tree of the right (left) child which was already higher than the left (right) sub-tree by 1 6
7
7 Rotation (for the case when the right sub-tree grows too high after an insertion) Is transformed into
8
8 Double rotation (for the case that the right sub-tree grows too high after an insertion at its left sub-tree) Is transformed into Double rotation
9
9 W Z a b c x y W Z a b c xy new First rotation Second rotation
10
10 Re-balancing after insertion: After an insertion the tree might be still balanced or: theorem: After an insertion we need only one rotation of double-rotation at the first node that got unbalanced * in order to re-establish the balance properties of the AVL tree. (* : on the way from the inserted node to the root). Because: after a rotation or double rotation the resulting tree will have the original size of the tree!
11
The same applies for deleting Only 2 cases (an their mirrors) – The element is deleted at the right (left) sub-tree of which was already smaller than the left (right) sub- tree by 1 – The new element is inserted at the left (right) sub- tree of the right (left) child which was already higher that the left (right) sub-tree by 1 11
12
The cases 12 1 1 1 Deleted node
13
13 Re-balancing after deleting: After deleting a node the tree might be still balanced or: Theorem: after deleting we can restore the AVL balance properties of the sub-tree having as root the first* node that got unbalanced with just only one simple rotation or a double rotation. (* : on the way from the deleted note to the root). However: the height of the resulting sub-tree might be shortened by 1, this means more rotations might be (recursively) necessary at the parent nodes, which can affect up to the root of the entire tree.
14
14 About Implementation While searching for unbalanced sub-tree after an operation it is only necessary to check the parent´s sub-tree only when the son´s sub-tree has changed it height. In order make the checking for unbalanced sub-trees more efficient, it is recommended to put some more information on the nodes, for example: the height of the sub-tree or the balance factor (height(left sub-tree) – height(right sub- tree)) This information must be updated after each operation It is necessary to have an operation that returns the parent of a certain node (for example, by adding a pointer to the parent).
15
15 Complexity analysis– worst case Be h the height of the AVL-tree. Searching: as in the normal binary search tree O(h). Insert: the insertion is the same as the binary search tree (O(h)) but we must add the cost of one simple or double rotation, which is constant : also O(h). delete: delete as in the binary search tree(O(h)) but we must add the cost of (possibly) one rotation at each node on the way from the deleted node to the root, which is at most the height of the tree: O(h). All operations are O(h).
16
16 Calculating the height of an AVL tree Be N(h) the minimal number of nodes In an AVL-tree having height h. N(0)=1, N(1)=2, N(h) = 1 + N(h-1) + N(h-2) für h 2. N(3)=4, N(4)=7 remember: Fibonacci-numbers fibo(0)=0, fibo(1)=1, fibo(n) = fibo(n-1) + fibo(n-2) fib(3)=1, fib(4)=2, fib(5)=3, fib(6)=5, fib(7)=8 By calculating we can state: N(h) = fibo(h+3) - 1 0 1 23 Principle of construction
17
17 Be n the number of nodes of an AVL-tree of height h. Then it holds that: n N(h), By making p = (1 + sqrt(5))/2 und q = (1- sqrt(5))/2 we can now write n fibo(h+3)-1 = ( p h+3 – q h+3 ) / sqrt(5) – 1 ( p h+3 /sqrt(5)) – 3/2, thus h+3+log p (1/sqrt(5)) log p (n+3/2), thus there is a constant c with h log p (n) + c = log p (2) log 2 (n) + c = 1.44… log 2 (n) + c = O(log n).
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.