AVL Tree
AVL Tree
Time Space
Operation Description Complexity Complexity
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height
= max(getHeight(y->left), getHeight(y->right)) + 1;
x->height
= max(getHeight(x->left), getHeight(x->right)) + 1;
return x;
}
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height
= max(getHeight(x->left), getHeight(x->right)) + 1;
y->height
= max(getHeight(y->left), getHeight(y->right)) + 1;
return y;
}
// Main function
int main()
{
struct Node* root = NULL;
// Inserting nodes
root = insert(root, 1);
root = insert(root, 2);
root = insert(root, 4);
root = insert(root, 5);
root = insert(root, 6);
root = insert(root, 3);
return 0;
}
Output
Inorder traversal of AVL tree: 1 2 3 4 5 6