Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
AVL tree
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==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]], 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#Searching|binary search tree]].<ref name="dixit-mastering-data-structures">{{Cite book |last=Dixit |first=J. B. |title=Mastering data structures through 'C' language |publisher=University Science Press, an imprint of Laxmi Publications Pvt. Ltd. |year=2010 |isbn=9789380386720 |location=New Delhi, India |oclc=939446542}}</ref>{{rp|ch. 8}} In order for search to work effectively it has to employ a comparison function which establishes a [[total order]] (or at least a [[Weak ordering#Total preorders|total preorder]]) on the set of keys.<ref name="brass-advanced-data-structures">{{Cite book |last=Brass |first=Peter |title=Advanced data structures |date=2008 |publisher=Cambridge University Press |isbn=9780511438202 |location=Cambridge |oclc=312435417}}</ref>{{rp|23}} The number of comparisons required for successful search is limited by the height {{math|''h''}} and for unsuccessful search is very close to {{math|''h''}}, so both are in {{math|O(log ''n'')}}.<ref name="hubbard-outline-theory-problems">{{Cite book |last=Hubbard |first=John Rast |url=https://archive.org/details/schaumsoutlineof0000hubb |title=Schaum's outline of theory and problems of data structures with Java |date=2000 |publisher=McGraw-Hill |isbn=0071378707 |location=New York |oclc=48139308 |url-access=registration}}</ref>{{rp|216}} ===Traversal=== As a read-only operation the traversal of an AVL tree functions the same way as on any other binary tree. Exploring all {{math|''n''}} 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 complexity|amortized]] constant time.<ref name="Pfaff" />{{rp|58}} Some instances of exploring these "nearby" nodes require traversing up to {{math|''h'' ∝ log(''n'')}} 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 {{math|''n''−1}} links in any tree, the amortized cost is {{math|2×(''n''−1)/''n''}}, 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]]. 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.<ref>{{Cite book |last=Weiss |first=Mark Allen |title=Data structures and algorithm analysis in C++ |date=2006 |publisher=Pearson Addison-Wesley |isbn=0-321-37531-9 |edition=3rd |location=Boston |pages=145 |oclc=61278554}}</ref> 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|balance factor]] of each node.<ref name="Knuth" />{{rp|458–481}} <ref name="Pfaff">{{Cite book |last=Pfaff |first=Ben |title=An Introduction to Binary Search Trees and Balanced Trees |date=2004 |publisher=Free Software Foundation, Inc.}}</ref>{{rp|108}} 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 {{nowrap|[–2,+2].}} 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.<ref name="brass-advanced-data-structures" />{{rp|52}} With insertion as the code below shows, the adequate rotation immediately perfectly [[#Rebalancing|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. ;[[Loop invariant|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. {{Collapse top|Example code for an insert operation}} <syntaxhighlight lang="c" style="overflow:hidden" line="1"> for (X = parent(Z); X != null; X = parent(Z)) { // Loop (possibly up to the root) // BF(X) has to be updated: if (Z == right_child(X)) { // The right subtree increases if (BF(X) > 0) { // X is right-heavy // ==> the temporary BF(X) == +2 // ==> rebalancing is required. G = parent(X); // Save parent of X around rotations if (BF(Z) < 0) // Right Left Case (see figure 3) N = rotate_RightLeft(X, Z); // Double rotation: Right(Z) then Left(X) else // Right Right Case (see figure 2) N = rotate_Left(X, Z); // Single rotation Left(X) // After rotation adapt parent link } else { if (BF(X) < 0) { BF(X) = 0; // Z’s height increase is absorbed at X. break; // Leave the loop } BF(X) = +1; Z = X; // Height(Z) increases by 1 continue; } } else { // Z == left_child(X): the left subtree increases if (BF(X) < 0) { // X is left-heavy // ==> the temporary BF(X) == -2 // ==> rebalancing is required. G = parent(X); // Save parent of X around rotations if (BF(Z) > 0) // Left Right Case N = rotate_LeftRight(X, Z); // Double rotation: Left(Z) then Right(X) else // Left Left Case N = rotate_Right(X, Z); // Single rotation Right(X) // After rotation adapt parent link } else { if (BF(X) > 0) { BF(X) = 0; // Z’s height increase is absorbed at X. break; // Leave the loop } BF(X) = -1; Z = X; // Height(Z) increases by 1 continue; } } // After a rotation adapt parent link: // N is the new root of the rotated subtree // Height does not change: Height(N) == old Height(X) parent(N) = G; if (G != null) { if (X == left_child(G)) left_child(G) = N; else right_child(G) = N; } else tree->root = N; // N is the new root of the total tree break; // There is no fall thru, only break; or continue; } // Unless loop is left via break, the height of the total tree increases by 1. </syntaxhighlight> {{Collapse bottom}} 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 {{math|O(log ''n'')}} for lookup, plus a maximum of {{math|O(log ''n'')}} retracing levels ({{math|O(1)}} on average) on the way back to the root, so the operation can be completed in {{math|O(log ''n'')}} time.<ref name="brass-advanced-data-structures" />{{rp|53}} ===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|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. {{Collapse top|Example code for a delete operation}} <syntaxhighlight lang="c" style="overflow:hidden" line="1"> for (X = parent(N); X != null; X = G) { // Loop (possibly up to the root) G = parent(X); // Save parent of X around rotations // BF(X) has not yet been updated! if (N == left_child(X)) { // the left subtree decreases if (BF(X) > 0) { // X is right-heavy // ==> the temporary BF(X) == +2 // ==> rebalancing is required. Z = right_child(X); // Sibling of N (higher by 2) b = BF(Z); if (b < 0) // Right Left Case (see figure 3) N = rotate_RightLeft(X, Z); // Double rotation: Right(Z) then Left(X) else // Right Right Case (see figure 2) N = rotate_Left(X, Z); // Single rotation Left(X) // After rotation adapt parent link } else { if (BF(X) == 0) { BF(X) = +1; // N’s height decrease is absorbed at X. break; // Leave the loop } N = X; BF(N) = 0; // Height(N) decreases by 1 continue; } } else { // (N == right_child(X)): The right subtree decreases if (BF(X) < 0) { // X is left-heavy // ==> the temporary BF(X) == -2 // ==> rebalancing is required. Z = left_child(X); // Sibling of N (higher by 2) b = BF(Z); if (b > 0) // Left Right Case N = rotate_LeftRight(X, Z); // Double rotation: Left(Z) then Right(X) else // Left Left Case N = rotate_Right(X, Z); // Single rotation Right(X) // After rotation adapt parent link } else { if (BF(X) == 0) { BF(X) = -1; // N’s height decrease is absorbed at X. break; // Leave the loop } N = X; BF(N) = 0; // Height(N) decreases by 1 continue; } } // After a rotation adapt parent link: // N is the new root of the rotated subtree parent(N) = G; if (G != null) { if (X == left_child(G)) left_child(G) = N; else right_child(G) = N; } else tree->root = N; // N is the new root of the total tree if (b == 0) break; // Height does not change: Leave the loop // Height(N) decreases by 1 (== old Height(X)-1) } // If (b != 0) the height of the total tree decreases by 1. </syntaxhighlight> {{Collapse bottom}} 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 {{math|O(log ''n'')}} for lookup, plus a maximum of {{math|O(log ''n'')}} retracing levels ({{math|O(1)}} on average) on the way back to the root, so the operation can be completed in {{math|O(log ''n'')}} 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 (set theory)|union]], [[Intersection (set theory)|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.<ref name="join-based">{{Citation |last1=Blelloch |first1=Guy E. |title=Symposium on Parallel Algorithms and Architectures |pages=253–264 |year=2016 |chapter=Just join for parallel ordered sets |publisher=ACM |arxiv=1602.02120 |doi=10.1145/2935764.2935768 |isbn=978-1-4503-4210-0 |s2cid=2897793 |last2=Ferizovic |first2=Daniel |last3=Sun |first3=Yihan}}.</ref> The function ''Join'' on two AVL trees {{math|''t''<sub>1</sub>}} and {{math|''t''<sub>2</sub>}} and a key {{mvar|k}} will return a tree containing all elements in {{math|''t''<sub>1</sub>}}, {{math|''t''<sub>2</sub>}} as well as {{mvar|k}}. It requires {{mvar|k}} to be greater than all keys in {{math|''t''<sub>1</sub>}} and smaller than all keys in {{math|''t''<sub>2</sub>}}. If the two trees differ by height at most one, ''Join'' simply create a new node with left subtree {{math|''t''<sub>1</sub>}}, root {{mvar|k}} and right subtree {{math|''t''<sub>2</sub>}}. Otherwise, suppose that {{math|''t''<sub>1</sub>}} is higher than {{math|''t''<sub>2</sub>}} for more than one (the other case is symmetric). ''Join'' follows the right spine of {{math|''t''<sub>1</sub>}} until a node {{mvar|c}} which is balanced with {{math|''t''<sub>2</sub>}}. At this point a new node with left child {{mvar|c}}, root {{mvar|k}} and right child {{math|''t''<sub>2</sub>}} is created to replace c. The new node satisfies the AVL invariant, and its height is one greater than {{mvar|c}}. 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. {{Collapse top|Pseudocode implementation for the Join algorithm}} '''function''' JoinRightAVL(T<sub>L</sub>, k, T<sub>R</sub>) (l, k', c) = expose(T<sub>L</sub>) '''if''' (Height(c) <= Height(T<sub>R</sub>)+1) T' = Node(c, k, T<sub>R</sub>) 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, T<sub>R</sub>) T<nowiki>''</nowiki> = Node(l, k', T') '''if''' (Height(T') <= Height(l)+1) '''return''' T<nowiki>''</nowiki> '''else''' '''return''' rotateLeft(T<nowiki>''</nowiki>) '''function''' JoinLeftAVL(T<sub>L</sub>, k, T<sub>R</sub>) /* symmetric to JoinRightAVL */ '''function''' Join(T<sub>L</sub>, k, T<sub>R</sub>) '''if''' (Height(T<sub>L</sub>)>Height(T<sub>R</sub>)+1) '''return''' JoinRightAVL(T<sub>L</sub>, k, T<sub>R</sub>) '''if''' (Height(T<sub>R</sub>)>Height(T<sub>L</sub>)+1) '''return''' JoinLeftAVL(T<sub>L</sub>, k, T<sub>R</sub>) '''return''' Node(T<sub>L</sub>, k, T<sub>R</sub>) Here Height(v) is the height of a subtree (node) {{mvar|v}}. (l,k,r) = expose(v) extracts {{mvar|v}}'s left child {{mvar|l}}, the key {{mvar|k}} of {{mvar|v}}'s root, and the right child {{mvar|r}}. Node(l,k,r) means to create a node of left child {{mvar|l}}, key {{mvar|k}}, and right child {{mvar|r}}. {{Collapse bottom}} To split an AVL tree into two smaller trees, those smaller than key {{mvar|k}}, and those greater than key {{mvar|k}}, first draw a path from the root by inserting {{mvar|k}} into the AVL. After this insertion, all values less than {{mvar|k}} will be found on the left of the path, and all values greater than {{mvar|k}} 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 {{math|O(log ''n'')}}, order of the height of the tree. {{Collapse top|Pseudocode implementation for the Split algorithm}} '''function''' Split(T, k) '''if''' (T = nil) return (nil, false, nil) (L,m,R) = expose(T) '''if''' (k = m) return (L, true, R) '''if''' (k<m) (L',b,R') = Split(L,k) '''return''' (L', b, Join(R', m, R)) '''if''' (k>m) (L',b,R') = Split(R, k) '''return''' (Join(L, m, L'), b, R')) {{Collapse bottom}} The union of two AVL trees {{math|''t''<sub>1</sub>}} and {{math|''t''<sub>2</sub>}} representing sets {{mvar|A}} and {{mvar|B}}, is an AVL {{mvar|''t''}} that represents {{math|''A'' ∪ ''B''}}. {{Collapse top|Pseudocode implementation for the Union algorithm}} '''function''' Union(t<sub>1</sub>, t<sub>2</sub>): '''if''' t<sub>1</sub> = nil: '''return''' t<sub>2</sub> '''if''' t<sub>2</sub> = nil: '''return''' t<sub>1</sub> (t<sub><</sub>, b, t<sub>></sub>) = Split(t<sub>2</sub>, t<sub>1</sub>.root) '''return''' Join(Union(left(t<sub>1</sub>), t<sub><</sub>), t<sub>1</sub>.root, Union(right(t<sub>1</sub>), t<sub>></sub>)) Here, ''Split'' is presumed to return two trees: one holding the keys less its input key, one holding the greater keys. (The algorithm is [[persistent data structure|non-destructive]], but an in-place destructive version exists as well.) {{Collapse bottom}} 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 tree algorithms|"join-based" implementation]]. The complexity of each of union, intersection and difference is <math>\text{O}\left(m \log \left({n\over m}+1\right)\right)</math> for AVL trees of sizes <math>m</math> and <math>n \; (\ge m)</math>. More importantly, since the recursive calls to union, intersection or difference are independent of each other, they can be executed [[parallel programming|in parallel]] with a [[Analysis of parallel algorithms|parallel depth]] <math>\text{O}(\log m\log n)</math>.<ref name="join-based" /> When <math>m=1</math>, the join-based implementation has the same computational DAG as single-element insertion and deletion.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)