C++; 超级哑问题,我想在C++中创建一个二叉树,下面是我的代码 include <iostream> using namespace std; struct tree{ tree * child_left = NULL; tree * child_right = NULL; int root; tree * parent = NULL; }; int main(int argc, const char * argv[]) { tree *t1 = new tree; tree *t2 = new tree; tree *t3 = new tree; tree *t4 = new tree; tree *t5 = new tree; tree *t6 = new tree; tree *t7 = new tree; t4->root = 1; t5->root = 3; t6->root = 6; t7->root = 9; t2->root = 2; t2->child_left = t4; t2->child_right = t5; t3->root = 7; t3->child_left = t6; t3->child_right = t7; t1->root = 4; t1->child_left = t2; t1->child_right = t3; cout << t1->child_left->child_right->root; return 0; } 包括 使用名称空间std; 结构树{ 树*child_left=NULL; 树*child_right=NULL; int根; tree*parent=NULL; }; int main(int argc,const char*argv[]{ tree*t1=新树; tree*t2=新树; tree*t3=新树; tree*t4=新树; tree*t5=新树; tree*t6=新树; tree*t7=新树; t4->root=1; t5->root=3; t6->root=6; t7->root=9; t2->root=2; t2->child_left=t4; t2->child_right=t5; t3->root=7; t3->child_left=t6; t3->child_right=t7; t1->root=4; t1->child_left=t2; t1->child_right=t3; 不能左下->右下->根; 返回0; }

C++; 超级哑问题,我想在C++中创建一个二叉树,下面是我的代码 include <iostream> using namespace std; struct tree{ tree * child_left = NULL; tree * child_right = NULL; int root; tree * parent = NULL; }; int main(int argc, const char * argv[]) { tree *t1 = new tree; tree *t2 = new tree; tree *t3 = new tree; tree *t4 = new tree; tree *t5 = new tree; tree *t6 = new tree; tree *t7 = new tree; t4->root = 1; t5->root = 3; t6->root = 6; t7->root = 9; t2->root = 2; t2->child_left = t4; t2->child_right = t5; t3->root = 7; t3->child_left = t6; t3->child_right = t7; t1->root = 4; t1->child_left = t2; t1->child_right = t3; cout << t1->child_left->child_right->root; return 0; } 包括 使用名称空间std; 结构树{ 树*child_left=NULL; 树*child_right=NULL; int根; tree*parent=NULL; }; int main(int argc,const char*argv[]{ tree*t1=新树; tree*t2=新树; tree*t3=新树; tree*t4=新树; tree*t5=新树; tree*t6=新树; tree*t7=新树; t4->root=1; t5->root=3; t6->root=6; t7->root=9; t2->root=2; t2->child_left=t4; t2->child_right=t5; t3->root=7; t3->child_left=t6; t3->child_right=t7; t1->root=4; t1->child_left=t2; t1->child_right=t3; 不能左下->右下->根; 返回0; },c++,binary-tree,C++,Binary Tree,这实际上是可行的,但如果在声明这些节点时删除新节点,xcode将出现类似(Thread1:EXC_BAD_ACCESS(code=1,address=0x10))的错误 我想知道是什么导致了线程问题,以及为什么在声明这些节点时需要使用new 提前谢谢 您正在声明指向树对象的指针。如果不使用new,就不会为指针分配任何对象 int main(int argc, const char * argv[]) { tree t1, t2, t3, t4, t5, t6, t7; t4.root =

这实际上是可行的,但如果在声明这些节点时删除新节点,xcode将出现类似(Thread1:EXC_BAD_ACCESS(code=1,address=0x10))的错误

我想知道是什么导致了线程问题,以及为什么在声明这些节点时需要使用new


提前谢谢

您正在声明指向树对象的指针。如果不使用new,就不会为指针分配任何对象

int main(int argc, const char * argv[]) {
tree t1, t2, t3, t4, t5, t6, t7;

  t4.root = 1;
  t5.root = 3;
  t6.root = 6;
  t7.root = 9;
  t2.root = 2;
  t2.child_left = &t4;
  t2.child_right = &t5;
  t3.root = 7;
  t3.child_left = &t6;
  t3.child_right = &t7;
  t1.root = 4;
  t1.child_left = &t2;
  t1.child_right = &t3;
  cout << t1.child_left->child_right->root;
  return 0;
}
int main(int argc,const char*argv[]{
树t1、t2、t3、t4、t5、t6、t7;
t4.root=1;
t5.root=3;
t6.root=6;
t7.root=9;
t2.root=2;
t2.child_left=&t4;
t2.儿童权利=&t5;
t3.root=7;
t3.child_left=&t6;
t3.child_right=&t7;
t1.root=4;
t1.child_left=&t2;
t1.child_right=&t3;
cout child\u right->root;
返回0;
}
这负责在堆栈上创建对象,然后在作用域结束时自动销毁这些对象


可能应该有一个接受参数的ctor,以便您创建一个对象而不是一包数据。

您正在声明指向树对象的指针。如果不使用new,就不会为指针分配任何对象

int main(int argc, const char * argv[]) {
tree t1, t2, t3, t4, t5, t6, t7;

  t4.root = 1;
  t5.root = 3;
  t6.root = 6;
  t7.root = 9;
  t2.root = 2;
  t2.child_left = &t4;
  t2.child_right = &t5;
  t3.root = 7;
  t3.child_left = &t6;
  t3.child_right = &t7;
  t1.root = 4;
  t1.child_left = &t2;
  t1.child_right = &t3;
  cout << t1.child_left->child_right->root;
  return 0;
}
int main(int argc,const char*argv[]{
树t1、t2、t3、t4、t5、t6、t7;
t4.root=1;
t5.root=3;
t6.root=6;
t7.root=9;
t2.root=2;
t2.child_left=&t4;
t2.儿童权利=&t5;
t3.root=7;
t3.child_left=&t6;
t3.child_right=&t7;
t1.root=4;
t1.child_left=&t2;
t1.child_right=&t3;
cout child\u right->root;
返回0;
}
这负责在堆栈上创建对象,然后在作用域结束时自动销毁这些对象


可能应该有一个接受参数的ctor,这样您就可以创建一个对象而不是一包数据。

因为
new
关键字实际上创建了指针指向的对象。如果没有
new
关键字,指针将未初始化,取消对它的引用将导致未定义的行为


此外,正确编写的代码必须始终
删除
使用
new
实例化的所有对象,以避免内存泄漏。在这个简单的例子中不需要,但最好尽早学习好习惯。

因为
new
关键字实际上创建了指针指向的对象。如果没有
new
关键字,指针将未初始化,取消对它的引用将导致未定义的行为


此外,正确编写的代码必须始终
删除
使用
new
实例化的所有对象,以避免内存泄漏。在这个简单的示例中不需要,但最好尽早学习好习惯。

初始化指针时需要使用新语句,因为所有节点/树变量都是指针。在开发数据结构时,由于指针的性质(元素的动态数量、可以在数据结构上执行的操作几乎总是依赖于堆分配的值等),使用指针是非常常见的初始化指针时需要使用新语句,因为所有节点/树变量都是指针。在开发数据结构时,由于指针的性质(元素的动态数量、可以在数据结构上执行的操作几乎总是依赖于堆分配的值等),使用指针是非常常见的

我想我应该指出,当使用关键字
new
时,实际上是在堆上创建一个变量,并在堆栈上创建指向该变量的指针。如果没有使用单词
new
,则只会在堆栈上创建指针,而不是创建变量

在C++中,每当你在堆上分配某个东西时,你必须记住在不再需要它时删除它。与堆栈上的变量不同,这些变量不会自动删除,因为它们永远不会超出范围。为此,您需要使用关键字
delete
,否则会出现“内存泄漏”

我建议您阅读本快速教程,以澄清您的动态编程概念:


希望这能让你对正在发生的事情有所了解

我想我应该指出,当您使用关键字
new
时,实际上是在堆上创建一个变量,并在堆栈上创建指向该变量的指针。如果没有使用单词
new
,则只会在堆栈上创建指针,而不是创建变量

在C++中,每当你在堆上分配某个东西时,你必须记住在不再需要它时删除它。与堆栈上的变量不同,这些变量不会自动删除,因为它们永远不会超出范围。为此,您需要使用关键字
delete
,否则会出现“内存泄漏”

我建议您阅读本快速教程,以澄清您的动态编程概念:


希望这能让你对正在发生的事情有所了解

如果不使用
新树
,则