468x60 Ads

free counters

binary tree

#include
#include
#include
#include
#include
#define MAXQUEUE 100

struct node
{
int data;
struct node *leftPtr;
struct node *rightPtr;
};
typedef node *NodeTree;
struct Queue
{
NodeTree data[MAXQUEUE];
int front;
int rear;
};
struct node *root;
int count=0;
int arrcount=0;
void initTree(int i);
void addTree(node *t,int i);
void getNo(node *t);
void viewTree(node *t,int x,int y);
void freeTree(node *t);
void insertNode(NodeTree *,int);
void breadth(NodeTree *,Queue *,int *);
void depth(NodeTree *,Queue *,int *);
void insertQueue(Queue *,NodeTree);
void insertQueue1(Queue *,NodeTree);
NodeTree * removeQueue(Queue *);

main()
{
int a,element,val;
NodeTree rootPtr=NULL;
start:
system("Cls");
printf("**********Binary Search Tree**********\n\n");
printf("\n Make your selection.");
printf("\n 1.Build a tree by inserting nodes.");
printf("\n 2.Search by Breadth First Search.");
printf("\n 3.Search by Depth First Search.");
printf("\n 4.View the tree.");
printf("\n 5.Chop down the tree.");
printf("\n 6.Exit.");
printf("\n\n**************************************");
cout<<"\nYour Choice?: ";
cin>>a;

if(a==1)
{
printf("Enter a node: ");
scanf("%d",&element);
insertNode(&rootPtr,element);
arrcount=0;
getNo(root);
if(arrcount==0){
initTree(element);
}
else {
addTree(root,element);
}
goto start;
}

else if(a==2)
{
Queue qPtr;
qPtr.front=0;
qPtr.rear=-1;
printf("Enter a value to search using Breadth First Search method: \n");
scanf("%d",&val);
if (val==0){
printf("Please enter a value more than zero.");
}
else {
breadth(&rootPtr,&qPtr,&val);
printf("\n");
}
getch();
goto start;
}

else if(a==3)
{
Queue qPtr;
qPtr.front=0;
qPtr.rear=-1;
printf("Enter a value to search using Depth First Search method: \n");
scanf("%d",&val);
if (val==0){
printf("Please enter a value more than zero.");
}
else {
depth(&rootPtr,&qPtr,&val);
printf("\n");
}
getch();
goto start;
}

else if(a==4)
{
system("Cls");
arrcount=0;
getNo(root);
if(arrcount==0){
printf("\nEmpty tree");
}
else{
viewTree(root,38,1);
}
getch();
goto start;
}

else if(a==5)
{
freeTree(root);root=NULL;
printf("\nThe Binary Tree has been chopped down.\n");
getch();
goto start;
}

else if(a==6)
{
printf("\n************** GoodBye! **************\n");
printf("");
exit(0);
}

else
{
printf("\nInvalid choice! Try again: ");
getch();
goto start;
}
return 0;
}

void initTree(int i)
{
root=(struct node *)malloc(sizeof(struct node));
root->data=i;
root->leftPtr=NULL;
root->rightPtr=NULL;
count++;
}
void addTree(struct node *t,int i)
{
struct node *p;
if((i>t->data)&&(t->rightPtr==NULL))
{
p=(struct node *)malloc(sizeof(struct node));
p->data=i;p->leftPtr=NULL;p->rightPtr=NULL;
t->rightPtr=p;
return;
}
if((idata)&&(t->leftPtr==NULL))
{
p=(struct node *)malloc(sizeof(struct node));
p->data=i;p->leftPtr=NULL;p->rightPtr=NULL;
t->leftPtr=p;
return;
}
if(i>t->data)addTree(t->rightPtr,i);
else if(idata)addTree(t->leftPtr,i);
}
void getNo(struct node *t)
{
if(t==NULL)return;
getNo(t->leftPtr);
arrcount++;
getNo(t->rightPtr);
}
void freeTree(struct node *t)
{
if(t!=NULL){
freeTree(t->leftPtr);
freeTree(t->rightPtr);
free(t);
}
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void viewTree(struct node *t,int x,int y)
{
if(t==NULL)
return;
else
{
gotoxy(x,y);
printf("%d",t->data);
viewTree(t->leftPtr,x-8,y+2);
viewTree(t->rightPtr,x+8,y+2);

}
}
void insertNode(NodeTree *treePtr,int element)
{
if(*treePtr==NULL)
{
*treePtr=NodeTree(malloc(sizeof(node)));

if(*treePtr!=NULL)
{
(*treePtr)->data=element;
(*treePtr)->leftPtr=NULL;
(*treePtr)->rightPtr=NULL;
}
else
printf("Insertion failed due to insufficient memory.\n");
}
else
{
if(element<(*treePtr)->data)
insertNode(&(*treePtr)->leftPtr,element);
else if(element>(*treePtr)->data)
insertNode(&(*treePtr)->rightPtr,element);
else
printf("Duplicate number found.\n");
}
}
void breadth(NodeTree *treePtr,Queue *qPtr, int *val)
{
if (*val==(*treePtr)->data){
printf("Found %d.",(*treePtr)->data);
*val=0;
return;
}
if((*treePtr)->leftPtr!=NULL)
insertQueue(qPtr,(*treePtr)->leftPtr);
if((*treePtr)->rightPtr!=NULL)
insertQueue(qPtr,(*treePtr)->rightPtr);
if(qPtr->rear>=qPtr->front)
breadth(removeQueue(qPtr),qPtr,&*val);
else {
printf("Data not found.");
}
}
void depth(NodeTree *treePtr,Queue *qPtr,int *val)
{
if (*val==(*treePtr)->data){
printf("Found %d.",(*treePtr)->data);
*val=0;
return;
}
if((*treePtr)->leftPtr!=NULL)
insertQueue(qPtr,(*treePtr)->leftPtr);
if((*treePtr)->rightPtr!=NULL)
insertQueue(qPtr,(*treePtr)->rightPtr);
if(qPtr->rear>=qPtr->front)
breadth(removeQueue(qPtr),qPtr,&*val);
else {
printf("Data not found.");
}
}
void insertQueue(Queue *qPtr,NodeTree tp)
{
qPtr->data[++qPtr->rear]=tp;
}
NodeTree * removeQueue(Queue *qPtr)
{
return &qPtr->data[qPtr->front++];
}

0 komentar:

Posting Komentar

 
Impossible is Nothing © 2011 Theme made with the special support of Maiahost for their cheap WordPress hosting services and free support.