0% found this document useful (0 votes)
3 views21 pages

AVLtree

The document contains a C++ implementation of an AVL tree, including node structure, insertion, and various traversal methods (inorder, preorder, postorder) both recursively and non-recursively. It features a menu-driven interface allowing users to interactively insert values and display the tree's contents. Sample output demonstrates the functionality of the AVL tree operations.

Uploaded by

gauravrarbat10
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)
3 views21 pages

AVLtree

The document contains a C++ implementation of an AVL tree, including node structure, insertion, and various traversal methods (inorder, preorder, postorder) both recursively and non-recursively. It features a menu-driven interface allowing users to interactively insert values and display the tree's contents. Sample output demonstrates the functionality of the AVL tree operations.

Uploaded by

gauravrarbat10
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/ 21

Name : Saksham Santosh Chaudhari Div

:B
Batch : B1
Roll No : 88
PRN : 202301040090
AVL tree
Code –
/* Name : Saksham Santosh Chaudhari
PRN : 202301040090 DIV : B PRN :
202301040090*/

#include <iostream>
#include <stack>
using namespace std;

// Node structure for AVL tree


struct Node {
int data;
Node* left;
Node* right;
int height;
};

// Utility function to get the height of a node


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

// Utility function to get the balance factor of a


node
int balanceFactor(Node* node) {
return node ? height(node->left) - height(node-
>right) : 0;
}

// Right rotate utility to balance the tree


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;
}

// Left rotate utility to balance the tree


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;
}

// Insert a node into the AVL tree and balance it


Node* insert(Node* node, int key) {
if (!node) {
Node* newNode = new Node();
newNode->data = key;
newNode->left = newNode->right = nullptr;
newNode->height = 1;
return newNode;
}

if (key < node->data) node->left = insert(node-


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

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

int balance = balanceFactor(node);

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


return rightRotate(node);
if (balance < -1 && key > node->right->data)
return leftRotate(node);
if (balance > 1 && key > node->left->data) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if (balance < -1 && key < node->right->data) {
node->right = rightRotate(node->right);
return leftRotate(node);
}

return node;
}

// Inorder Traversal (Recursive)


void inorderRecursive(Node* root) {
if (root) {
inorderRecursive(root->left);
cout << root->data << " ";
inorderRecursive(root->right);
}
}

// Preorder Traversal (Recursive)


void preorderRecursive(Node* root) {
if (root) {
cout << root->data << " ";
preorderRecursive(root->left);
preorderRecursive(root->right);
}
}

// Postorder Traversal (Recursive)


void postorderRecursive(Node* root) {
if (root) {
postorderRecursive(root->left);
postorderRecursive(root->right);
cout << root->data << " ";
}
}

// Inorder Traversal (Non-recursive)


void inorderNonRecursive(Node* root) {
stack<Node*> s;
Node* current = root;
while (current || !s.empty()) {
while (current) {
s.push(current);
current = current->left;
}
current = s.top();
s.pop();
cout << current->data << " ";
current = current->right;
}
}

// Preorder Traversal (Non-recursive)


void preorderNonRecursive(Node* root) {
if (!root) return;
stack<Node*> s;
s.push(root);
while (!s.empty()) {
Node* current = s.top();
s.pop();
cout << current->data << " ";
if (current->right) s.push(current->right);
if (current->left) s.push(current->left);
}
}

// Postorder Traversal (Non-recursive)


void postorderNonRecursive(Node* root) {
if (!root) return;
stack<Node*> s1, s2;
s1.push(root);
while (!s1.empty()) {
Node* current = s1.top();
s1.pop();
s2.push(current);
if (current->left) s1.push(current->left);
if (current->right) s1.push(current->right);
}
while (!s2.empty()) {
cout << s2.top()->data << " ";
s2.pop();
}
}

// Main function with menu


int main() {
Node* root = nullptr;
int choice, value;

while (true) {
cout << "\nAVL Tree Menu:";
cout << "\n1. Insert into AVL Tree";
cout << "\n2. Inorder Traversal (Recursive)";
cout << "\n3. Preorder Traversal
(Recursive)";
cout << "\n4. Postorder Traversal
(Recursive)";
cout << "\n5. Inorder Traversal (Non-
recursive)";
cout << "\n6. Preorder Traversal (Non-
recursive)";
cout << "\n7. Postorder Traversal (Non-
recursive)";
cout << "\n8. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value to insert: ";
cin >> value;
root = insert(root, value);
break;
case 2:
cout << "Inorder Traversal (Recursive):
";
inorderRecursive(root);
cout << endl;
break;
case 3:
cout << "Preorder Traversal (Recursive):
";
preorderRecursive(root);
cout << endl;
break;
case 4:
cout << "Postorder Traversal
(Recursive): ";
postorderRecursive(root);
cout << endl;
break;
case 5:
cout << "Inorder Traversal (Non-
recursive): ";
inorderNonRecursive(root);
cout << endl;
break;
case 6:
cout << "Preorder Traversal (Non-
recursive): ";
preorderNonRecursive(root);
cout << endl;
break;
case 7:
cout << "Postorder Traversal (Non-
recursive): ";
postorderNonRecursive(root);
cout << endl;
break;
case 8:
exit(0);
default:
cout << "Invalid choice!" << endl;
}
}
return 0;
}

