473,413 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Need help on basic binary tree programming

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:

Expand|Select|Wrap|Line Numbers
  1. struct treenode
  2.         {
  3.             int data;
  4.             struct treenode *left;
  5.             struct treenode *right;
  6.         };typedef struct treenode *PTR;
  7.  
  8. class tree
  9. {
  10.     private:
  11.          PTR tree;
  12.         public:
  13.             void insert_node(PTR *pt,int x);
  14.             void preorder_traversal(PTR t);
  15.             void inorder_traversal(PTR t);
  16.             void postorder_traversal(PTR t);
  17.             void find_node(PTR t,int x,int i);
  18. };
  19.  

on my tree.cpp file:


Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include"tree.h"
  6.  
  7. void tree::insert_node(PTR *pt,int x)
  8. {
  9.     PTR t;
  10.     t=*pt;
  11.  
  12.     if (t==NULL)
  13.     {
  14.         t=(PTR)malloc(sizeof(struct treenode));
  15.         t->data=x;
  16.         t->left=NULL;
  17.         t->right=NULL;
  18.     }
  19.     else
  20.         if (x<t->data)
  21.         insert_node(&(t->left),x);
  22.     else
  23.        insert_node(&(t->right),x);
  24.         *pt=t;
  25. }
  26.  
  27. void tree::preorder_traversal(PTR t)
  28. {
  29.     if(t!=NULL)
  30.     {
  31.         printf("%d",t->data);
  32.         preorder_traversal(t->left);
  33.         preorder_traversal(t->right);
  34.     }
  35.  }
  36.  
  37.  
  38. void tree::postorder_traversal(PTR t)
  39. {
  40.     if (t!=NULL)
  41.     {
  42.         postorder_traversal(t->left);
  43.         postorder_traversal(t->right);
  44.         printf("%d",t->data);
  45.     }
  46.  
  47. }
  48.  
  49. void tree::inorder_traversal(PTR t)
  50. {
  51.     if (t!=NULL)
  52.     {
  53.         inorder_traversal(t->left);
  54.         printf("%d",t->data);
  55.         inorder_traversal(t->right);
  56.     }
  57.  
  58. }
  59.  
  60. void tree::find_node(PTR t,int x,int i)
  61. {
  62.     i++;
  63.     if (t==NULL)
  64.     {
  65.         printf("not found");
  66.         printf("Made %d Try",i);
  67.     }
  68.     else if (t->data==x)
  69.     {
  70.         printf("Found");
  71.         printf("Made %d try",i);
  72.      }
  73.      else if (x<t->data)
  74.         find_node(t->left,x,i);
  75.      else
  76.         find_node(t->right,x,i);
  77. }
  78.  
