Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

I'm creating a binary search tree and i have a problem in growing up my tree:
i cannot make relation between tree nodes.

here i type my code, and my problem


bstnode class here:
C++
class bstnode{

private:
	int key;
	bstnode *left;
	bstnode *right;

public:
	//bstnode
	bstnode()
		{key=-1; left=0; right=0;}
        int get_key()
		{return key;}
	//set key
	void set_key
		(int x){key=x;}
        //get right
	bstnode *get_right()
		{return(right);}
	//set right
	void set_right(bstnode *r)
		{right = r;}
};



main code:
C++
void main()
{
        bstnode(node);
	bstnode(*node2);
	bstnode(*node3);

        //set right of node
	node.set_right(node2);
	node2->set_right(node3); //------> here
}


when it reaches this line it says:
The variable 'node2' is being used without being initialized.

i thought maybe it's because 'node2' is a poiinter so i changed it to class substance it again couldn't get 'node3'as right.

so if pointer cannot get pointer as their child.
and if bstnode substance can only get pointer as child.

then how can i grow up my tree.
Posted

Lots of problems here. Your main code as presented here wouldn't even compile so I'm not too sure what you're doing. If you want the root node on the stack and the rest on the heap you could try something like:
void main()
{
    bstnode node;
    bstnode* node2 = new bstnode();
    bstnode* node3 = new bstnode();

    node.set_right( node2 );
    node2->set_right( node3 );
}


However that's about the worst piece of code I've written in 20 years. I leaks because the new uses have no matching delete and that really isn't the way you'd ever build up binary tree except in sample code.
Unfortunately I can't help much further because I don't know what the real purpose is here or where the node data is supposed to be coming from.
 
Share this answer
 
Comments
m.r.m.40 6-Jun-13 5:04am    
about declaration and using them i did like you said.

actually this was the problem the node data and also other function i could build them up myself so i didn't see any necessity to put them here.

now the tree is built and its working properly.
C++
    bstnode(node);
bstnode(*node2);
bstnode(*node3);

Same problem as in your last question. These are not definitions of nodes and your compiler tells you that they are syntactically wring. Use instead:
C++
bstnode node;
bstnode node2;
bstnode node3;

Then again the same problem as in your last post: Your set_right function takes a pointer as parameter, not the node itself, so use:
C++
node.set_right (&node2);
node2.set_right (&node3);


I suggest you stop experimenting around this way. It will be very time consuming to learn the basics of C++ by playing around. Get a good book on C++ or take one of the many online tutorials and try a step-by-step approach. That will save you a lot of time.
 
Share this answer
 
v2
Comments
m.r.m.40 6-Jun-13 5:10am    
nv3, thanks for your suggestion.
actually i solved it.
and you are right 100 percent.
I'm using deitel book. ( if you know any better resource please tell )
but to be honest its faster for me to do this way as you may see now from yesterday up to now i could implement the binary search tree.
nv3 6-Jun-13 5:30am    
As far as I can tell, you still have problems with the following concepts:
- what is a pointer, an address, a reference
- what is a constructor and when is it called
- how to define a class object on the stack
- how to allocate a class object on the heap
- how to call a member function of a class object having the object itself or having a pointer to it
As long as you don't have those basics as clear as sunshine you won't be getting anywhere.
i defined the class this way:

C#
class bstnode{
private:
       struct tree_node
       {
          tree_node* left;
          tree_node* right;
          int key;
       };
};


and i declared other pointers this way:

C++
tree_node* t = new tree_node;


then i used them this way:

C++
t->right = s;
 
Share this answer
 
Comments
nv3 6-Jun-13 5:00am    
It's a bad idea to post additions to your question as a solution. That will upset people and as the question is marked as "solved" most readers will not further look into it. So you lower your chances that somebody will answer your question.
m.r.m.40 6-Jun-13 5:34am    
thats a word.
i'll do that.
Richard MacCutchan 6-Jun-13 5:03am    
Why have you created a struct inside your class; it's totally redundant. I suggest you go back to your reference books, or this MSDN section, and study the concept of classes and how to use them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900