computer science, an AVL tree is a self-balancing binary search tree, and it is the first such data structureto be invented. [Robert Sedgewick, "Algorithms", Addison-Wesley, 1983, ISBN 0-201-06672-6, page 199, chapter 15: Balanced Trees.] In an AVL tree, the heights of the two child subtrees of any node differ by at most one; therefore, it is also said to be height-balanced. Lookup, insertion, and deletion all take O(log "n") time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. Insertions 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 and 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.
AVL trees are often compared with
red-black treesbecause they support the same set of operations and because red-black trees also take O(log "n") time for the basic operations. AVL trees perform better than red-black trees for lookup-intensive applications. [cite web|last = Pfaff|first = Ben|title = Performance Analysis of BSTs in System Software| publisher = Stanford University|year = 2004|month = June|url = http://www.stanford.edu/~blp/papers/libavl.pdf|format = PDF] The AVL tree balancing algorithm appears in many computer science curricula.
The basic operations of an AVL tree generally involve carrying out the same actions as would be carried out on an unbalanced
binary search tree, but preceded or followed by one or more operations called tree rotations, which help to restore the height balance of the subtrees.
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. If the balance factor becomes -1, 0, or 1 then the tree is still in AVL form, and no rotations are necessary.
If the balance factor becomes 2 or -2 then the tree rooted at this node is unbalanced, and a tree rotation is needed. At most a single or double rotation will be needed to balance the tree.
There are basically four cases which need to be accounted for, of which two are symmetric to the other two. For simplicity, the root of the unbalanced subtree will be called P, the right child of that node will be called R, and the left child will be called L. If the balance factor of P is 2, it means that the right subtree outweighs the left subtree of the given node, and the balance factor of the right child (R) must then be checked. If the balance factor of R is 1, it means the insertion occurred on the (external) right side of that node and a left rotation is needed (
tree rotation) with P as the root. If the balance factor of R is -1, this means the insertion happened on the (internal) left side of that node. This requires a double rotation. The first rotation is a right rotation with R as the root. The second is a left rotation with P as the root.
The other two cases are identical to the previous two, but with the original balance factor of -2 and the left subtree outweighing the right subtree. [http://www.eli.sdsu.edu/courses/fall96/cs660/notes/avl/avl.html] [http://www.cs.purdue.edu/homes/ayg/CS251/slides/chap7b.pdf] [http://www.cse.ohio-state.edu/~gurari/course/cis680/cis680Ch10.html]
Only the nodes traversed from the insertion point to the root of the tree need be checked, and rotations are a constant time operation, and because the height is limited to O(log(n)), the execution time for an insertion is O(log(n)).
If the node is a leaf, remove it.If the node is not a leaf, replace it with either the largest in its left subtree (inorder predecessor) or the smallest in its right subtree (inorder successor), and remove that node.The node that was found as replacement has at most one subtree. After deletion retrace the path back up the tree (parent of the replacement) to the root, adjusting the balance factors as needed.
The retracing can stop if the balance factor becomes -1 or 1 indicating 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(log(n)) for lookup plus maximum O(log(n)) rotations on the way back to the root; so the operation can be completed in O(log "n") time.
Lookup in an AVL tree is performed exactly as in an unbalanced binary search tree. Because of the height-balancing of the tree, a lookup takes O(log "n") time. No special provisions need to be taken, and the tree's structure is not modified by lookups. (This is in contrast to
splay treelookups, which do modify their tree's structure.)
If each node additionally records the size of its subtree (including itself and its descendants), then the nodes can be retrieved by index in O(log "n") time as well.
Once a node has been found in a balanced tree, the "next" or "previous" node can be obtained in amortized constant time. (In a few cases, about 2*log(n) links will need to be traversed. In most cases, only a single link need be traversed. On the average, about two links need to be traversed.)Fact|date=August 2007
Comparison to other structures
Both AVL trees and red-black trees are self-balancing binary search trees, so they are very similar mathematically. The operations to balance the trees are different, but both occur in constant time. The real difference between the two is the limiting height. For a tree of size :
*an AVL tree's height is limited to
*a red-black tree's height is limited to
The AVL tree is more rigidly balanced than Red-Black trees, leading to slower insertion and removal but faster retrieval.
List of data structures
*cite journal|last=Adelson-Velskii|first=G.|coauthors=E. M. Landis|year=1962|title=An algorithm for the organization of information|journal=
Proceedings of the USSR Academy of Sciences|volume=146|pages=263–266 ru icon 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".
* [http://www.nist.gov/dads/HTML/avltree.html Description from the Dictionary of Algorithms and Data Structures]
* [http://www.eli.sdsu.edu/courses/fall96/cs660/notes/avl/avl.html Visual Tutorial of AVL Tree operations]
* [http://webpages.ull.es/users/jriera/Docencia/AVL/AVL%20tree%20applet.htm A dynamic visualization of AVL trees]
* [http://www.goletas.com/solutions/collections/ Iterative Implementation of AVL Trees in C#]
* [http://herbert.gandraxa.com/herbert/avl.asp Heavily documented fast Implementation] in Linoleum (a cross-platform Assembler)
* [http://geocities.com/wkaras/gen_cpp/avl_tree.html C++ AVL Tree Template]
* [http://www.mathematik.uni-ulm.de/oberon/0.5/lib/man/AVLTrees.html Ulm's Oberon Library: AVLTrees]
* [http://www.csi.uottawa.ca/~stan/csi2514/applets/avl/BT.html AVL tree applet]
* [http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/common/avl/avl.c OpenSolaris AVL Tree C source]
* [http://www.stanford.edu/~blp/avl/ GNU libavl]
* [http://bjourne.blogspot.com/2006/11/avl-tree-in-python.html Python AVL tree]
Wikimedia Foundation. 2010.
Look at other dictionaries:
AVL tree — Arbre AVL Pour les articles homonymes, voir AVL. Un exemple d arbre non AVL. En informatique, les arbres AVL ont été his … Wikipédia en Français
AVL — can stand for any one of the following things:*AVL, the Austrian based (automotive) engineering consulting firm and research institute Anstalt für Verbrennungskraftmaschinen List *Acadèmia Valenciana de la Llengua (Valencian academy of the… … Wikipedia
Tree rotation — A tree rotation is an operation on a binary search tree that changes the structure without interfering with the order of the elements. A tree rotation moves one node up in the tree and one node down. They are used to change the shape of the tree … Wikipedia
Tree (data structure) — A simple unordered tree; in this diagram, the node labeled 7 has two children, labeled 2 and 6, and one parent, labeled 2. The root node, at the top, has no parent. In computer science, a tree is a widely used data structure that emulates a… … Wikipedia
Tree structure — A tree structure showing the possible hierarchical organization of an encyclopedia … Wikipedia
AVL — abbr. Adelson, Veslkij and Laudis (tree) comp. abbr. AVL Tree (named after its founders: Adelson, Veiskii and Landis) … United dictionary of abbreviations and acronyms
Red-black tree — A red black tree is a type of self balancing binary search tree, a data structure used in computer science, typically used to implement associative arrays. The original structure was invented in 1972 by Rudolf Bayer who called them symmetric… … Wikipedia
Splay tree — A splay tree is a self balancing binary search tree with the additional property that recently accessed elements are quick to access again. It performs basic operations such as insertion, look up and removal in O(log(n)) amortized time. For many… … Wikipedia
Binary search tree — In computer science, a binary search tree (BST) is a binary tree data structurewhich has the following properties: *each node (item in the tree) has a value; *a total order (linear order) is defined on these values; *the left subtree of a node… … Wikipedia
T-tree — In computer science a T tree is a type of binary tree data structure that is used by main memory databases, such as DataBlitz, e X treme DB, MySQL Cluster, Oracle TimesTen and [http://www.kairosdbms.com Kairos] [http://www.emware.co.kr… … Wikipedia