on a test.cpp file

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include"tree.h"
  6.  
  7. main()
  8. {
  9.  int x,n,i=0;
  10.  int choise;
  11.  PTR bt;
  12.  bt=NULL;
  13.  
  14.  while(x!=0)
  15.   {
  16.    insert_node(&bt,x);
  17.    printf("Give Number");
  18.    scanf("%d",&x);
  19.   }
  20.  
  21.   printf("1.preorder\n");
  22.      printf("2.postorder\n");
  23.      printf("3.inorder\n");
  24.      printf("4.findnode\n");
  25.      printf("2.exit\n");
  26.      printf("choise?");
  27.      scanf("%d",&choise);
  28.  
  29.      switch (choise)
  30.      {
  31.       case 1:
  32.        printf("preorder\n");
  33.        preorder_traversal(bt);
  34.        break;
  35.       case 2:
  36.        printf("postorder\n");
  37.        postorder_traversal(bt);
  38.        break;
  39.       case 3:
  40.        printf("inorder\n");
  41.        inorder_traversal(bt);
  42.        break;
  43.       case 4:
  44.        printf("GIve Number");
  45.        scanf("%d",&n);
  46.        find_node(bt,n,i);
  47.        break;
  48.  
  49.    } while (choise!=5);
  50.      getch();
  51.  
  52.  
  53.  }



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()???
Oct 15 '07 #1
20 2360
weaknessforcats
9,208 Expert Mod 8TB
Trouble starts right here:
class tree
{
private:
PTR tree;
etc...
The private member tree has the same name as your class. Big no-no. The compiler thinks you have delcared a constructor with a return of PTR.

Fix this first.
Oct 15 '07 #2
i fixed it same errors:( i am stuck 3 days now
Oct 15 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
This code compiles aand links but I didn't run it:
Expand|Select|Wrap|Line Numbers
  1. struct treenode
  2. {
  3. int data;
  4. struct treenode *left;
  5. struct treenode *right;
  6. };typedef struct treenode *PTR;
  7.  
  8. class tree
  9. {
  10. private:
  11. ///PTR tree;
  12.     PTR theData;
  13. public:
  14. void insert_node(PTR *pt,int x);
  15. void preorder_traversal(PTR t);
  16. void inorder_traversal(PTR t);
  17. void postorder_traversal(PTR t);
  18. void find_node(PTR t,int x,int i);
  19. };
  20.  
  21.  
  22.  
  23. //on my tree.cpp file:
  24.  
  25.  
  26. #include<stdio.h>
  27. #include<conio.h>
  28. #include<stdlib.h>
  29. #include<string.h>
  30. //#include"tree.h"
  31.  
  32. void tree::insert_node(PTR *pt,int x)
  33. {
  34. PTR t;
  35. t=*pt;
  36.  
  37. if (t==NULL)
  38. {
  39. t=(PTR)malloc(sizeof(struct treenode));
  40. t->data=x;
  41. t->left=NULL;
  42. t->right=NULL;
  43. }
  44. else
  45. if (x<t->data)
  46. insert_node(&(t->left),x);
  47. else
  48. insert_node(&(t->right),x);
  49. *pt=t;
  50. }
  51.  
  52. void tree::preorder_traversal(PTR t)
  53. {
  54. if(t!=NULL)
  55. {
  56. printf("%d",t->data);
  57. preorder_traversal(t->left);
  58. preorder_traversal(t->right);
  59. }
  60. }
  61.  
  62.  
  63. void tree::postorder_traversal(PTR t)
  64. {
  65. if (t!=NULL)
  66. {
  67. postorder_traversal(t->left);
  68. postorder_traversal(t->right);
  69. printf("%d",t->data);
  70. }
  71.  
  72. }
  73.  
  74. void tree::inorder_traversal(PTR t)
  75. {
  76. if (t!=NULL)
  77. {
  78. inorder_traversal(t->left);
  79. printf("%d",t->data);
  80. inorder_traversal(t->right);
  81. }
  82.  
  83. }
  84.  
  85. void tree::find_node(PTR t,int x,int i)
  86. {
  87. i++;
  88. if (t==NULL)
  89. {
  90. printf("not found");
  91. printf("Made %d Try",i);
  92. }
  93. else if (t->data==x)
  94. {
  95. printf("Found");
  96. printf("Made %d try",i);
  97. }
  98. else if (x<t->data)
  99. find_node(t->left,x,i);
  100. else
  101. find_node(t->right,x,i);
  102. }
  103.  
  104.  
  105. //on a test.cpp file
  106.  
  107. #include<stdio.h>
  108. #include<conio.h>
  109. #include<stdlib.h>
  110. #include<string.h>
  111. //#include"tree.h"
  112.  
  113. int main()
  114. {
  115. int x,n,i=0;
  116. int choise;
  117. PTR bt;
  118. bt=NULL;
  119. tree theTree;
  120.  
  121. while(x!=0)
  122. {
  123. theTree.insert_node(&bt,x);
  124. printf("Give Number");
  125. scanf("%d",&x);
  126. }
  127.  
  128. printf("1.preorder\n");
  129. printf("2.postorder\n");
  130. printf("3.inorder\n");
  131. printf("4.findnode\n");
  132. printf("2.exit\n");
  133. printf("choise?");
  134. scanf("%d",&choise);
  135.  
  136. switch (choise)
  137. {
  138. case 1:
  139. printf("preorder\n");
  140. theTree.preorder_traversal(bt);
  141. break;
  142. case 2:
  143. printf("postorder\n");
  144. theTree.postorder_traversal(bt);
  145. break;
  146. case 3:
  147. printf("inorder\n");
  148. theTree.inorder_traversal(bt);
  149. break;
  150. case 4:
  151. printf("GIve Number");
  152. scanf("%d",&n);
  153. theTree.find_node(bt,n,i);
  154. break;
  155.  
  156. } while (choise!=5);
  157. getchar();
  158.  
  159.  
  160. }
  161.  
I fixed:
1) PTR tree in the tree class is now PTR theData;
2) main() returns an int
3) getch() is deprecated. Use getchar()
4) you did not declare a tree object in main(). I created one anc changed the function calls in the cases.
Oct 15 '07 #4
To begin with thanks for trying to help me i really need it:)
The basically idea is i want the functions to work
to test if they work correct and then put them as code in optical form that s my exam :) is there a function like destroy_tree()????
now trying the code you have given
excuse my english :P
Oct 15 '07 #5
sorry compiled see now if it works thanks
edit
it works fine :P
your the man:)
but it only run once and stops
how can i destroy the tree and re enter??? do you know a way?????
Oct 15 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
but it only run once and stops
how can i destroy the tree and re enter??? do you know a way?????
Write your main differently.

