Main Contains Menu
*/
int main()
{
int choice, item;
avlTree avl;
while (1)
{
cout << "\n---------------------" << endl;
cout << "AVL Tree Implementation" << endl;
cout << "\n---------------------" << endl;
cout << "1.Insert Element into the tree" << endl;
cout << "2.Display Balanced AVL Tree" << endl;
cout << "3.InOrder traversal" << endl;
cout << "4.PreOrder traversal" << endl;
cout << "5.PostOrder traversal" << endl;
cout << "6.Exit" << endl;
cout << "Enter your Choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter value to be inserted: ";
cin >> item;
root = avl.insert(root, item);
break;
case 2:
if (root == NULL)
{
cout << "Tree is Empty" << endl;
continue;
}
cout << "Balanced AVL Tree:" << endl;
avl.display(root, 1);
break;
case 3:
cout << "Inorder Traversal:" << endl;
avl.inorder(root);
cout << endl;
break;
case 4:
cout << "Preorder Traversal:" << endl;
avl.preorder(root);
cout << endl;
break;
case 5:
cout << "Postorder Traversal:" << endl;
avl.postorder(root);
cout << endl;
break;
case 6:
exit(1);
break;
default:
cout << "Wrong Choice" << endl;
}
}
return 0;
}
/*
Height of AVL Tree
/
int avlTree::height(avl_node temp)
{
int h = 0;
if (temp != NULL)
{
int l_height = height(temp->left);
int r_height = height(temp->right);
int max_height = max(l_height, r_height);
h = max_height + 1;
}
return h;
}
/*
Height Difference
/
int avlTree::diff(avl_node temp)
{
int l_height = height(temp->left);
int r_height = height(temp->right);
int b_factor = l_height - r_height;
return b_factor;
}
include
include
include
include
define pow2(n) (1 << (n))
using namespace std;
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
}
/*