HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includi ...
, an AVL tree (named after inventors Adelson-Velsky and Landis) is a
self-balancing binary search tree In computer science, a self-balancing binary search tree (BST) is any node-based binary search tree that automatically keeps its height (maximal number of levels below the root) small in the face of arbitrary item insertions and deletions.Donal ...
. It was 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; if at any time they differ by more than one, rebalancing is done to restore this property. Lookup, insertion, and deletion all take 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 rotation In discrete mathematics, tree rotation is an operation on a binary 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. It is used to change the shape ...
s. The AVL tree is named after its two
Soviet The Soviet Union,. officially the Union of Soviet Socialist Republics. (USSR),. was a transcontinental country that spanned much of Eurasia from 1922 to 1991. A flagship communist state, it was nominally a federal union of fifteen nation ...
inventors,
Georgy Adelson-Velsky Georgy Maximovich Adelson-Velsky (russian: Гео́ргий Макси́мович Адельсо́н-Ве́льский; name is sometimes transliterated as Georgii Adelson-Velskii) (8 January 1922 – 26 April 2014) was a Soviet and Israeli m ...
and Evgenii Landis, who published it in their 1962 paper "An algorithm for the organization of information". AVL trees are often compared with red–black trees because both support the same set of operations and take \text(\log n) time for the basic operations. For lookup-intensive applications, AVL trees are faster than red–black trees because they are more strictly balanced. Similar to red–black trees, AVL trees are height-balanced. Both are, in general, neither weight-balanced nor \mu-balanced for any \mu\leq\tfrac; that is, sibling nodes can have hugely differing numbers of descendants.


Definition


Balance factor

In a binary tree the ''balance factor'' of a node X is defined to be the height difference : \text(X) := \text(\text(X)) - \text(\text(X)) of its two child sub-trees. A binary tree is defined to be an ''AVL tree'' if the invariant :\text(X) \in holds for every node X in the tree. A node X with \text(X)<0 is called "left-heavy", one with \text(X)>0 is called "right-heavy", and one with \text(X)=0 is sometimes simply called "balanced".


Properties

