MCS-208 · Unit 7

Advanced Trees

Section 9 of 9

Block 3 · Unit 7Advanced Trees12 min · Section 9 of 9

7.9AA-Trees and Treaps

Covers IGNOU MCS-208 §7.7, 7.8

Red-black trees have many cases, especially for deletion. The AA-tree (named after its inventor, Arne Andersson, 1993) is a red-black tree with one extra restriction that removes most of those cases. Instead of colours it stores a level in each node, and only two simple operations, skew and split, keep it balanced.

A link from a node to a right child on the same level is called a horizontal link (it corresponds to a red child in a red-black tree). The rules say: horizontal links may only go to the right (no red left children), and two horizontal links may not follow each other (no red-red).

Skew and split

  • Skew removes a left horizontal link with a single right rotation.
  • Split removes two consecutive right horizontal links with a single left rotation; the middle node moves up one level.

Insertion adds the new key as a leaf at level 1, then on the way back up the search path calls skew and then split at each node. Deletion is similar, with level decreases followed by a few skews and splits.

Treaps

A treap (“tree” + “heap”) stores in every node a key and a randomly chosen priority. It is a binary search tree with respect to the keys and a heap with respect to the priorities: each node’s priority is at least as large as its parent’s (so the smallest priority is at the root). Insertion adds the node as a BST leaf and then rotates it up while its priority is smaller than its parent’s. Because the priorities are random, the tree behaves like a BST built from keys in random order: expected height O(log⁡n)O(\log n), with very simple code.

Comparing the balanced search trees

TreeBalance informationGuaranteeBest for
AVLHeight or balance factorWorst case O(log⁡n)O(\log n), height ≤ 1.44 log nSearch-heavy, in memory
Red-black1 colour bitWorst case O(log⁡n)O(\log n), height ≤ 2 log nUpdate-heavy, libraries
AA-treeLevel (small integer)Worst case O(log⁡n)O(\log n)Simple code for a red-black-like tree
SplayNoneAmortized O(log⁡n)O(\log n)Repeated access to the same keys
TreapRandom priorityExpected O(log⁡n)O(\log n)Simple randomised balancing
B-treeKey counts per nodeWorst case O(log⁡mn)O(\log_m n)Disks and databases

Key takeaways

  • AA-tree: levels instead of colours; horizontal links only to the right, never two in a row.
  • Skew = right rotation removing a left horizontal link; split = left rotation, middle node up a level.
  • Insertion: add a level-1 leaf, then skew and split back up the path.
  • Treap: BST on keys + heap on random priorities: expected O(log⁡n)O(\log n).

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What does skew do in an AA-tree?

Show answer

Answer: B. Removes a left horizontal link with a right rotation

Left horizontal links are not allowed; skew turns one into a right horizontal link. Removing two right links is split.

Question 2 · multiple choice

Which situation calls for split?

Show answer

Answer: C. A node, its right child and its right grandchild are all on the same level.

Two consecutive right horizontal links are forbidden; split lifts the middle node one level. A single right horizontal link is allowed.

Question 3 · multiple choice

What is the level of every leaf in an AA-tree?

Show answer

Answer: A. 1

Rule 1. A leaf that is a right child may share level 1 with its parent through a horizontal link.

Question 4 · multiple choice

In a treap, which two orders hold at the same time?

Show answer

Answer: D. BST order on the keys and heap order on the priorities

Inorder gives sorted keys; along every root-to-leaf path the priorities increase (with the smallest at the root).

Question 5 · multiple choice

What does this program print?

C · q7-aa.c
#include <stdio.h>

int main(void)
{
    /* levels of a node t, its right child r and right grandchild rr */
    int t = 1, r = 1, rr = 1;
    int need_split = (r == t && rr == t);
    int new_level_of_r = need_split ? r + 1 : r;
    printf("split=%d level of middle node=%d\n", need_split, new_level_of_r);
    return 0;
}
Show answer

Answer: C. split=1 level of middle node=2

Three nodes on the same level joined by two right links need a split, and the middle node moves up to level 2.

Question 6 · short answer

Insert 1, 2, 3 into an empty AA-tree and explain each skew and split.

Show worked answer

Insert 1: a leaf at level 1. Insert 2: 2 > 1, so 2 is 1’s right child at level 1, a single right horizontal link (allowed); skew and split do nothing. Insert 3: 3 becomes 2’s right child at level 1. On the way back up, at node 1 there is no left horizontal link (no skew), but 1 → 2 → 3 are two consecutive right horizontal links, so split at 1: a left rotation makes 2 the root and 2 moves up to level 2, with children 1 and 3 at level 1. Final tree: 2/2(1/1, 3/1).

↑ ↓ move↵ openTab switch type