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()???

Those errors have more to do with not knowing how to use a class than not understanding trees. Because those functions are member functions, you need an object of the tree class before you can call them.

yeah stupid of me
because the name of PTR was the same as my class
:(
i made it run but it runs only once
can you help me do you know how to destry the tree and re enter choises
something like desrtoy_tree()?????

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include"tree.h"

int main()
{
  int x=0,n=0,i=0;
  int choise;
  PTR bt;
  bt=NULL;
  tree* ncAgora=new tree;


  while(x!=9)
  {
    ncAgora->insert_node(&bt,x);
    printf("Give Number");
    scanf_s("%d",&x);
  }
  do{
    printf("1.preorder\n");
    printf("2.postorder\n");
    printf("3.inorder\n");
    printf("4.findnode\n");
    printf("2.exit\n");
    printf("choice?");
    scanf_s("%d",&choise);

    switch (choise)
    {
      case 1:
        printf("preorder\n");
        ncAgora->preorder_traversal(bt);
        break;
      case 2:
        printf("postorder\n");
        ncAgora->postorder_traversal(bt);
        break;
      case 3:
        printf("inorder\n");
        ncAgora->inorder_traversal(bt);
        break;
      case 4:
        printf("Give Number");
        scanf_s("%d",&n);
        ncAgora->find_node(bt,n,i);
        break;
    } 
  }while (choise!=5);
  _getch();
}

i have it working now
but the prob now is it runs only one time then stops
is there a function like destroy_tree() to reeenter the the tree?????

Note the do{ statement before the 1st printf, and the while before the _getch(). The switch is now inside the main loop.

This article has been dead for over six months. Start a new discussion instead.