1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#include <iostream>
#include <fstream>
#include <Windows.h>
using namespace std;
struct node{
int element;
int balance;
node *left;
node *right;
};
int FindBalance( node * );
node *insert( node * , node * );
//void delnode( int , nodeptr & );
//void find( int , nodeptr & );
node *leftleft( node * );
node *rightright( node * );
node *leftright( node * );
node *rightleft ( node * );
int FindBalace( node *root ){
if( root == NULL )
return -1;//return with error
else
return root->balance;
}
node *insert( node *leaf , node *root ){
if( root == NULL )
return leaf;
else if( leaf->element < root->element ){
root->left = insert( leaf , root->left );
if( FindBalance( root->left ) - FindBalance( root->right ) == 2 ){
if( leaf->element < root->left->element )
root = leftleft( root );
else
root = leftright( root );
}
}
else if( leaf->element > root->element ){
root->right = insert( leaf , root->right );
if( FindBalance ( root->right ) - FindBalance( root->left ) == 2 ){
if( leaf->element > root->right->element )
root = rightright( root );
else
root = rightleft( root );
}
}
root->balance = max( FindBalance( root->left ) , FindBalance( root->right ) ) + 1;
return root;
}
node *leftleft( node *rotate ){
node *pivot = rotate->left;
rotate->left = pivot->right;
pivot->right = rotate;
rotate->balance = max( FindBalance( rotate->left ) , FindBalance ( rotate->right ) ) + 1;
pivot->balance = max( FindBalance ( pivot->left ) , FindBalance ( rotate->right ) ) + 1;
return pivot;
}
node *rightright( node *rotate ){
node *pivot = rotate->right;
rotate->right = pivot->left;
pivot->left = rotate;
rotate->balance = max( FindBalance( rotate->left ) , FindBalance ( rotate->right ) ) + 1;
pivot->balance = max( FindBalance ( pivot->right ) , FindBalance ( rotate->left ) ) + 1;
return pivot;
}
node *leftright( node *rotatetop ){
rotatetop->left = rightright( rotatetop->left );
return leftleft( rotatetop );
}
node *rightleft( node *rotatetop ){
rotatetop->right = leftleft( rotatetop->right );
return rightright( rotatetop );
}
node *readFile( ifstream &inFile ){
node *root = NULL;
node *leaf;
while( !inFile.eof() ){
leaf = new node;
inFile >> leaf->element ;
leaf->balance = 0;
leaf->right = leaf->left = NULL;
if( root == NULL )
root = leaf;
root = insert( leaf , root );
}
return root;
}
int main(){
char choice ='a';
node *root = NULL;
node *leaf;
leaf = new node;
do{
system( "cls" );
cout << "\n\t\t\t\tWELCOME TO AVL TREE" << endl;
cout << "\t\t\t\t:::::::::::::::::::\n" << endl;
cout << "\t\t::::::::::::::::::::::::::::::::::::::::::::::::" << endl;
cout << "\t\t::::Enter 1 to insert a new node" << endl;
cout << "\t\t::::Enter 2 to delete a value" << endl;
cout << "\t\t::::Enter 3 to Calculate balances after rotation" << endl;
cout << "\t\t::::Enter 4 to Calculate height of sub-tree" << endl;
cout << "\t\t::::Enter 5 to display the height of the tree" << endl;
cout << "\t\t::::Enter 6 to search a value" << endl;
cout << "\t\t::::Enter 0 to exit" << endl;
cout << "\t\t::::::::::::::::::::::::::::::::::::::::::::::::\n" << endl;
cout<<"\nEnter the choice: ";
cin>>choice;
switch( choice ){
case '0':
break;
case '1':
cout << "Enter node element : ";
cin >> leaf->element;
while( cin.fail() ){
cin.clear();
cin.ignore( 100 , '\n' );
cout << "Cannot enter alphabet ! Please re-enter ! " << endl;
cout << "Enter node element : ";
cin >> leaf->element;
}
root = insert( leaf , root );
break;
case '2':
break;
case '3':
break;
case '4':
break;
case '5':
break;
case '6':
break;
}
}while( choice != '0' && choice != '1' && choice != '2' && choice != '3' && choice != '4' && choice != '5' );
system( "pause" );
return 0;
}
|