C++ 二叉树不';不接受新节点 intmain(){ 节点*root、*temp、*temp\u之前; root=新节点(0,NULL,NULL,NULL); 前温度=根; temp=前温度->左; temp=新节点(5,NULL,NULL,NULL); 温度=根->左; coutleft它不起作用,我运行它时程序停止了。我不知道,也许我做了一件非常奇怪的事情,期望它返回一些东西,但问题出在哪里?

C++ 二叉树不';不接受新节点 intmain(){ 节点*root、*temp、*temp\u之前; root=新节点(0,NULL,NULL,NULL); 前温度=根; temp=前温度->左; temp=新节点(5,NULL,NULL,NULL); 温度=根->左; coutleft它不起作用,我运行它时程序停止了。我不知道,也许我做了一件非常奇怪的事情,期望它返回一些东西,但问题出在哪里?,c++,pointers,tree,binary-tree,C++,Pointers,Tree,Binary Tree,有几件事。你需要检查你的节点是否等于null。你还应该更新你正在输出的内容 int main(){ Node *root , *temp , *temp_before; root = new Node(0 , NULL , NULL , NULL); temp_before = root; temp = temp_before->left; temp = new Node(5 , NULL , NULL , NULL); temp = root->left; cout<<

有几件事。你需要检查你的节点是否等于null。你还应该更新你正在输出的内容

int main(){
Node *root , *temp , *temp_before;
root = new Node(0 , NULL , NULL , NULL);
temp_before = root;
temp = temp_before->left;
temp = new Node(5 , NULL , NULL , NULL);
temp = root->left;
cout<<temp->val;
}
node=root;
while(节点!=null)
{
向左数;
}

让我们逐行查看代码:

node = root;
while (node != null) 
{
    count << node;
    node = node->left;

}
当您使用指针分配内存时,指针只指向该内存块。除非您手动将它们连接在一起,否则它们不会成为连接的结构

这正是您试图做的,但方式不对。请这样想:当您分配内存时,操作系统会给您一个来自主内存的空间。它通过给您一个对该内存块的“引用”来实现,这样您就可以随意使用该内存块。这就是指针也被称为“句柄”的原因因为它们是你与内存块交互的方式

在Erroous行中,您将句柄重写为刚才分配的内存。当您这样做时,您将失去对该内存块的访问,直到程序执行结束。这些重写称为“内存泄漏”,因为该内存块是您请求但忘记的

当您执行
temp=root->left;
时,它会用另一个指针覆盖您的指针(在本例中,指向
NULL
),当您尝试打印它时,它会给您一个名为
NULL pointer exception
的错误,从名称中您可以清楚地看到问题:)

当你试图做的事情过于复杂时,这些错误就会发生 通常会发生这种情况,尤其是当您对内存缺乏经验时。简化此代码的方法是:

struct Node {
    int val;
    Node* left;
    Node* right;
    Node* parent;
    Node(int value, Node* left, Node* right, Node* parent): val(value), left(left), right(right), parent(parent){}
};

void main() {
    Node *root, *temp, *temp_before;
    root = new Node(0, NULL, NULL, NULL); // root points to the newly allocated memory
    temp_before = root; //temp_before also points to that memory
    temp = temp_before->left; // temp points to the left of root, which is null
    // NOTE: at this point, temp isn't connected to the tree, even if you allocate memory with it.
    // You only have pointers ("handles") for those memory blocks but you haven't combined them together yet.
    temp = new Node(5, NULL, NULL, NULL); // This allocated memory for the memory address pointed by temp
    // temp = root->left; //error
    root->left = temp; //correct one
    // Now root->left points to the memory block you allocated with temp
}
此代码与
main
函数中的代码相同。请这样想:您正在分配一个内存块,并使用句柄
temp
访问它。然后,您将该句柄的信息分配给根的左节点指针。这样,您甚至可以通过
root->left
访问同一内存块如果您再也无法访问
temp

下面的代码是您如何思考的。试着思考这两者之间的区别,一旦您弄明白了,指针将更有意义:

Node* temp = new Node(5, NULL, NULL, NULL);
root->left = temp;

在树中的何处设置节点以指向
temp
?未初始化的局部非静态变量(如您拥有的)在初始化之前,具有不确定的值。使用这些未初始化的变量将导致未定义的行为。指向将root->left设置为除null@pm100temp\u before=root;temp=temp\u before->left temp=new节点(5,null,null,null);我认为这为root->left声明了一个值,我错了吗?temp=new node处也有一个memoryleak,在它的正下方,你说temp=root->left node->left等于null,我应该如何将node传输到node->left?首先将node设置为head,然后遍历。我更新了我的答案@TahaSümerI know,我不应该写“谢谢”在stack overflow的评论中,但真的非常感谢这个意义深远的答案!这完全是我正在搜索的内容。同时,我在指针方面遇到了问题,这也帮助了我,非常感谢你花时间给出了答案。
Node* temp = new Node(5, NULL, NULL, NULL);
root->left = temp;
Node* temp = root->left;
temp = new Node(5, NULL, NULL, NULL);