Add a menu.

1) Create new Tree
2) Update current Tree
3) Exit

Write a loop and inside the loop get a choice from the user. Based on the choice call an appropriate function. You stay in the loop forever until the user selects Exit and then you clean up the current tree and break out of the loop.
Oct 16 '07 #7
weaknes any idea how to start with them?????
is it mater of memory???
memalloc etc.????????
Oct 18 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
weaknes any idea how to start with them?????
is it mater of memory???
memalloc etc.????????
I'm not sure I understand your question.
Oct 18 '07 #9
in the function
void insert_node(PTR *pt,int x);
we have
t=(PTR)malloc(sizeof(struct treenode));
if i want to clear the tree(destroy it to re-enter values)
do i have to use the
free
command??
i acn make the function destroy_tree()
and how do i run the proggramm (multiple inorder postorder functioning)
is the problem in switch?????
sorry for my english:(
you are big help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Oct 18 '07 #10
weaknessforcats
9,208 Expert Mod 8TB
You do not use malloc and free in C++.

Instead, you use new and delete.

Your tree contains many treenodes. When you delete a treenode, a call is made to the tree node destructor (which you have not written) that destructor checks to see if there are any left treenodes. I so, it deletes the left treenode. Which calls the treenode destructor on that treenode, etc...

When the delete of the left treennode returns, the treenode destructor checks to see of there are any right treenodes and if there are then it deletes the right treenode. Which calls the treenode destructor, and off you go again.

Expand|Select|Wrap|Line Numbers
  1. treenode::~treenode()
  2. {
  3.     if (left)
  4.     {
  5.         delete left;
  6.         left = 0;
  7.     }
  8.     if (right)
  9.     {
  10.        delete right;
  11.        right = 0;
  12. }
  13.  
Then in your tree class:
Expand|Select|Wrap|Line Numbers
  1. void tree::insert_node(PTR *pt,int x)
  2. {
  3.       delete t;   //deletes entire tree.
  4. }
  5.  
Oct 18 '07 #11
sorry but why goes the
delete t; goes in the
insert_node function???
shouldn t it be on the destroy_node function???




Edit: I got your point i have idded constructors and destructors :)
Now i have to figure out how to use them :P
Oct 18 '07 #12
weaknessforcats
9,208 Expert Mod 8TB
sorry but why goes the
delete t; goes in the
insert_node function???
shouldn t it be on the destroy_node function???
I just did that because you said earlier:
in the function
void insert_node(PTR *pt,int x);
we have
t=(PTR)malloc(sizeof(struct treenode));
if i want to clear the tree(destroy it to re-enter values)
do i have to use the
free
command??
Looked odd to me too.
Oct 19 '07 #13
Now i need help again
I made everything you told me and all in console works fine
exept the part deleting the tree
now i need help on the following.
i am going to put it in optical enviroment and it say error on the construtors
it is like this.
on tree.h:


Expand|Select|Wrap|Line Numbers
  1. struct treenode
  2.         {
  3.             int data;
  4.             struct treenode *left;
  5.             struct treenode *right;
  6.         };typedef struct treenode *PTR;
  7.  
  8. class tree
  9. {
  10.     private:
  11.          //PTR treeT;
  12.          PTR theData;
  13.         public:
  14.             tree();
  15.             tree(PTR t);
  16.             ~tree();
  17.             void insert_node(PTR *pt,int x);
  18.             void destroy_tree(PTR *pt);
  19.             int countNodes(PTR t);
  20.             void preorder_traversal(PTR t);
  21.             void inorder_traversal(PTR t);
  22.             void postorder_traversal(PTR t);
  23.             void find_node(PTR t,int x,int i);
  24. };
  25.  
  26.  
  27.  
  28. on tree.cpp:
  29.  
  30.  
  31. tree::~tree()
  32. {
  33.     if (theData->left)
  34.     {
  35.         delete theData->left;
  36.     }
  37.     if (theData->right)
  38.     {
  39.     delete theData->right;
  40.     }
  41. }
  42.  
  43. tree::tree()
  44. {
  45.  
  46. }
  47.  
  48. tree::tree(PTR t)
  49. {
  50.     theData->data=t->data;
  51.     theData->left=t->left;
  52.     theData->right=t->right;
  53. }
  54.  
  55.  
  56.  
  57.  
  58. and the errors are:
  59. [ILINK32 Error] Error: Unresolved external 'tree::tree()' referenced from C:\DOCUMENTS AND SETTINGS\DEMONFOX\VERSION 0.0.1.2.0\DEBUG\FORMTREETEST.OBJ
  60. [ILINK32 Error] Error: Unresolved external 'tree::~tree()' referenced from C:\DOCUMENTS AND SETTINGS\DEMONFOX\VERSION 0.0.1.2.0\DEBUG\FORMTREETEST.OBJ
  61.  
  62.  
  63. Please what do i wrong?????
Oct 31 '07 #14
weaknessforcats
9,208 Expert Mod 8TB
I assume the cpp file with main() does a #include of tree.h and that you have added tree.cpp to the project build.

The linker is telling you that it can't find the code for the tree:tree() constructor. Usually that means the tree.cpp file was not compiled or was somehow left out of the build.

The fact that tje linker can't find the tree destructor just comfirms that the tree.cpp file was left out of your build.
Oct 31 '07 #15
i found the caouse of problem it was the follow
int the tree.cpp i made the changes:
tree::tree(PTR t1)
{
PTR t;
t->data=t1->data;
t->left=t1->left;
t->right=t1->right;
}

and now it is ok
:P:P:P:P:P
but the next thing is:
void tree::preorder_traversal(PTR t)
{
if(t!=NULL)
{
printf("%d",t->data);
preorder_traversal(t->left);
preorder_traversal(t->right);
}
}



How can i take the results of the function into a Edit??????
in optical interface?
Oct 31 '07 #16
weaknessforcats
9,208 Expert Mod 8TB
How can i take the results of the function into a Edit??????
in optical interface?
I don't think I understand your requirement. Can you provide a little more info.
Oct 31 '07 #17
I have this
void tree::preorder_traversal(PTR t)
{
if(t!=NULL)
{
printf("%d",t->data);
preorder_traversal(t->left);
preorder_traversal(t->right);
}
}

i want the results of the traversal
to be shown on an edit box.
the final think i want is to (give)insert a node to be presented in icons in a paintbox
to be shown step by step the traversals. did this help????
Oct 31 '07 #18
weaknessforcats
9,208 Expert Mod 8TB
Are you using Windows?

If so there is a Tree control in MFC called CTreeCtrl. All the graphical work is done.
Oct 31 '07 #19
Are you using Windows?

If so there is a Tree control in MFC called CTreeCtrl. All the graphical work is done.


Iam using now CodeGear borland c++ 2007 is it working????
Nov 6 '07 #20
do someone Know about paintbox?????
Nov 14 '07 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am > cs students and are now facing difficult problems in understanding > what a binary tree is, how it works, and the algorithm...
11
by: jova | last post by:
Is there a difference between a Binary Tree and a Binary Search Tree? If so can someone please explain. Thank You.
15
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked...
8
by: sudharsan | last post by:
please gimme the logic to merge two binary search trees?I mean which node has to be the root node of the new binary tree?? Thanks in advance
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
0
by: rokuingh | last post by:
ok, so i've been working on this one for quite a while, and the code is very big so i'm just going to give the relevant parts. this is a program that builds polymers (chemical structures of repeated...
1
by: satan | last post by:
I need to write the definition of the method leavesCount that takes as a parameter a reference of the root node of a binary tree and returns the numbers of leaves in the binary tree. This what i get...
4
by: n.noumia | last post by:
- with 10 nodes, give one example of an unbalanced binary tree and one example of a balanced binary tree - what is the advantage of having a balanced binary tree over an unbalanced tree? - number...
7
by: Vinodh | last post by:
Started reading about Binary Trees and got the following questions in mind. Please help. Definition of a Binary Tree from "Data Structures using C and C++ by Tanenbaum" goes like this, "A...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.