You are on page 1of 5

AVL tree 1

AVL tree
AVL tree
Type Tree

Invented 1962

Invented by G.M. Adelson-Velskii and E.M. Landis

Time complexity
in big O notation

Average Worst case

Space O(n) O(n)

Search O(log n) O(log n)

Insert O(log n) O(log n)

Delete O(log n) O(log n)

In computer science, an AVL tree is a self-balancing binary search tree, and it was the first such data structure to be
invented.[1] In an AVL tree, the heights of the two child subtrees of any node differ by at most one. 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 Soviet inventors, G.M. Adelson-Velskii and E.M. Landis, who published it in
their 1962 paper "An algorithm for the organization of information."[2]
The balance factor of a node is the height of its left subtree minus the height of its right subtree (sometimes
opposite) 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 trees because they support the same set of operations and because
red-black trees also take O(log n) time for the basic operations. Because AVL trees are more rigidly balanced, they
are faster than red-black trees for lookup intensive applications.[3] However, red-black trees have faster insertion and
removal times.

Operations
Basic operations of an AVL tree involve carrying out the same actions as would be carried out on an unbalanced
binary search tree, but modifications are preceded or followed by one or more operations called tree rotations, which
help to restore the height balance of the subtrees.

Lookup
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 actions 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.)
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 nodes can be explored in amortized constant
time. A few cases require traversing up to 2×log(n) links. However exploring all n nodes in the tree in this manner
AVL tree 2

would use each link exactly twice, and there are n−1 links, so the amortized cost is 2×(n−1)/n, approximately 2.

Insertion
After inserting a node, it is necessary to check each of the
node's ancestors for consistency with the rules of AVL. For
each node checked, if the balance factor remains −1, 0, or
+1 then no rotations are necessary. However, if the balance
factor becomes ±2 then the subtree rooted at this node is
unbalanced. If insertions are performed serially, after each
insertion, at most one tree rotation is needed to restore the
entire tree to the rules of AVL.

There are four cases which need to be considered, of which


two are symmetric to the other two. Let P be the root of the
unbalanced subtree. Let R be the right child of P. Let L be
the left child of P.
Right-Right case and Right-Left case: If the balance factor
of P is −2, then the right subtree outweighs the left subtree
of the given node, and the balance factor of the right child
(R) must be checked. If the balance factor of R is < 0, a left
rotation is needed with P as the root. If the balance factor
of R is -1, a double left rotation (with respect to P and then
R) is needed . If the balance factor of R is +1, two different
rotations are needed. The first rotation is a right rotation
with R as the root. The second is a left rotation with P as the
root.
Pictorial description of how rotations cause rebalancing tree,
Left-Left case and Left-Right case: If the balance factor of and then retracing one's steps toward the root updating the
P is +2, then the left subtree outweighs the right subtree of balance factor of the nodes. The numbered circles represent the
the given node, and the balance factor of the left child (L) nodes being balanced. The lettered triangles represent subtrees
which are themselves balanced BSTs
must be checked. If the balance factor of L is > 0, a right
rotation is needed with P as the root. If the balance factor
of L is +1, a double right rotation (with respect to P and then L) is needed. If the balance factor of R is -1, two
different rotations are needed. The first rotation is a left rotation with L as the root. The second is a right rotation
with P as the root.

Deletion
If the node is a leaf or has only one child, remove it. Otherwise, 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 a 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.
As with all binary trees, a node's in-order successor is the left-most child of its right subtree, and a node's in-order
predecessor is the right-most child of its left subtree. In either case, this node will have zero or one children. Delete it
according to one of the two simpler cases above.
AVL tree 3

In addition to the balancing described above for insertions, if the balance factor for the tree is 2 and that of the left
subtree is 0, a right rotation must be performed on P. The mirror of this case is also necessary.
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 a maximum of O(log n) rotations on the way back to the root, so the
operation can be completed in O(log n) time.

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 O(log n) time. The real difference between the two
is the limiting height. For a tree of size :
• An AVL tree's height is strictly less than:[4]

where is the golden ratio.


[5]
• A red-black tree's height is at most
AVL trees are more rigidly balanced than red-black trees, leading to slower insertion and removal but faster
retrieval.

References
[1] Robert Sedgewick, Algorithms, Addison-Wesley, 1983, ISBN 0-201-06672-6, page 199, chapter 15: Balanced Trees.
[2] Adelson-Velskii, G.; E. M. Landis (1962). "An algorithm for the organization of information". Proceedings of the USSR Academy of Sciences
146: 263–266. (Russian) English translation by Myron J. Ricci in Soviet Math. Doklady, 3:1259–1263, 1962.
[3] Pfaff, Ben (June 2004). "Performance Analysis of BSTs in System Software" (http:/ / www. stanford. edu/ ~blp/ papers/ libavl. pdf) (PDF).
Stanford University. .
[4] Burkhard, Walt (Spring 2010). "AVL Dictionary Data Type Implementation" (http:/ / ieng6. ucsd. edu/ ~cs100s/ public/ Notes/
CSE100Spring2010. pdf). Advanced Data Structures. La Jolla: A.S. Soft Reserves (http:/ / softreserves. ucsd. edu/ ), UC San Diego. p. 99. .
[5] Proof of asymptotic bounds
AVL tree 4

