Thursday, July 28, 2011

Height Balance Tree with coord

//compiled with Code:Block 10.05
#include<iostream>
#include<cmath>
#include<conio.h>
#include<windows.h>
using namespace std;

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

ptr Insert(int x, ptr p);
ptr findMin(ptr root);
ptr Delete(int x, ptr p);
int GetMaxDepth(ptr root, int depth);
void GoToXY(int x,int y);
void Print(ptr root,int d,int c,int swing);
ptr RotateLeft(ptr x);
ptr RotateRight(ptr x);
int BalanceFactor(ptr x);
ptr RebalanceTree(ptr x);
template<class T>
void tempStore(T* R);

int main()
{
char j = '1';
ptr Root = 0;
int h;
char in[8];
in[0] = 'C' ;
//cout<<"1.Input\n2.Delete\n   Type answer : ";
do
{
cout<<"1.Input\n2.Delete\n  Type answer : ";
cin>>j;
if (j == '1')
{
system("cls");
while (true)
{
system("cls");
cout<<"Enter integer number, X or x for select menu: ";
cin>>in;
if (in[0] == 'x' || in[0] == 'X')
break;
h= atoi(in);
Root  = Insert(h,Root);
tempStore(Root );
getch();
}
}
else
{
if (j =='2')
{
while(true)
{
system("cls");
cout<<"Enter Value to Delete, X or x for select menu: ";
cin>>in;
if (in[0] == 'x' || in[0] == 'X')
break;
h= atoi(in);
Root  = Delete(h,Root);
tempStore(Root );
getch();
system("cls");
}
}

}
}while(j>= 'a' || j<='z' || j>='A' || j<='Z' );
}

ptr Insert (int x, ptr p)
{
ptr temp;
if (!p)
{
temp = new Node;
temp->data = x;
temp->left = temp->right = 0;
temp->height= 0;
return temp;
}
else
{
if( x < p->data)
p->left=Insert(x,p->left);
else
{
if( x > p->data)
p->right=Insert(x,p->right);
}
}
p->height = BalanceFactor(p);
if (abs(p->height) >1 )
p = RebalanceTree(p);
return p;
}

ptr findMin(ptr root)
{
if (root->left)
return findMin(root->left);
else
return root;
}

ptr Delete(int x, ptr p)
{
ptr temp;
if (!p)
return 0;
else
{
if ( x > p->data)
{
temp = Delete(x,p->right);
p->right = temp;
}
else
{
if (  x < p->data)
{
temp = Delete(x,p->left);
p->left= temp;
}
else
{
if (p->left && p->right)
{
ptr swap = findMin(p->right);
p->data = swap->data;
temp = Delete(swap->data,p->right);
p->right = temp;
}
else
{
ptr a = (p->right)?p->right:p->left;
p = 0;
delete p;
return a;

}
}
}
}
p->height = BalanceFactor(p);
if (abs(p->height) >1 )
p = RebalanceTree(p);
return p;
}

int GetMaxDepth(ptr root, int depth)
{
if (!root)
return depth -1;
else
{
int a,b;
a = GetMaxDepth(root->left,depth+1);
b = GetMaxDepth(root->right,depth +1);
return (a>b)?a:b;
}
}

void GoToXY(int x,int y)
{
COORD coord;
coord.X = x ;coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

void Print(ptr root,int d,int c,int swing)
{
if (root)
{
if (root->left)
{
GoToXY(swing + pow(2,d-c-1),(c+1)*2);
cout<<'|';

GoToXY(swing + pow(2,d-c-1),(c+1)*2-1);
for(int i = pow(2,d-c-1);i< pow(2,d-c);i++)
cout<<'_';
}
if (root->right)
{
GoToXY(swing + pow(2,d-c-1) + pow(2,d-c),(c+1)*2);
cout<<'|';

GoToXY(swing + pow(2,d-c-1) + pow(2,d-c),(c+1)*2-1);
for(int i = pow(2,d-c-1);i<= pow(2,d-c);i++)
{
cout<<'_';GoToXY(swing + pow(2,d-c-1) + pow(2,d-c)-(i-pow(2,d-c-1)),(c+1)*2-1);
}
}
GoToXY(swing + pow(2,d-c),(c+1)*2 -1);
cout<<root->data;
Print(root->left,d,c+1,swing);
Print(root->right,d,c+1,swing + pow(2,d-c));
}
}

ptr RotateLeft(ptr x)
{
ptr temp = x->right;
x->right = x->right->left;
temp->left = x;
return temp;
}

ptr RotateRight(ptr x)
{
ptr temp = x->left;
x->left = x->left->right;
temp->right = x;
return temp;
}

int BalanceFactor(ptr x)
{
return GetMaxDepth(x->left,1) - GetMaxDepth(x->right,1);
}
ptr RebalanceTree(ptr x)
{
if (x->height == 2)
{
if (x->left->height ==1)
return RotateRight(x);
else
{
x->left = RotateLeft(x->left);
return RotateRight(x);
}
}
else
{
if (x->right->height ==-1)
return RotateLeft(x);
else
{
x->right = RotateRight(x->right);
return RotateLeft(x);
}
}
}

template<class T>
void tempStore(T* R)
{
Print(R,GetMaxDepth(R,1),0,0);
GoToXY(0,GetMaxDepth(R,1)+1);
}

//L

//Jangan cumacopy-paste doang!!pelajari!!

//Project ini dibuat semata-mata untuk pembelajaran

No comments:

Post a Comment

Your Comment is Our Order, Your Majesty