/**********************************************************************
APPLICATION : Program to make a tree (Given Preorder and Inorder)
CODED BY : Ankit Pokhrel
COMPILED ON : Borland Turbo C++ Ver 3.0
DATE : 2010 - July - 23
***********************************************************************/
#include "iostream.h"
#include "conio.h"
struct TREE //Structure to represent a node of a tree
{
int data;
struct TREE *left,*right;
};
typedef TREE tree;
tree *New,*trav,*root = NULL;
int pre[25],in[25],index;
void create(int Item) //Function to create a node
{
New = new tree;
New -> left = NULL;
New -> right = NULL;
New -> data = Item;
if(root == NULL) //if First Node
{
root = New; //Set root
trav = root;
}
}
int errorCheck(int lenPre,int lenIn)
{
if(lenPre != lenIn) //If length of Preorder input and Inorder input do not match
return 1; //return error
int valid;
for(int j = 0;j < lenPre;j++) //for every preorder data
{
valid = false; //assume that data is not in Inorder
for(int k = 0;k < lenIn;k++)
if(in[k] == pre[j]) //find in Inorder, If found
{
valid = true; //set valid = true
break; //exit from loop
}
if(!valid)
return 1; //return error
}
return 0; //return valid
}
int Find_Index(int len,int Item) //Function to find Index
{
for(int i = 0;i < len;i++)
if(in[i] == Item)
return i;
}
void construct(int Item,int len)
{
int ind;
ind = Find_Index(len,trav -> data);
if(index < ind)
{
if(trav -> left == NULL)
{
create(Item); //allocate Memory
trav -> left = New;
}
else
{
trav = trav -> left; //go to left
construct(Item,len); //Insert Item
}
}
else
{
if(trav -> right == NULL)
{
create(Item); //allocate Memory
trav -> right = New; //Insert Item
}
else
{
trav = trav -> right; //go to right
construct(Item,len);
}
}
}
void preorder(tree *t)
{
if(t != NULL)
{
cout << t -> data << ' ';
preorder(t -> left);
preorder(t -> right);
}
}
void inorder(tree *t)
{
if(t != NULL)
{
inorder(t -> left);
cout << t -> data << ' ';
inorder(t -> right);
}
}
void postorder(tree *t)
{
if(t != NULL)
{
postorder(t -> left);
postorder(t -> right);
cout << t -> data << ' ';
}
}
int main()
{
int lenPre,lenIn,i = 0;
//Scan Preorder and Inorder Datas
cout << "Enter Preorder and Inorder Data Correctly,\n";
cout << "Input -999 as last Data,\n\n";
cout << "Preorder : ";
do
{
cin >> pre[i];
i++;
}while(pre[i-1] != -999);
lenPre = i - 1;
cout << "\nInorder : ";
i = 0;
do
{
cin >> in[i];
i++;
}while(in[i-1] != -999);
lenIn = i - 1;
int error = errorCheck(lenPre,lenIn); //Check for Errors
if(error)
{
cout << "\nThe Given Expressions are not Valid.";
getch();
return 0; //exit from program
}
create(pre[0]);
i = 1;
while(i < lenPre)
{
index = Find_Index(lenPre,pre[i]); //find index
trav = root; //Start from root
construct(pre[i],lenPre); //Insert data in right place
i++;
}
cout << endl << endl << "Inorder : "; //Print tree in Inorder
inorder(root);
cout << endl << endl << "Preorder : "; //Print tree in Preorder
preorder(root);
cout << endl << endl << "Postorder : "; //print tree in Postorder
postorder(root);
getch();
return 0;
}
......................
Labels:
Tree
No comments:
Post a Comment