0% found this document useful (0 votes)
11 views3 pages

Avl Tree

Uploaded by

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

Avl Tree

Uploaded by

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

#include <iostream>

using namespace std;

class Node {
public:
int key;
Node* left;
Node* right;
int height;
};

class AVLTree {
public:
Node* root;

AVLTree() {
root = nullptr;
}

int height(Node* node) {


if (node == nullptr)
return 0;
return node->height;
}

int max(int a, int b) {


return (a > b) ? a : b;
}

Node* newNode(int key) {


Node* node = new Node();
node->key = key;
node->left = nullptr;
node->right = nullptr;
node->height = 1;
return node;
}

Node* rightRotate(Node* y) {
Node* x = y->left;
Node* T2 = x->right;

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

y->height = max(height(y->left), height(y->right)) + 1;


x->height = max(height(x->left), height(x->right)) + 1;

return x;
}

Node* leftRotate(Node* x) {
Node* y = x->right;
Node* T2 = y->left;

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

x->height = max(height(x->left), height(x->right)) + 1;


y->height = max(height(y->left), height(y->right)) + 1;

return y;
}

int getBalance(Node* node) {


if (node == nullptr)
return 0;
return height(node->left) - height(node->right);
}

Node* insert(Node* node, int key) {


if (node == nullptr)
return newNode(key);

if (key < node->key)


node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else
return node;

node->height = 1 + max(height(node->left), height(node->right));

int balance = getBalance(node);

if (balance > 1 && key < node->left->key)


return rightRotate(node);

if (balance < -1 && key > node->right->key)


return leftRotate(node);

if (balance > 1 && key > node->left->key) {


node->left = leftRotate(node->left);
return rightRotate(node);
}

if (balance < -1 && key < node->right->key) {


node->right = rightRotate(node->right);
return leftRotate(node);
}

return node;
}

void inorderTraversal(Node* root) {


if (root == nullptr)
return;

inorderTraversal(root->left);
cout << root->key << " ";
inorderTraversal(root->right);
}

void postorderTraversal(Node* root) {


if (root == nullptr)
return;

postorderTraversal(root->left);
postorderTraversal(root->right);
cout << root->key << " ";
}

void preorderTraversal(Node* root) {


if (root == nullptr)
return;

cout << root->key << " ";


preorderTraversal(root->left);
preorderTraversal(root->right);
}

void insert(int key) {


root = insert(root, key);
}
};

int main() {
AVLTree tree;

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

cout << "Inorder traversal: ";


tree.inorderTraversal(tree.root);
cout << endl;

cout << "Postorder traversal: ";


tree.postorderTraversal(tree.root);
cout << endl;

cout << "Preorder traversal: ";


tree.preorderTraversal(tree.root);
cout << endl;

return 0;
}

You might also like