/* Sample Output :

AVL Tree Menu:


1. Insert into AVL Tree
2. Inorder Traversal (Recursive)
3. Preorder Traversal (Recursive)
4. Postorder Traversal (Recursive)
5. Inorder Traversal (Non-recursive)
6. Preorder Traversal (Non-recursive)
7. Postorder Traversal (Non-recursive)
8. Exit
Enter your choice: 1
Enter value to insert: 10

AVL Tree Menu:


1. Insert into AVL Tree
2. Inorder Traversal (Recursive)
3. Preorder Traversal (Recursive)
4. Postorder Traversal (Recursive)
5. Inorder Traversal (Non-recursive)
6. Preorder Traversal (Non-recursive)
7. Postorder Traversal (Non-recursive)
8. Exit
Enter your choice: 1
Enter value to insert: 20

AVL Tree Menu:


1. Insert into AVL Tree
2. Inorder Traversal (Recursive)
3. Preorder Traversal (Recursive)
4. Postorder Traversal (Recursive)
5. Inorder Traversal (Non-recursive)
6. Preorder Traversal (Non-recursive)
7. Postorder Traversal (Non-recursive)
8. Exit
Enter your choice: 1
Enter value to insert: 30

AVL Tree Menu:


1. Insert into AVL Tree
2. Inorder Traversal (Recursive)
3. Preorder Traversal (Recursive)
4. Postorder Traversal (Recursive)
5. Inorder Traversal (Non-recursive)
6. Preorder Traversal (Non-recursive)
7. Postorder Traversal (Non-recursive)
8. Exit
Enter your choice: 1
Enter value to insert: 40

AVL Tree Menu:


1. Insert into AVL Tree
2. Inorder Traversal (Recursive)
3. Preorder Traversal (Recursive)
4. Postorder Traversal (Recursive)
5. Inorder Traversal (Non-recursive)
6. Preorder Traversal (Non-recursive)
7. Postorder Traversal (Non-recursive)
8. Exit
Enter your choice: 1
Enter value to insert: 50
AVL Tree Menu:
1. Insert into AVL Tree
2. Inorder Traversal (Recursive)
3. Preorder Traversal (Recursive)
4. Postorder Traversal (Recursive)
5. Inorder Traversal (Non-recursive)
6. Preorder Traversal (Non-recursive)
7. Postorder Traversal (Non-recursive)
8. Exit
Enter your choice: 1
Enter value to insert: 60

AVL Tree Menu:


1. Insert into AVL Tree
2. Inorder Traversal (Recursive)
3. Preorder Traversal (Recursive)
4. Postorder Traversal (Recursive)
5. Inorder Traversal (Non-recursive)
6. Preorder Traversal (Non-recursive)
7. Postorder Traversal (Non-recursive)
8. Exit
Enter your choice: 1
Enter value to insert: 70

AVL Tree Menu:


1. Insert into AVL Tree
2. Inorder Traversal (Recursive)
3. Preorder Traversal (Recursive)
4. Postorder Traversal (Recursive)
5. Inorder Traversal (Non-recursive)
6. Preorder Traversal (Non-recursive)
7. Postorder Traversal (Non-recursive)
8. Exit
Enter your choice: 1
Enter value to insert: 80

AVL Tree Menu:


1. Insert into AVL Tree
2. Inorder Traversal (Recursive)
3. Preorder Traversal (Recursive)
4. Postorder Traversal (Recursive)
5. Inorder Traversal (Non-recursive)
6. Preorder Traversal (Non-recursive)
7. Postorder Traversal (Non-recursive)
8. Exit
Enter your choice: 2
Inorder Traversal (Recursive): 10 20 30 40 50 60
70 80

AVL Tree Menu:


1. Insert into AVL Tree
2. Inorder Traversal (Recursive)
3. Preorder Traversal (Recursive)
4. Postorder Traversal (Recursive)
5. Inorder Traversal (Non-recursive)
6. Preorder Traversal (Non-recursive)
7. Postorder Traversal (Non-recursive)
8. Exit
Enter your choice: 3
Preorder Traversal (Recursive): 40 20 10 30 60 50
70 80

AVL Tree Menu:


1. Insert into AVL Tree
2. Inorder Traversal (Recursive)
3. Preorder Traversal (Recursive)
4. Postorder Traversal (Recursive)
5. Inorder Traversal (Non-recursive)
6. Preorder Traversal (Non-recursive)
7. Postorder Traversal (Non-recursive)
8. Exit
Enter your choice: 4
Postorder Traversal (Recursive): 10 30 20 50 80 70
60 40

AVL Tree Menu:


1. Insert into AVL Tree
2. Inorder Traversal (Recursive)
3. Preorder Traversal (Recursive)
4. Postorder Traversal (Recursive)
5. Inorder Traversal (Non-recursive)
6. Preorder Traversal (Non-recursive)
7. Postorder Traversal (Non-recursive)
8. Exit
Enter your choice: 5
Inorder Traversal (Non-recursive): 10 20 30 40 50
60 70 80

AVL Tree Menu:


1. Insert into AVL Tree
2. Inorder Traversal (Recursive)
3. Preorder Traversal (Recursive)
4. Postorder Traversal (Recursive)
5. Inorder Traversal (Non-recursive)
6. Preorder Traversal (Non-recursive)
7. Postorder Traversal (Non-recursive)
8. Exit
Enter your choice: 6
Preorder Traversal (Non-recursive): 40 20 10 30
60 50 70 80

AVL Tree Menu:


1. Insert into AVL Tree
2. Inorder Traversal (Recursive)
3. Preorder Traversal (Recursive)
4. Postorder Traversal (Recursive)
5. Inorder Traversal (Non-recursive)
6. Preorder Traversal (Non-recursive)
7. Postorder Traversal (Non-recursive)
8. Exit
Enter your choice: 7
Postorder Traversal (Non-recursive): 10 30 20 50
80 70 60 40

AVL Tree Menu:


1. Insert into AVL Tree
2. Inorder Traversal (Recursive)
3. Preorder Traversal (Recursive)
4. Postorder Traversal (Recursive)
5. Inorder Traversal (Non-recursive)
6. Preorder Traversal (Non-recursive)
7. Postorder Traversal (Non-recursive)
8. Exit
Enter your choice: 8

--------------------------------
Process exited after 36.56 seconds with return
value 0
Press any key to continue . . .
*/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy