Lably
Skip to content

AA tree

Form of balanced tree

  • 7 min read
  • 4 chapters

Related on Lably

Through Computer science

AA trees are a variation of the red–black tree, a form of binary search tree which supports efficient addition and deletion of entries. Unlike red–black trees, red nodes on an AA tree can only be added as a right subchild. In other words, no red node can be a left sub-child. This results in the simulation of a 2–3 tree instead of a 2–3–4 tree, which greatly simplifies the maintenance operations. The maintenance algorithms for a red–black tree need to consider seven different shapes to properly balance the tree:

An AA tree on the other hand only needs to consider two shapes due to the strict requirement that only right links can be red:

Balancing rotations

Whereas red–black trees require one bit of balancing metadata per node (the color), AA trees require O(log(log(N))) bits of metadata per node, in the form of an integer "level". The following invariants hold for AA trees:

  1. The level of every leaf node is one.
  2. The level of every left child is exactly one less than that of its parent.
  3. The level of every right child is equal to or one less than that of its parent.
  4. The level of every right grandchild is strictly less than that of its grandparent.
  5. Every node of level greater than one has two children.

A link where the child's level is equal to that of its parent is called a horizontal link, and is analogous to a red link in the red–black tree. Individual right horizontal links are allowed, but consecutive ones are forbidden; all left horizontal links are forbidden. These are more restrictive constraints than the analogous ones on red–black trees, with the result that re-balancing an AA tree is procedurally much simpler than re-balancing a red–black tree.

Insertions and deletions may transiently cause an AA tree to become unbalanced (that is, to violate the AA tree invariants). Only two distinct operations are needed for restoring balance: "skew" and "split". Skew is a right rotation to replace a subtree containing a left horizontal link with one containing a right horizontal link instead. Split is a left rotation and level increase to replace a subtree containing two or more consecutive right horizontal links with one containing two fewer consecutive right horizontal links. Implementation of balance-preserving insertion and deletion is simplified by relying on the skew and split operations to modify the tree only if needed, instead of making their callers decide whether to skew or split.

<b id="mwKA">function</b> skew <b id="mwKQ">is</b> <b id="mwKg">input:</b> T, a node representing an AA tree that needs to be rebalanced. <b id="mwKw">output:</b> Another node representing the rebalanced AA tree. <b id="mwLA">if</b> nil(T) <b id="mwLQ">then</b> <b id="mwLg">return</b> Nil <b id="mwLw">else if</b> nil(left(T)) <b id="mwMA">then</b> <b id="mwMQ">return</b> T <b id="mwMg">else if</b> level(left(T)) == level(T) <b id="mwMw">then</b> <i id="mwNA">Swap the pointers of horizontal left links.</i> L = left(T) left(T) := right(L) right(L) := T <b id="mwNQ">return</b> L <b id="mwNg">else</b> <b id="mwNw">return</b> T <b id="mwOA">end if</b> <b id="mwOQ">end function</b>

Skew:

<b id="mwPw">function</b> split <b id="mwQA">is</b> <b id="mwQQ">input:</b> T, a node representing an AA tree that needs to be rebalanced. <b id="mwQg">output:</b> Another node representing the rebalanced AA tree. <b id="mwQw">if</b> nil(T) <b id="mwRA">then</b> <b id="mwRQ">return</b> Nil <b id="mwRg">else if</b> nil(right(T)) <b id="mwRw">or </b> nil(right(right(T))) <b id="mwSA">then</b> <b id="mwSQ">return</b> T <b id="mwSg">else if</b> level(T) == level(right(right(T))) <b id="mwSw">then</b> <i id="mwTA">We have two horizontal right links. Take the middle node, elevate it, and return it.</i> R = right(T) right(T) := left(R) left(R) := T level(R) := level(R) + 1 <b id="mwTQ">return</b> R <b id="mwTg">else</b> <b id="mwTw">return</b> T <b id="mwUA">end if</b> <b id="mwUQ">end function</b>

Split:

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication