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!
===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}}
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)