i have started my midterm exersize than is on binary treescan anyone help me on the basics
i have started and i have made the following
on my tree.h file:
struct treenode
{
int data;
struct treenode *left;
struct treenode *right;
};typedef struct treenode *PTR;
class tree
{
private:
PTR tree;
public:
void insert_node(PTR *pt,int x);
void preorder_traversal(PTR t);
void inorder_traversal(PTR t);
void postorder_traversal(PTR t);
void find_node(PTR t,int x,int i);
};
on my tree.cpp file:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include"tree.h"
void tree::insert_node(PTR *pt,int x)
{
PTR t;
t=*pt;
if (t==NULL)
{
t=(PTR)malloc(sizeof(struct treenode));
t->data=x;
t->left=NULL;
t->right=NULL;
}
else
if (x<t->data)
insert_node(&(t->left),x);
else
insert_node(&(t->right),x);
*pt=t;
}
void tree::preorder_traversal(PTR t)
{
if(t!=NULL)
{
printf("%d",t->data);
preorder_traversal(t->left);
preorder_traversal(t->right);
}
}
void tree::postorder_traversal(PTR t)
{
if (t!=NULL)
{
postorder_traversal(t->left);
postorder_traversal(t->right);
printf("%d",t->data);
}
}
void tree::inorder_traversal(PTR t)
{
if (t!=NULL)
{
inorder_traversal(t->left);
printf("%d",t->data);
inorder_traversal(t->right);
}
}
void tree::find_node(PTR t,int x,int i)
{
i++;
if (t==NULL)
{
printf("not found");
printf("Made %d Try",i);
}
else if (t->data==x)
{
printf("Found");
printf("Made %d try",i);
}
else if (x<t->data)
find_node(t->left,x,i);
else
find_node(t->right,x,i);
}
on a test.cpp file
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include"tree.h"
main()
{
int x,n,i=0;
int choise;
PTR bt;
bt=NULL;
while(x!=0)
{
insert_node(&bt,x);
printf("Give Number");
scanf("%d",&x);
}
printf("1.preorder\n");
printf("2.postorder\n");
printf("3.inorder\n");
printf("4.findnode\n");
printf("2.exit\n");
printf("choise?");
scanf("%d",&choise);
switch (choise)
{
case 1:
printf("preorder\n");
preorder_traversal(bt);
break;
case 2:
printf("postorder\n");
postorder_traversal(bt);
break;
case 3:
printf("inorder\n");
inorder_traversal(bt);
break;
case 4:
printf("GIve Number");
scanf("%d",&n);
find_node(bt,n,i);
break;
} while (choise!=5);
getch();
}
it gives me the error :
[BCC32 Error] test.cpp(16): E2268 Call to undefined function 'insert_node'
[BCC32 Error] test.cpp(33): E2268 Call to undefined function 'preorder_traversal'
[BCC32 Error] test.cpp(37): E2268 Call to undefined function 'postorder_traversal'
[BCC32 Error] test.cpp(41): E2268 Call to undefined function 'inorder_traversal'
[BCC32 Error] test.cpp(46): E2268 Call to undefined function 'find_node'
[BCC32 Warning] test.cpp(49): W8019 Code has no effect
Do i forget something????????
are there more()??
like void destroynode()???