0% found this document useful (0 votes)
10 views4 pages

Practical No 10 Faiz

Uploaded by

aniruddhaphadke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Practical No 10 Faiz

Uploaded by

aniruddhaphadke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical No – 10

Aim:- Write C++ Program for Implementation of AVL Tree


Program:- Code For Implementation of AVL Tree
CODE:-
#include <iostream>
using namespace std;

// AVL Node
class AVLNode {
public:
int key;
int height;
AVLNode* left;
AVLNode* right;

AVLNode(int k) : key(k), height(1), left(nullptr), right(nullptr) {}


};

// AVL Tree
class AVLTree {
private:
AVLNode* root;

// Function to get the height of a node


int getHeight(AVLNode* node) {
return (node ? node->height : 0);
}

// Function to update the height of a node


void updateHeight(AVLNode* node) {
if (node) {
node->height = max(getHeight(node->left), getHeight(node->right))
+ 1;
}
}

// Function to get the balance factor of a node


int getBalanceFactor(AVLNode* node) {
return (node ? getHeight(node->left) - getHeight(node->right) : 0);
}
// Function to perform a right rotation
AVLNode* rotateRight(AVLNode* y) {
AVLNode* x = y->left;
AVLNode* T2 = x->right;

x->right = y;
y->left = T2;

updateHeight(y);
updateHeight(x);

return x;
}

// Function to perform a left rotation


AVLNode* rotateLeft(AVLNode* x) {
AVLNode* y = x->right;
AVLNode* T2 = y->left;

y->left = x;
x->right = T2;

updateHeight(x);
updateHeight(y);

return y;
}

// Function to balance a node


AVLNode* balanceNode(AVLNode* node) {
if (!node) {
return nullptr;
}

updateHeight(node);

int balance = getBalanceFactor(node);

// Left Heavy
if (balance > 1) {
if (getBalanceFactor(node->left) < 0) {
node->left = rotateLeft(node->left);
}
return rotateRight(node);
}
// Right Heavy
if (balance < -1) {
if (getBalanceFactor(node->right) > 0) {
node->right = rotateRight(node->right);
}
return rotateLeft(node);
}

return node;
}

// Function to insert a key into the AVL tree


AVLNode* insertKey(AVLNode* node, int key) {
if (!node) {
return new AVLNode(key);
}

if (key < node->key) {


node->left = insertKey(node->left, key);
} else if (key > node->key) {
node->right = insertKey(node->right, key);
} else {
// Duplicate keys are not allowed in this simple implementation
return node;
}

return balanceNode(node);
}

// Function to perform an in-order traversal


void inOrderTraversal(AVLNode* node) {
if (node) {
inOrderTraversal(node->left);
cout << node->key << " ";
inOrderTraversal(node->right);
}
}

public:
AVLTree() : root(nullptr) {}

// Public function to insert a key into the AVL tree


void insert(int key) {
root = insertKey(root, key);
}

// Public function to perform an in-order traversal


void inOrderTraversal() {
inOrderTraversal(root);
cout << endl;
}
};

int main() {
AVLTree myAVLTree;

myAVLTree.insert(10);
myAVLTree.insert(20);
myAVLTree.insert(30);
myAVLTree.insert(40);
myAVLTree.insert(50);
myAVLTree.insert(25);

cout << "In-Order Traversal of AVL Tree: ";


myAVLTree.inOrderTraversal();

return 0;
}

*OUTPUT*

RESULT:- Thus We Have Successfully Implemented AVL-Tree


Program in C++.

You might also like