A complete implementation of a self-balancing AVL (Adelson-Velsky and Landis) tree in C++. This data structure maintains O(log n) time complexity for insertions, deletions, and searches by automatically rebalancing itself after modifications.
- Self-balancing: Automatically maintains balance through rotations
- Generic key-value storage: Integer keys with string values
- Complete API: Insert, remove, find, and height operations
- Memory safe: Proper copy constructor, assignment operator, and destructor (Rule of Three)
- Efficient: O(log n) time complexity for all major operations
AVL Tree in C++/
├── AVLTree.h # Header file with class declaration
├── AVLTree.cpp # Implementation file
├── main.cpp # Example usage and tests
└── README.md # This file
g++ -std=c++11 -o avltree main.cpp AVLTree.cpp
./avltreeclang++ -std=c++11 -o avltree main.cpp AVLTree.cpp
./avltreeCreate a CMakeLists.txt file:
cmake_minimum_required(VERSION 3.10)
project(AVLTree)
set(CMAKE_CXX_STANDARD 11)
add_executable(avltree main.cpp AVLTree.cpp)Then build:
mkdir build
cd build
cmake ..
make
./avltree#include "AVLTree.h"
#include <iostream>
int main() {
AVLTree tree;
// Insert key-value pairs
tree.insert(10, "ten");
tree.insert(20, "twenty");
tree.insert(30, "thirty");
// Search for a key
std::string* result = tree.find(20);
if (result) {
std::cout << "Found: " << *result << std::endl;
}
// Remove a key
tree.remove(20);
// Print all entries in order
tree.printInOrder();
// Get tree height
std::cout << "Height: " << tree.height() << std::endl;
return 0;
}AVLTree()- Creates an empty AVL treeAVLTree(const AVLTree &other)- Copy constructor~AVLTree()- Destructor (automatically cleans up all nodes)
AVLTree& operator=(const AVLTree &other)- Assignment operator
void insert(int key, std::string value)- Insert or update a key-value pairvoid remove(int key)- Remove a key from the treestd::string* find(int key)- Search for a key, returns pointer to value or nullptrint height()- Returns the height of the treevoid printInOrder()- Prints all entries in ascending key order
- Copy
AVLTree.handAVLTree.cppinto your project directory - Include the header in your code:
#include "AVLTree.h" - Compile both files with your project
Compile as a static library:
g++ -c -std=c++11 AVLTree.cpp -o AVLTree.o
ar rcs libavltree.a AVLTree.oThen link in your project:
g++ -std=c++11 your_program.cpp -L. -lavltree -o your_programTo use custom types instead of int keys and std::string values, modify the class to use templates:
template<typename K, typename V>
class AVLTree {
// Modify Node struct and methods accordingly
};- A binary search tree where the heights of left and right subtrees differ by at most 1
- Balance factor = height(left subtree) - height(right subtree)
- Valid balance factors: -1, 0, or 1
The tree uses four types of rotations to maintain balance:
- Left Rotation: Used when right subtree is too tall
- Right Rotation: Used when left subtree is too tall
- Left-Right Rotation: Left rotation on left child, then right rotation on node
- Right-Left Rotation: Right rotation on right child, then left rotation on node
| Operation | Average | Worst Case |
|---|---|---|
| Insert | O(log n) | O(log n) |
| Delete | O(log n) | O(log n) |
| Search | O(log n) | O(log n) |
| Height | O(1) | O(1) |
O(n) where n is the number of nodes in the tree.
Running the included main.cpp produces output demonstrating:
- Basic insertions with automatic rebalancing
- Search operations (successful and unsuccessful)
- Duplicate key handling (value updates)
- Deletion with rebalancing
- Copy constructor functionality
- Assignment operator functionality
- C++11 or later
- Standard C++ compiler (g++, clang++, MSVC, etc.)
This project is provided as-is for educational and commercial use.
Feel free to fork, modify, and use this implementation in your projects. Contributions and improvements are welcome!