0% found this document useful (0 votes)
5 views7 pages

Avl

The document outlines a program for an AVL Tree-based dictionary that allows for the insertion, display, and management of keyword-value pairs. It includes functionalities for displaying the data in both inorder and level order, as well as maintaining balance during insertions. The program is designed to handle user inputs through a menu-driven interface.

Uploaded by

sshahidss978
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)
5 views7 pages

Avl

The document outlines a program for an AVL Tree-based dictionary that allows for the insertion, display, and management of keyword-value pairs. It includes functionalities for displaying the data in both inorder and level order, as well as maintaining balance during insertions. The program is designed to handle user inputs through a menu-driven interface.

Uploaded by

sshahidss978
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/ 7

1.

​ Name: Ojas Jayant Panse


2.​ Roll No.: SA43
3.​ Batch: B
4.​ A Dictionary stores keywords and its meanings. Provide facility for adding
new keywords, deleting keywords, updating values of any entry. Provide a facility to
Display whole data sorted in ascending/ Descending order. Also find how many
Maximum comparisons may require for finding any keyword. Use Height balance tree
and find the complexity for finding a keyword.
5. Program:

#include <bits/stdc++.h>
using namespace std;

class AVLTree {
private:
struct Node {
string key;
string value;
int balanceFactor;
Node* left;
Node* right;
Node(string k, string v) : key(k), value(v), balanceFactor(0), left(nullptr), right(nullptr) {}
};

Node* root;

int height(Node* node) {


if (node == nullptr) return 0;
return 1 + max(height(node->left), height(node->right));
}

int getBalance(Node* node) {


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

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

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

y->balanceFactor = height(y->left) - height(y->right);


x->balanceFactor = height(x->left) - height(x->right);

return x;
}

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

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

x->balanceFactor = height(x->left) - height(x->right);


y->balanceFactor = height(y->left) - height(y->right);

return y;
}

Node* insert(Node* node, string key, string value) {


if (node == nullptr)
return new Node(key, value);

if (key < node->key)


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

int leftHeight = height(node->left);


int rightHeight = height(node->right);
node->balanceFactor = leftHeight - rightHeight;

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


return rightRotate(node);

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


return leftRotate(node);

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


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

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


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

return node;
}

void displayWithBalance(Node* root, const string& order) {


if (order == "inorder") {
if (root != nullptr) {
displayWithBalance(root->left, order);
cout << root->key << " : " << root->value << " : " << root->balanceFactor << "\n";
displayWithBalance(root->right, order);
}
} else if (order == "levelorder") {
if (root == nullptr) return;
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* curr = q.front();
q.pop();
cout << curr->key << " : " << curr->value << " : " << curr->balanceFactor << "\n";
if (curr->left != nullptr) q.push(curr->left);
if (curr->right != nullptr) q.push(curr->right);
}
}
}

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

void insert(string key, string value) {


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

void displayInorder() {
displayWithBalance(root, "inorder");
}

void displayLevelOrder() {
displayWithBalance(root, "levelorder");
}
};

int main() {
AVLTree tree;
int choice;
string key;
string value;

while (true) {
cout << "\nMenu:\n";
cout << "1. Insert\n";
cout << "2. Display Inorder (with balance factor)\n";
cout << "3. Display Level Order (with balance factor)\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter key to insert: ";
cin >> key;
cout << "Enter value: ";
cin >> value;
cout << "Tree before insertion:\n";
tree.displayInorder();
cout << "\n";
tree.insert(key, value);
cout << "Tree after insertion:\n";
tree.displayInorder();
cout << "\nKey-value pair inserted.\n";
break;
case 2:
cout << "Inorder traversal with balance factors: \n";
tree.displayInorder();
break;
case 3:
cout << "Level order traversal with balance factors: \n";
tree.displayLevelOrder();
break;
case 4:
cout << "Exiting...\n";
return 0;
default:
cout << "Invalid choice. Try again.\n";
}
}
return 0;
}

OUTPUT:
Menu:
1. Insert
2. Display Inorder (with balance factor)
3. Display Level Order (with balance factor)
4. Exit
Enter your choice: 1
Enter key to insert: H
Enter value: h
Tree before insertion:

Tree after insertion:


H:h:0

Key-value pair inserted.

Menu:
1. Insert
2. Display Inorder (with balance factor)
3. Display Level Order (with balance factor)
4. Exit
Enter your choice: 1
Enter key to insert: K
Enter value: k
Tree before insertion:
H:h:0

Tree after insertion:


H : h : -1
K:k:0

Key-value pair inserted.

Menu:
1. Insert
2. Display Inorder (with balance factor)
3. Display Level Order (with balance factor)
4. Exit
Enter your choice: 1
Enter key to insert: O
Enter value: o
Tree before insertion:
H : h : -1
K:k:0

Tree after insertion:


H:h:0
K:k:0
O:o:0

Key-value pair inserted.

Menu:
1. Insert
2. Display Inorder (with balance factor)
3. Display Level Order (with balance factor)
4. Exit
Enter your choice: 1
Enter key to insert: E
Enter value: e
Tree before insertion:
H:h:0
K:k:0
O:o:0

Tree after insertion:


E:e:0
H:h:1
K:k:1
O:o:0

Key-value pair inserted.

Menu:
1. Insert
2. Display Inorder (with balance factor)
3. Display Level Order (with balance factor)
4. Exit
Enter your choice: 1
Enter key to insert: F
Enter value: f
Tree before insertion:
E:e:0
H:h:1
K:k:1
O:o:0

Tree after insertion:


E:e:0
F:f:0
H:h:0
K:k:1
O:o:0

Key-value pair inserted.

Menu:
1. Insert
2. Display Inorder (with balance factor)
3. Display Level Order (with balance factor)
4. Exit
Enter your choice: 3
Level order traversal with balance factors:
K:k:1
F:f:0
O:o:0
E:e:0
H:h:0

Menu:
1. Insert
2. Display Inorder (with balance factor)
3. Display Level Order (with balance factor)
4. Exit
Enter your choice:

You might also like