Balance factors can be kept up-to-date by knowing the previous balance factors and the change in height – it is not necessary to know the absolute height. For holding the AVL balance information, two bits per node are sufficient. The height h (counted as the maximal number of levels) of an AVL tree with n nodes lies in the interval: :\log_2(n+1) \le h < \log_\varphi(n+2) + b where \varphi := \tfrac2 \approx 1.618  is the
golden ratio In mathematics, two quantities are in the golden ratio if their ratio is the same as the ratio of their sum to the larger of the two quantities. Expressed algebraically, for quantities a and b with a > b > 0, where the Greek letter phi ( ...
and b := \frac - 2 \approx \; -0.3277 . This is because an AVL tree of height h contains at least F_-1 nodes where \_ is the
Fibonacci sequence In mathematics, the Fibonacci numbers, commonly denoted , form a sequence, the Fibonacci sequence, in which each number is the sum of the two preceding ones. The sequence commonly starts from 0 and 1, although some authors start the sequence from ...
with the seed values F_1=F_2=1 .


Operations

Read-only operations of an AVL tree involve carrying out the same actions as would be carried out on an unbalanced
binary search tree In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left subtree and ...
, but modifications have to observe and restore the height balance of the sub-trees.


Searching

Searching for a specific key in an AVL tree can be done the same way as that of any balanced or unbalanced
binary search tree In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left subtree and ...
. In order for search to work effectively it has to employ a comparison function which establishes a
total order In mathematics, a total or linear order is a partial order in which any two elements are comparable. That is, a total order is a binary relation \leq on some set X, which satisfies the following for all a, b and c in X: # a \leq a ( reflex ...
(or at least a
total preorder In mathematics, especially order theory, a weak ordering is a mathematical formalization of the intuitive notion of a ranking of a set, some of whose members may be tied with each other. Weak orders are a generalization of totally ordered set ...
) on the set of keys. The number of comparisons required for successful search is limited by the height and for unsuccessful search is very close to , so both are in .


Traversal

As a read-only operation the traversal of an AVL tree functions the same way as on any other binary tree. Exploring all nodes of the tree visits each link exactly twice: one downward visit to enter the subtree rooted by that node, another visit upward to leave that node's subtree after having explored it. Once a node has been found in an AVL tree, the ''next'' or ''previous'' node can be accessed in
amortized In computer science, amortized analysis is a method for analyzing a given algorithm's complexity, or how much of a resource, especially time or memory, it takes to execute. The motivation for amortized analysis is that looking at the worst-case ...
constant time. Some instances of exploring these "nearby" nodes require traversing up to links (particularly when navigating from the rightmost leaf of the root's left subtree to the root or from the root to the leftmost leaf of the root's right subtree; in the AVL tree of figure 1, navigating from node P to the next-to-the-right node Q takes 3 steps). Since there are links in any tree, the amortized cost is , or approximately 2.


Insert

When inserting a node into an AVL tree, you initially follow the same process as inserting into a
Binary Search Tree In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left subtree and ...
. If the tree is empty, then the node is inserted as the root of the tree. If the tree is not empty, then we go down the root, and recursively go down the tree searching for the location to insert the new node. This traversal is guided by the comparison function. In this case, the node always replaces a NULL reference (left or right) of an external node in the tree i.e., the node is either made a left-child or a right-child of the external node. After this insertion, if a tree becomes unbalanced, only ancestors of the newly inserted node are unbalanced. This is because only those nodes have their sub-trees altered. So it is necessary to check each of the node's ancestors for consistency with the invariants of AVL trees: this is called "retracing". This is achieved by considering the balance factor of each node. Since with a single insertion the height of an AVL subtree cannot increase by more than one, the temporary balance factor of a node after an insertion will be in the range For each node checked, if the temporary balance factor remains in the range from –1 to +1 then only an update of the balance factor and no rotation is necessary. However, if the temporary balance factor is ±2, the subtree rooted at this node is AVL unbalanced, and a rotation is needed. With insertion as the code below shows, the adequate rotation immediately perfectly rebalances the tree. In figure 1, by inserting the new node Z as a child of node X the height of that subtree Z increases from 0 to 1. ; Invariant of the retracing loop for an insertion The height of the subtree rooted by Z has increased by 1. It is already in AVL shape. for (X = parent(Z); X != null; X = parent(Z)) // Unless loop is left via break, the height of the total tree increases by 1. In order to update the balance factors of all nodes, first observe that all nodes requiring correction lie from child to parent along the path of the inserted leaf. If the above procedure is applied to nodes along this path, starting from the leaf, then every node in the tree will again have a balance factor of −1, 0, or 1. The retracing can stop if the balance factor becomes 0 implying that the height of that subtree remains unchanged. If the balance factor becomes ±1 then the height of the subtree increases by one and the retracing needs to continue. If the balance factor temporarily becomes ±2, this has to be repaired by an appropriate rotation after which the subtree has the same height as before (and its root the balance factor 0). The time required is for lookup, plus a maximum of retracing levels ( on average) on the way back to the root, so the operation can be completed in time.


Delete

The preliminary steps for deleting a node are described in section Binary search tree#Deletion. There, the effective deletion of the subject node or the replacement node decreases the height of the corresponding child tree either from 1 to 0 or from 2 to 1, if that node had a child. Starting at this subtree, it is necessary to check each of the ancestors for consistency with the invariants of AVL trees. This is called "retracing". Since with a single deletion the height of an AVL subtree cannot decrease by more than one, the temporary balance factor of a node will be in the range from −2 to +2. If the balance factor remains in the range from −1 to +1 it can be adjusted in accord with the AVL rules. If it becomes ±2 then the subtree is unbalanced and needs to be rotated. (Unlike insertion where a rotation always balances the tree, after delete, there may be BF(Z) ≠ 0 (see figures 2 and 3), so that after the appropriate single or double rotation the height of the rebalanced subtree decreases by one meaning that the tree has to be rebalanced again on the next higher level.) The various cases of rotations are described in section Rebalancing. ;Invariant of the retracing loop for a deletion The height of the subtree rooted by N has decreased by 1. It is already in AVL shape. for (X = parent(N); X != null; X = G) // If (b != 0) the height of the total tree decreases by 1. The retracing can stop if the balance factor becomes ±1 (it must have been 0) meaning that the height of that subtree remains unchanged. If the balance factor becomes 0 (it must have been ±1) then the height of the subtree decreases by one and the retracing needs to continue. If the balance factor temporarily becomes ±2, this has to be repaired by an appropriate rotation. It depends on the balance factor of the sibling Z (the higher child tree in figure 2) whether the height of the subtree decreases by one –and the retracing needs to continue– or does not change (if Z has the balance factor 0) and the whole tree is in AVL-shape. The time required is for lookup, plus a maximum of retracing levels ( on average) on the way back to the root, so the operation can be completed in time.


Set operations and bulk operations

In addition to the single-element insert, delete and lookup operations, several set operations have been defined on AVL trees:
union Union commonly refers to: * Trade union, an organization of workers * Union (set theory), in mathematics, a fundamental operation on sets Union may also refer to: Arts and entertainment Music * Union (band), an American rock group ** ''Un ...
, intersection and set difference. Then fast ''bulk'' operations on insertions or deletions can be implemented based on these set functions. These set operations rely on two helper operations, ''Split'' and ''Join''. With the new operations, the implementation of AVL trees can be more efficient and highly-parallelizable.. The function ''Join'' on two AVL trees and and a key will return a tree containing all elements in , as well as . It requires to be greater than all keys in and smaller than all keys in . If the two trees differ by height at most one, ''Join'' simply create a new node with left subtree , root and right subtree . Otherwise, suppose that is higher than for more than one (the other case is symmetric). ''Join'' follows the right spine of until a node which is balanced with . At this point a new node with left child , root and right child is created to replace c. The new node satisfies the AVL invariant, and its height is one greater than . The increase in height can increase the height of its ancestors, possibly invalidating the AVL invariant of those nodes. This can be fixed either with a double rotation if invalid at the parent or a single left rotation if invalid higher in the tree, in both cases restoring the height for any further ancestor nodes. ''Join'' will therefore require at most two rotations. The cost of this function is the difference of the heights between the two input trees. function JoinRightAVL(TL, k, TR) (l,k',c) = expose(TL) if (Height(c) <= Height(TR)+1) T' = Node(c,k,TR) if (Height(T') <= Height(l)+1) then return Node(l,k',T') else return rotateLeft(Node(l,k',rotateRight(T'))) else T' = JoinRightAVL(c,k,TR) T'' = Node(l,k',T') if (Height(T') <= Height(l)+1) return T'' else return rotateLeft(T'') function JoinLeftAVL(TL, k, TR) /* symmetric to JoinRightAVL */ function Join(TL, k, TR) if (Height(TL)>Height(TR)+1) return JoinRightAVL(TL, k, TR) if (Height(TR)>Height(TL)+1) return JoinLeftAVL(TL, k, TR) return Node(TL,k,TR) Here Height(v) is the height of a subtree (node) . (l,k,r) = expose(v) extracts 's left child , the key of 's root, and the right child . Node(l,k,r) means to create a node of left child , key , and right child . To split an AVL tree into two smaller trees, those smaller than key , and those larger than key , first draw a path from the root by inserting into the AVL. After this insertion, all values less than will be found on the left of the path, and all values greater than will be found on the right. By applying ''Join'', all the subtrees on the left side are merged bottom-up using keys on the path as intermediate nodes from bottom to top to form the left tree, and the right part is asymmetric. The cost of ''Split'' is , order of the height of the tree. function Split(T,k) if (T = nil) return (nil,false,nil) (L,m,R) = expose(T) if (k = m) return (L,true,R) if (km) (L',b,R') = Split(R,k) return (Join(L,m,L'),b,R')) The union of two AVL trees and representing sets and , is an AVL that represents . function Union(t1, t2): if t1 = nil: return t2 if t2 = nil: return t1 (t<, b, t>) = Split(t2, t1.root) return Join(Union(left(t1), t<), t1.root, Union(right(t1), t>)) Here, ''Split'' is presumed to return two trees: one holding the keys less its input key, one holding the greater keys. (The algorithm is non-destructive, but an in-place destructive version exists as well.) The algorithm for intersection or difference is similar, but requires the ''Join2'' helper routine that is the same as ''Join'' but without the middle key. Based on the new functions for union, intersection or difference, either one key or multiple keys can be inserted to or deleted from the AVL tree. Since ''Split'' calls ''Join'' but does not deal with the balancing criteria of AVL trees directly, such an implementation is usually called the "join-based" implementation. The complexity of each of union, intersection and difference is \text\left(m \log \left(+1\right)\right) for AVL trees of sizes m and n \; (\ge m). More importantly, since the recursive calls to union, intersection or difference are independent of each other, they can be executed in parallel with a parallel depth \text(\log m\log n). When m=1, the join-based implementation has the same computational DAG as single-element insertion and deletion.


Rebalancing

If during a modifying operation the height difference between two child subtrees changes, this may, as long as it is < 2, be reflected by an adaption of the balance information at the parent. During insert and delete operations a (temporary) height difference of 2 may arise, which means that the parent subtree has to be "rebalanced". The given repair tools are the so-called
tree rotation In discrete mathematics, tree rotation is an operation on a binary 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. It is used to change the shape ...
s, because they move the keys only "vertically", so that the ("horizontal") in-order sequence of the keys is fully preserved (which is essential for a binary-search tree). Let X be the node that has a (temporary) balance factor of −2 or +2. Its left or right subtree was modified. Let Z be the higher child (see figures 2 and 3). Note that both children are in AVL shape by induction hypothesis. In case of insertion this insertion has happened to one of Z's children in a way that Z's height has increased. In case of deletion this deletion has happened to the sibling t1 of Z in a way so that t1's height being already lower has decreased. (This is the only case where Z's balance factor may also be 0.) There are four possible variants of the violation: And the rebalancing is performed differently: Thereby, the situations are denoted as where ''C'' (= child direction) and ''B'' (= balance) come from the set with The balance violation of case is repaired by a simple rotation whereas the case is repaired by a double rotation The cost of a rotation, either simple or double, is constant.


Simple rotation

Figure 2 shows a Right Right situation. In its upper half, node X has two child trees with a balance factor of +2. Moreover, the inner child t23 of Z (i.e., left child when Z is right child resp. right child when Z is left child) is not higher than its sibling t4. This can happen by a height increase of subtree t4 or by a height decrease of subtree t1. In the latter case, also the pale situation where t23 has the same height as t4 may occur. The result of the left rotation is shown in the lower half of the figure. Three links (thick edges in figure 2) and two balance factors are to be updated. As the figure shows, before an insertion, the leaf layer was at level h+1, temporarily at level h+2 and after the rotation again at level h+1. In case of a deletion, the leaf layer was at level h+2, where it is again, when t23 and t4 were of same height. Otherwise the leaf layer reaches level h+1, so that the height of the rotated tree decreases. ;Code snippet of a simple left rotation node *rotate_Left(node *X, node *Z)


Double rotation

Figure 3 shows a Right Left situation. In its upper third, node X has two child trees with a balance factor of +2. But unlike figure 2, the inner child Y of Z is higher than its sibling t4. This can happen by the insertion of Y itself or a height increase of one of its subtrees t2 or t3 (with the consequence that they are of different height) or by a height decrease of subtree t1. In the latter case, it may also occur that t2 and t3 are of the same height. The result of the first, the right, rotation is shown in the middle third of the figure. (With respect to the balance factors, this rotation is not of the same kind as the other AVL single rotations, because the height difference between Y and t4 is only 1.) The result of the final left rotation is shown in the lower third of the figure. Five links (thick edges in figure 3) and three balance factors are to be updated. As the figure shows, before an insertion, the leaf layer was at level h+1, temporarily at level h+2 and after the double rotation again at level h+1. In case of a deletion, the leaf layer was at level h+2 and after the double rotation it is at level h+1, so that the height of the rotated tree decreases. ;Code snippet of a right-left double rotation node *rotate_RightLeft(node *X, node *Z)


Comparison to other structures

Both AVL trees and red–black (RB) trees are self-balancing binary search trees and they are related mathematically. Indeed, every AVL tree can be colored red–black, but there are RB trees which are not AVL balanced. For maintaining the AVL resp. RB tree's invariants, rotations play an important role. In the worst case, even without rotations, AVL or RB insertions or deletions require inspections and/or updates to AVL balance factors resp. RB colors. RB insertions and deletions and AVL insertions require from zero to three tail-recursive rotations and run in
amortized In computer science, amortized analysis is a method for analyzing a given algorithm's complexity, or how much of a resource, especially time or memory, it takes to execute. The motivation for amortized analysis is that looking at the worst-case ...
time, Dinesh P. Mehta, Sartaj Sahni (Ed.) ''Handbook of Data Structures and Applications'' 10.4.2 thus equally constant on average. AVL deletions requiring rotations in the worst case are also on average. RB trees require storing one bit of information (the color) in each node, while AVL trees mostly use two bits for the balance factor, although, when stored at the children, one bit with meaning «lower than sibling» suffices. The bigger difference between the two data structures is their height limit. For a tree of size *an AVL tree's height is at most *: \begin h & \leqq \; c \log_2 (n + d) + b \\ & < \; c \log_2 (n + 2) + b \end :where \varphi := \tfrac2 \approx 1.618  the
golden ratio In mathematics, two quantities are in the golden ratio if their ratio is the same as the ratio of their sum to the larger of the two quantities. Expressed algebraically, for quantities a and b with a > b > 0, where the Greek letter phi ( ...
, c := \tfrac 1 \approx 1.440,   b := \tfrac2 \log_2 5 - 2 \approx \; -0.328, and  d:=1+\tfrac \approx 1.065. *a RB tree's height is at most *: \begin h & \leqq \; 2\log_2(n+1) \end  . Red–black tree#Proof of asymptotic bounds AVL trees are more rigidly balanced than RB trees with an asymptotic relation AVL/RB ≈0.720 of the maximal heights. For insertions and deletions, Ben Pfaff shows in 79 measurements a relation of AVL/RB between 0.677 and 1.077 with median ≈0.947 and geometric mean ≈0.910.


See also

*
Trees In botany, a tree is a perennial plant with an elongated stem, or trunk, usually supporting branches and leaves. In some usages, the definition of a tree may be narrower, including only woody plants with secondary growth, plants that are u ...
*
Tree rotation In discrete mathematics, tree rotation is an operation on a binary 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. It is used to change the shape ...
* WAVL tree * Red–black tree * Splay tree * Scapegoat tree *
B-tree In computer science, a B-tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. The B-tree generalizes the binary search tree, allowing for ...
*
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, eXtremeDB, MySQL Cluster, Oracle TimesTen and MobileLite. A T-tree is a balanced index tree data structure optim ...
*
List of data structures This is a list of well-known data structures. For a wider list of terms, see list of terms relating to algorithms and data structures. For a comparison of running times for a subset of this list see comparison of data structures. Data types ...


References


Further reading

*
Donald Knuth Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist, mathematician, and professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of computer sc ...
. ''
The Art of Computer Programming ''The Art of Computer Programming'' (''TAOCP'') is a comprehensive monograph written by the computer scientist Donald Knuth presenting programming algorithms and their analysis. Volumes 1–5 are intended to represent the central core of com ...
'', Volume 3: ''Sorting and Searching'', Third Edition. Addison-Wesley, 1997. . Pages 458–475 of section 6.2.3: Balanced Trees. * .


External links

* {{DEFAULTSORT:AVL Tree 1962 in computing Articles with example pseudocode Binary trees Soviet inventions Search trees