Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create avl_tree.c++ #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions avl_tree.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include <iostream>
using namespace std;

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

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

class AVLTree {
public:
int height(Node *N) {
if (N == nullptr)
return 0;
return N->height;
}

int max(int a, int b) {
return (a > b) ? a : b;
}

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 *N) {
if (N == nullptr)
return 0;
return height(N->left) - height(N->right);
}

Node* insert(Node *node, int key) {
if (node == nullptr)
return new Node(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);

// Left Left Case
if (balance > 1 && key < node->left->key)
return rightRotate(node);

// Right Right Case
if (balance < -1 && key > node->right->key)
return leftRotate(node);

// Left Right Case
if (balance > 1 && key > node->left->key) {
node->left = leftRotate(node->left);
return rightRotate(node);
}

// Right Left Case
if (balance < -1 && key < node->right->key) {
node->right = rightRotate(node->right);
return leftRotate(node);
}

return node;
}

void preOrder(Node *root) {
if (root != nullptr) {
cout << root->key << " ";
preOrder(root->left);
preOrder(root->right);
}
}
};

int main() {
AVLTree tree;
Node *root = nullptr;

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

cout << "Preorder traversal of the constructed AVL tree is: \n";
tree.preOrder(root);

return 0;
}