Further reading
• 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.

External links
• Description from the Dictionary of Algorithms and Data Structures (http://www.nist.gov/dads/HTML/avltree.
html)
• C++ Implementation (https://sourceforge.net/projects/standardavl/)
• Python Implementation (http://github.com/pgrafov/python-avl-tree/)
• Single C header file by Ian Piumarta (http://piumarta.com/software/tree/)
• AVL Tree Demonstration (http://www.strille.net/works/media_technology_projects/avl-tree_2001/)
• AVL Tree in examples (http://www.cs.ucf.edu/~reinhard/classes/cop3503/lectures/AVLTrees02.pdf)
• AVL tree applet (http://webdiis.unizar.es/asignaturas/EDA/AVLTree/avltree.html)
• Fast and efficient implementation of AVL Trees (http://github.com/fbuihuu/libtree)
• PowerPoint Teaching Slides for the insertion and deletion algorithms of AVL Trees (http://employees.oneonta.
edu/zhangs/PowerPointPlatform/index.php)
• Animated AVL Tree (http://www.cs.jhu.edu/~goodrich/dsa/trees/avltree.html)
Article Sources and Contributors 5

Article Sources and Contributors


AVL tree  Source: http://en.wikipedia.org/w/index.php?oldid=424753975  Contributors: Adamd1008, Adamuu, Aent, Agrawalyogesh, Alex Kapranoff, AlexGreat, Altenmann, Anant sogani,
Andreas Kaufmann, Andrew Weintraub, Apanag, Astral, Auntof6, Binnacle, Bkil, Blackllotus, Bluebusy, Byrial, Castorvx, Caviare, ChrisMP1, Codingrecipes, Compusense, Conversion script,
Cybercobra, Cyhawk, Daewoollama, Damian Yerrick, Darangho, David.Mestel, Dawynn, Dcamp314, Dcoetzee, Docboat, Doradus, Drilnoth, Dtrebbien, Dysprosia, Eleuther, Enviroboy,
Euchiasmus, Evil Monkey, Flyingspuds, Fredrik, FvdP, G0gogcsc300, Gaius Cornelius, Geek84, Geoff55, Gnowor, Greenrd, Greg Tyler, Gruu, Gulliveig, Gurch, Gökhan, II MusLiM HyBRiD
II, Iamnitin, Intgr, J.delanoy, Jeepday, Jeff02, Jennavecia, Jirka6, Jll, Joeyadams, KGasso, Kain2396, Kdau, Kenyon, Kingpin13, Kjkolb, Ksulli10, Kukolar, LOL, Lankiveil, Larry V, Leuko, M,
MarkHeily, Materialscientist, MattyIX, Mckaysalisbury, Mellerho, Merovingian, Michael Hardy, Michael M Clarke, Michael miceli, Mike Rosoft, Mikm, Minesweeper, Mjkoo, MladenWiki,
Mnogo, Moberg, Mohammad ahad, Momet, Mr.Berna, Msanchez1978, Mtanti, Mzruya, NawlinWiki, Neilc, Nguyễn Hữu Dung, Nixdorf, Noldoaran, Nysin, Obradovic Goran, Oleg Alexandrov,
Oliversisson, Ommiy-Pangaeus, Orimosenzon, Paul D. Anderson, Pavel Vozenilek, Pedrito, Pgan002, Pnorcks, Poor Yorick, RJFJR, Resper, Rockslave, Ruud Koot, ST47, Sebculture, Seyen,
Shlomif, Shmomuffin, Smalljim, Srivesh, Ste4k, Tamfang, Tobias Bergemann, Toby Douglass, Tphyahoo, Tsemii, Tsoft, UnwashedMeme, Uw.Antony, Vektor330, Vlad.c.manea,
West.andrew.g, Xevior, Yksyksyks, Zian, 318 anonymous edits

Image Sources, Licenses and Contributors


Image:AVL Tree Rebalancing.svg  Source: http://en.wikipedia.org/w/index.php?title=File:AVL_Tree_Rebalancing.svg  License: Creative Commons Attribution-Sharealike 2.5  Contributors:
CyHawk
Image:binary search tree delete.svg  Source: http://en.wikipedia.org/w/index.php?title=File:Binary_search_tree_delete.svg  License: Public Domain  Contributors: User:Dcoetzee

License
Creative Commons Attribution-Share Alike 3.0 Unported
http:/ / creativecommons. org/ licenses/ by-sa/ 3. 0/

You might also like