AA Trees in C/C++?

An AA tree in computer science is defined as a form of balanced binary search tree designed for storing and retrieving ordered data efficiently. AA trees are a variation of red-black trees but with simplified balancing rules. Unlike red-black trees, red nodes (horizontal links) in AA trees can only exist as right children, never left children.

Key Properties

AA trees maintain the following invariants −

  • The level of every leaf node is one
  • The level of every left child is exactly one smaller than its parent
  • The level of every right child is equal to or one smaller than its parent
  • The level of every right grandchild is strictly smaller than its grandparent
  • Every node of level greater than one has two children

Advantages Over Red-Black Trees

While red-black trees require considering seven different shapes for balancing, AA trees only need two operations: skew and split. This makes implementation significantly simpler.

AA Tree Balancing Operations Skew (Right Rotation) A B ? B A Split (Left Rotation + Level Up) A B C ? B A C

Basic AA Tree Structure

Here's a basic implementation of an AA tree node structure −

#include <stdio.h>
#include <stdlib.h>

typedef struct AANode {
    int data;
    int level;
    struct AANode* left;
    struct AANode* right;
} AANode;

AANode* createNode(int data) {
    AANode* node = (AANode*)malloc(sizeof(AANode));
    if (node == NULL) return NULL;
    
    node->data = data;
    node->level = 1;
    node->left = NULL;
    node->right = NULL;
    return node;
}

int getLevel(AANode* node) {
    return (node == NULL) ? 0 : node->level;
}

int main() {
    AANode* root = createNode(10);
    printf("AA Tree node created with data: %d, level: %d\n", root->data, root->level);
    
    free(root);
    return 0;
}
AA Tree node created with data: 10, level: 1

Skew Operation

The skew operation eliminates left horizontal links by performing a right rotation −

#include <stdio.h>
#include <stdlib.h>

typedef struct AANode {
    int data;
    int level;
    struct AANode* left;
    struct AANode* right;
} AANode;

int getLevel(AANode* node) {
    return (node == NULL) ? 0 : node->level;
}

AANode* skew(AANode* root) {
    if (root == NULL) return NULL;
    if (root->left == NULL) return root;
    
    if (getLevel(root->left) == getLevel(root)) {
        /* Right rotation */
        AANode* leftChild = root->left;
        root->left = leftChild->right;
        leftChild->right = root;
        return leftChild;
    }
    return root;
}

AANode* createNode(int data) {
    AANode* node = (AANode*)malloc(sizeof(AANode));
    node->data = data;
    node->level = 1;
    node->left = node->right = NULL;
    return node;
}

int main() {
    AANode* root = createNode(20);
    root->left = createNode(10);
    root->left->level = 2;  /* Create horizontal left link */
    root->level = 2;
    
    printf("Before skew: Root = %d\n", root->data);
    root = skew(root);
    printf("After skew: Root = %d\n", root->data);
    
    free(root->right);
    free(root);
    return 0;
}
Before skew: Root = 20
After skew: Root = 10

Split Operation

The split operation eliminates consecutive right horizontal links by performing a left rotation and incrementing the level −

#include <stdio.h>
#include <stdlib.h>

typedef struct AANode {
    int data;
    int level;
    struct AANode* left;
    struct AANode* right;
} AANode;

int getLevel(AANode* node) {
    return (node == NULL) ? 0 : node->level;
}

AANode* split(AANode* root) {
    if (root == NULL) return NULL;
    if (root->right == NULL || root->right->right == NULL) return root;
    
    if (getLevel(root) == getLevel(root->right->right)) {
        /* Left rotation and level increment */
        AANode* rightChild = root->right;
        root->right = rightChild->left;
        rightChild->left = root;
        rightChild->level++;
        return rightChild;
    }
    return root;
}

AANode* createNode(int data) {
    AANode* node = (AANode*)malloc(sizeof(AANode));
    node->data = data;
    node->level = 1;
    node->left = node->right = NULL;
    return node;
}

int main() {
    AANode* root = createNode(10);
    root->right = createNode(20);
    root->right->right = createNode(30);
    /* All nodes at same level to create consecutive horizontal links */
    root->level = root->right->level = root->right->right->level = 1;
    
    printf("Before split: Root = %d, Level = %d\n", root->data, root->level);
    root = split(root);
    printf("After split: Root = %d, Level = %d\n", root->data, root->level);
    
    return 0;
}
Before split: Root = 10, Level = 1
After split: Root = 20, Level = 2

Comparison with Red-Black Trees

Aspect AA Tree Red-Black Tree
Balancing Operations 2 (skew, split) 7 different cases
Implementation Complexity Simple Complex
Metadata per Node Integer level 1 color bit
Tree Simulation 2-3 tree 2-3-4 tree

Conclusion

AA trees provide a simpler alternative to red-black trees with easier implementation and maintenance. The restriction to only right horizontal links reduces the complexity from seven balancing cases to just two operations: skew and split.

Updated on: 2026-03-15T12:40:18+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements