class substance - 指针 - 二叉搜索树 [英] class substance - pointers - binary search tree

查看:113
本文介绍了class substance - 指针 - 二叉搜索树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在创建一个二叉搜索树,我在成长树时遇到问题:

i无法生成树节点之间的关系。



这里我输入我的代码,我的问题





bstnode class here:

  class  bstnode {

< span class =code-keyword> private :
int key;
bstnode * left;
bstnode * right;

public
// bstnode
bstnode()
{key = - 1 ; left = 0 ; right = 0 ;}
int get_key()
{返回键;}
// 设置密钥
void set_key
int x){key = x;}
// 正确
bstnode * get_right()
{ return (右);}
// 设置正确
void set_right(bstnode * r)
{right = r;}
};







主要代码:

  void  main()
{
bstnode(node);
bstnode(* node2);
bstnode(* node3);

// 设置节点权限
node.set_right(节点2);
node2-> set_right(node3); // ------>这里
}





到达此行时它会说:

变量'node2'正在被使用而没有被初始化。



i也许是因为'node2'是一个poiinter所以我把它改成了类物质它再次无法获得'node3'是正确的。



所以如果指针不能将指针作为他们的孩子。

如果bstnode物质只能得到指针作为孩子。



然后我怎么能长大我的树。

解决方案

这里有很多问题。你在这里提出的主要代码甚至不会编译,所以我不太清楚你在做什么。如果你想要堆栈上的根节点和堆上的其余节点你可以尝试类似的东西:

 void main()
{
bstnode node ;
bstnode * node2 = new bstnode();
bstnode * node3 = new bstnode();

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





然而,这是我20年来编写的最糟糕的代码。我泄漏是因为 new 使用没有匹配的 delete ,这真的不是你建立的方式二进制树除了示例代码。

不幸的是,我无法进一步帮助,因为我不知道这里的真正目的是什么,或者节点数据应该来自哪里。


 bstnode(node); 
bstnode(* node2);
bstnode(* node3);



与上一个问题相同的问题。这些不是节点的定义,您的编译器会告诉您它们在语法上是拧干的。改为使用:

 bstnode node; 
bstnode node2;
bstnode node3;



然后再次出现与上一篇文章相同的问题:你的set_right函数将指针作为参数,而不是节点本身,所以使用:

 node.set_right(& node2); 
node2.set_right(& node3);





我建议你不要再这样试验了。通过游戏来学习C ++的基础知识将是非常耗时的。获得一本关于C ++的好书,或者参考许多在线教程中的一个并尝试逐步的方法。这将节省你很多时间。


我用这种方式定义了这个类:



  class  bstnode {
private
struct tree_node
{
tree_node * left;
tree_node * right;
int 键;
};
};





我以这种方式宣布其他指示:



 tree_node * t =  new  tree_node; 





然后我用这种方式:



 t-> right = s; 


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:

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:

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.

解决方案

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.


    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:

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:

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.


i defined the class this way:

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



and i declared other pointers this way:

tree_node* t = new tree_node;



then i used them this way:

t->right = s;


这篇关于class substance - 指针 - 二叉搜索树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