ArticleslgStudy

computer science

AVL tree

AVL tree is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand AVL tree rather than just read about it. In short: In computer science, an AVL tree (named after inventors Adelson-Velsky and Landis) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by not more than one; if at any time they differ by more than one, rebalancing is done to restore this property.

AVL tree — main illustration
AVL tree — illustration

Key takeaways

  • AVL tree belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect AVL tree to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of AVL tree from memory before moving on to harder problems.

Reference excerpt

In computer science, an AVL tree (named after inventors Adelson-Velsky and Landis) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by not more than one; if at any time they differ by more than one, rebalancing is done to restore this property. 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, Georgy Adelson-Velsky and Evgenii Landis, who published it in their 1962 paper "An algorithm for the organization of information". It was the first self-balancing binary search tree data structure to be invented. AVL trees are often compared with red–black trees because both support the same set of operations and take O(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 μ-balanced for any μ≤12; 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

BF(X):=Height(RightSubtree(X))−Height(LeftSubtree(X)) of its two child sub-trees rooted by node X. A binary tree is defined to be an AVL tree if

BF(X)∈{−1,0,1} holds for every node X in the tree. A node X with BF(X)<0 is called "left-heavy", one with BF(X)>0 is called "right-heavy", and one with BF(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:

log2(n+1)≤h<logφ(n+2)+b where φ:=1+52≈1.618 is the golden ratio and b:=log252log2φ−2≈−0.3277. This is because an AVL tree of height h contains at least Fh+2−1 nodes where {Fn}n∈ℕ is the Fibonacci sequence with the seed values F1=F2=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, 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 order for search to work effectively it has to employ a comparison function which establishes a total order (or at least a total preorder) on the set of keys. The number of comparisons required for successful search is limited by the height h and for unsuccessful search is very close to h, so both are in O(log n), where n is the number of nodes in the tree.

Traversal As a read-only operation the traversal of an AVL tree functions the same way as on any other binary tree. Exploring all 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 constant time. Some instances of exploring these "nearby" nodes require traversing up to 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 n−1 links in any tree, the amortized cost is 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 existing node in the tree i.e., the node is either made a left-child or a right-child of the existing 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 [–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. 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.

… excerpt ends here. Continue reading the full article.

Illustrations

AVL tree: Animation showing the insertion of several elements into an AVL tree. It includes left, right, left-right and right-left rotations.
Animation showing the insertion of several elements into an AVL tree. It includes left, right, left-right and right-left rotations.
AVL tree: Fig. 1: AVL tree with balance factors (green)
Fig. 1: AVL tree with balance factors (green)
AVL tree: Fig. 2: Simple rotationrotate_Left(X,Z)
Fig. 2: Simple rotationrotate_Left(X,Z)
AVL tree: Fig. 3: Double rotation rotate_RightLeft(X,Z)= rotate_Right around Z followed byrotate_Left around X
Fig. 3: Double rotation rotate_RightLeft(X,Z)= rotate_Right around Z followed byrotate_Left around X

Worked examples

Example 1 — a first encounter with AVL tree

Start with the simplest possible case. Write down what AVL tree claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to AVL tree before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about AVL tree ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of AVL tree

In research
AVL tree appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses AVL tree in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
AVL tree is common in secondary-school and first-year university syllabi. It links to neighbouring topics 1962 in computing, Amortized data structures, Binary trees, so understanding it makes those chapters shorter.
In everyday life
Look for AVL tree outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “AVL tree” →

How to study AVL tree in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what AVL tree means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain AVL tree out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is AVL tree in simple terms?

In computer science, an AVL tree (named after inventors Adelson-Velsky and Landis) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by not more than one; if at any time they differ by more than one, rebalancing is done to restore this…

Why does AVL tree matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study AVL tree?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on AVL tree.

Tags

  • 1962 in computing
  • Amortized data structures
  • Binary trees
  • Search trees
  • Soviet inventions

Keep exploring