ADVANCED DATA STRUCTURES PROGRAM • LEVEL 14 — BALANCED BINARY SEARCH TREES
Validate AVL Height Balance
Learn how to validate avl height balance using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
Balanced = Yes
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
#include <stdlib.h>
struct Node { int key, height; struct Node *left, *right; };
int height(struct Node *node) { return node ? node->height : 0; }
int maximum(int a, int b) { return a > b ? a : b; }
struct Node *new_node(int key) { struct Node *node = malloc(sizeof *node); if (!node) exit(EXIT_FAILURE); node->key = key; node->height = 1; node->left = node->right = NULL; return node; }
void update(struct Node *node) { node->height = 1 + maximum(height(node->left), height(node->right)); }
struct Node *rotate_right(struct Node *root) { struct Node *pivot = root->left, *middle = pivot->right; pivot->right = root; root->left = middle; update(root); update(pivot); return pivot; }
struct Node *rotate_left(struct Node *root) { struct Node *pivot = root->right, *middle = pivot->left; pivot->left = root; root->right = middle; update(root); update(pivot); return pivot; }
struct Node *balance_node(struct Node *root)
{
update(root); int balance = height(root->left) - height(root->right);
if (balance > 1) { if (height(root->left->left) < height(root->left->right)) root->left = rotate_left(root->left); return rotate_right(root); }
if (balance < -1) { if (height(root->right->right) < height(root->right->left)) root->right = rotate_right(root->right); return rotate_left(root); }
return root;
}
struct Node *insert(struct Node *root, int key) { if (!root) return new_node(key); if (key < root->key) root->left = insert(root->left, key); else if (key > root->key) root->right = insert(root->right, key); else return root; return balance_node(root); }
struct Node *minimum_node(struct Node *root) { while (root->left) root = root->left; return root; }
struct Node *delete_key(struct Node *root, int key)
{
if (!root) return NULL;
if (key < root->key) root->left = delete_key(root->left, key);
else if (key > root->key) root->right = delete_key(root->right, key);
else if (!root->left || !root->right) { struct Node *child = root->left ? root->left : root->right; free(root); return child; }
else { struct Node *next = minimum_node(root->right); root->key = next->key; root->right = delete_key(root->right, next->key); }
return balance_node(root);
}
void inorder(struct Node *root) { if (root) { inorder(root->left); printf("%d ", root->key); inorder(root->right); } }
void free_tree(struct Node *root) { if (root) { free_tree(root->left); free_tree(root->right); free(root); } }
int check(struct Node *root) { if (!root) return 0; int left = check(root->left), right = check(root->right); if (left < 0 || right < 0 || left - right > 1 || right - left > 1) return -1; return 1 + maximum(left, right); }
int main(void)
{
int keys[] = {30, 20, 40, 10, 25, 35, 50}; struct Node *root = NULL;
for (int i = 0; i < 7; i++) root = insert(root, keys[i]);
printf("Balanced = %s\n", check(root) >= 0 ? "Yes" : "No"); free_tree(root); return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Balanced = Yes
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Return subtree heights bottom-up and reject any node whose two heights differ by more than one.
- Display the computed result.
Return subtree heights bottom-up and reject any node whose two heights differ by more than one.
EFFICIENCY
Time and space complexity
Time complexity
O(n)
Auxiliary space
O(h)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
