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()???
20 2360
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.
i fixed it same errors:( i am stuck 3 days now
This code compiles aand links but I didn't run it: -
struct treenode
-
{
-
int data;
-
struct treenode *left;
-
struct treenode *right;
-
};typedef struct treenode *PTR;
-
-
class tree
-
{
-
private:
-
///PTR tree;
-
PTR theData;
-
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"
-
-
int main()
-
{
-
int x,n,i=0;
-
int choise;
-
PTR bt;
-
bt=NULL;
-
tree theTree;
-
-
while(x!=0)
-
{
-
theTree.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");
-
theTree.preorder_traversal(bt);
-
break;
-
case 2:
-
printf("postorder\n");
-
theTree.postorder_traversal(bt);
-
break;
-
case 3:
-
printf("inorder\n");
-
theTree.inorder_traversal(bt);
-
break;
-
case 4:
-
printf("GIve Number");
-
scanf("%d",&n);
-
theTree.find_node(bt,n,i);
-
break;
-
-
} while (choise!=5);
-
getchar();
-
-
-
}
-
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.
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
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?????
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.
weaknes any idea how to start with them?????
is it mater of memory???
memalloc etc.????????
weaknes any idea how to start with them?????
is it mater of memory???
memalloc etc.????????
I'm not sure I understand your question.
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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
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. -
treenode::~treenode()
-
{
-
if (left)
-
{
-
delete left;
-
left = 0;
-
}
-
if (right)
-
{
-
delete right;
-
right = 0;
-
}
-
Then in your tree class: -
void tree::insert_node(PTR *pt,int x)
-
{
-
delete t; //deletes entire tree.
-
}
-
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
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.
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: - struct treenode
-
{
-
int data;
-
struct treenode *left;
-
struct treenode *right;
-
};typedef struct treenode *PTR;
-
-
class tree
-
{
-
private:
-
//PTR treeT;
-
PTR theData;
-
public:
-
tree();
-
tree(PTR t);
-
~tree();
-
void insert_node(PTR *pt,int x);
-
void destroy_tree(PTR *pt);
-
int countNodes(PTR t);
-
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 tree.cpp:
-
-
-
tree::~tree()
-
{
-
if (theData->left)
-
{
-
delete theData->left;
-
}
-
if (theData->right)
-
{
-
delete theData->right;
-
}
-
}
-
-
tree::tree()
-
{
-
-
}
-
-
tree::tree(PTR t)
-
{
-
theData->data=t->data;
-
theData->left=t->left;
-
theData->right=t->right;
-
}
-
-
-
-
-
and the errors are:
-
[ILINK32 Error] Error: Unresolved external 'tree::tree()' referenced from C:\DOCUMENTS AND SETTINGS\DEMONFOX\VERSION 0.0.1.2.0\DEBUG\FORMTREETEST.OBJ
-
[ILINK32 Error] Error: Unresolved external 'tree::~tree()' referenced from C:\DOCUMENTS AND SETTINGS\DEMONFOX\VERSION 0.0.1.2.0\DEBUG\FORMTREETEST.OBJ
-
-
-
Please what do i wrong?????
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.
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?
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.
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????
Are you using Windows?
If so there is a Tree control in MFC called CTreeCtrl. All the graphical work is done.
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????
do someone Know about paintbox?????
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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.
|
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...
|
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
|
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;
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |