AVL tree
From Wikipedia, the free encyclopedia
In computer science, an AVL tree is a self-balancing binary search tree, and the first such data structure to be invented. In an AVL tree the heights of the two child subtrees of any node differ by at most one, therefore it is also called height-balanced. Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases. Additions and deletions may require the tree to be rebalanced by one or more tree rotations.
The AVL tree is named after its two inventors, G.M. Adelson-Velsky and E.M. Landis, who published it in their 1962 paper "An algorithm for the organization of information."
The balance factor of a node is the height of its right subtree minus the height of its left subtree. A node with balance factor 1, 0, or -1 is considered balanced. A node with any other balance factor is considered unbalanced and requires rebalancing the tree. The balance factor is either stored directly at each node or computed from the heights of the subtrees.
While AVL trees are theoretically quite sound, they are not commonly implemented due to the high complexity required to maintain balance, making development less effective when compared with self-correcting tree structures, such as splay trees or heaps.[citation needed] They do perform better than red-black trees for lookup-intensive applications.<ref>Pfaff, Ben (June 2004). Performance Analysis of BSTs in System Software (PDF). Stanford University.</ref> The AVL tree balancing algorithm appears in many computer science curriculums.
Contents |
[edit] Operations
The basic operations of an AVL tree generally involve carrying out the same algorithms as would be carried out on an unbalanced binary search tree, but preceded or followed by one or more of the so-called "AVL rotations."
[edit] Insertion
Insertion into an AVL tree may be carried out by inserting the given value into the tree as if it were an unbalanced binary search tree, and then retracing one's steps toward the root updating the balance factor of the nodes. Retracing is stopped when a node's balance factor becomes 0, 2, or -2. If the balance factor becomes 0 then the height of the subtree hasn't changed because of the insert and the insertion is finished.
If the balance factor becomes 2 or -2 then the tree rooted at this node is unbalanced, and a tree rotation is needed.
The tree rotation will always leave the subtree evenly balanced stopping the need for any further retracing towards the root.
The rotation can be done in constant time.
Similar to an unbalanced binary search tree, insertion takes O(height) time. This is O(height) to find the insertion point plus O(height) to check for any needed rotations. The balanced nature of the AVL tree provides us with an upper bound on its height: at most 1.44 lg(n + 2) <ref>E. Horowitz, S. Sahni, and D. Mehta, Fundamentals of Data Structures in C++. Computer Science Press, 1995. ISBN 0-7167-8292-8 </ref>. So the insertion process in total takes O(log n) time.
[edit] Deletion
Remove the node. If it is not a leaf, replace the removed node with either the largest in the left subtree or the smallest in the right subtree. This way we always consider deletion at a leaf. After deletion retrace the path back up the tree to the root, adjusting the balance factors as needed.
The retracing can stop if the balance factor becomes -1 or 1 indictating that the height of that subtree has remained unchanged. If the balance factor becomes 0 then the height of the subtree has decreased by one and the retracing needs to continue. If the balance factor becomes -2 or 2 then the subtree is unbalanced and needs to be rotated to fix it. If the rotation leaves the subtree's balance factor at 0 then the retracing towards the root must continue since the height of this subtree has decreased by one. This is in contrast to an insertion where a rotation resulting in a balance factor of 0 indicated that the subtree's height has remained unchanged.
The time required is O(h) for lookup plus O(h) rotations on the way back to the root; so the operation can be completed in O(log n) time.
[edit] Lookup
Lookup in an AVL tree is performed exactly as in an unbalanced binary search tree, and thus takes O(log n) time, since an AVL tree is always kept balanced. No special provisions need to be taken, and the tree's structure is not modified by lookups. (This is in contrast to splay tree lookups, which do modify their tree's structure.)
[edit] See also
[edit] References
- G. Adelson-Velskii and E.M. Landis, "An algorithm for the organization of information." Doklady Akademii Nauk SSSR, 146:263–266, 1962 (Russian). English translation by Myron J. Ricci in Soviet Math. Doklady, 3:1259–1263, 1962.
- Donald Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89685-0. Pages 458–475 of section 6.2.3: Balanced Trees. Note that Knuth calls AVL trees simply "balanced trees".
<references/>
[edit] External links
- Description from the Dictionary of Algorithms and Data Structures
- 5 Types of AVL Trees in C++
- Memory Allocation in Visual Basic using Balanced Binary AVL Trees for keys of arbitrary length
- Iterative Implementation of AVL Trees in C#
- Heavily documented fast Implementation in Linoleum (a cross-platform Assembler) by Herbert Glarner
- AVL Tree Traversal
- C++ AVL Tree Template and C AVL TREE "Generic Package" by Walt Karas
- A Visual Basic AVL Tree Container Class by Jim Harris
- AVL Trees: Tutorial and C++ Implementation by Brad Appleton
- Ulm's Oberon Library: AVLTrees
- The AVL TREE Data Type
- CNAVLTree Class Reference
- GNU libavl
- AVL-trees - balanced binary trees by Alex Konshin
- Simulation of AVL Trees
- AVL tree applet
- AVL, Splay and Red/Black Applet
- Visual Tutorial of AVL Tree operationscs:AVL-strom
da:AVL-træ de:AVL-Baum es:Árbol AVL fr:Arbre AVL it:Albero AVL he:עץ AVL lt:AVL medis ja:AVL木 pl:Drzewo AVL pt:Árvore AVL sk:AVL strom sl:AVL drevo fi:AVL-puu sv:AVL-träd zh:AVL树



