关于二叉树问题

luozike_110110 2009-06-14 11:07:14
#include<iostream>
using namespace std;
typedef struct Node
{
int key;
Node *patent;
Node *left;
Node *right;
}Node,Tree;
void InitTree(Tree *t)
{
t->key=NULL;
t->patent=NULL;
t->left=NULL;
t->right=NULL;
}
void InsertNode(Tree *t,int key)
{
Node *x,*y;
Node *p=new Node;
p->key=key;
p->left=NULL;
p->right=NULL;
p->patent=NULL;
x=t;
y=NULL;
while(x->key!=NULL)
{
y=x;
if(x->key>p->key)
x=x->left;
else
x=x->right;
}
p->patent=y;
if(y==NULL)
t=p;
else
if(p->key<y->key)
y->left=p;
else
y->right=p;
}
void InOrder(Tree *t)
{
if(t!=NULL)
{
InOrder(t->left);
cout<<t->key<<" ";
InOrder(t->right);
}

}

void main()
{
Tree *t=new Tree;
InitTree(t);

InsertNode(t,12);
InsertNode(t,6);
InsertNode(t,15);
InsertNode(t,18);
InsertNode(t,2);
}

在运行时,应该是内存分配出问题了,请教各位了!插入节点出问题了!
...全文
72 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
luozike_110110 2009-06-14
  • 打赏
  • 举报
回复
非常感谢各位的帮助,让我这个刚学的学生很感动!
hz_yck 2009-06-14
  • 打赏
  • 举报
回复
#include <iostream> 
using namespace std;
typedef struct Node
{
int key;
Node *patent;
Node *left;
Node *right;
}Node,Tree;
//void InitTree(Tree *t) //这个init把问题搞复杂了
//{
// t->key=NULL;
// t->patent=NULL;
// t->left=NULL;
// t->right=NULL;
//}
void InsertNode(Tree **t,int key) //改成了传指针的指针,虽然看上去很怪
{
Node *x,*y;
Node *p=new Node;
p->key=key;
p->left=NULL;
p->right=NULL;
p->patent=NULL;
x=*t;
y=NULL;
while(x) //如果有上面那个init的话,叶子和新的根的判断就不一样了,要while(x && x->key)
{
y=x;
if(x->key>p->key) x=x->left;
else x=x->right;
}
p->patent=y;
if(y==NULL) //原先的话新的根被附到参数传值产生的局部变量t上面了,所以等于没改根节点
*t=p; //按原先的应该是t->key=key,这样刚分配的p又要马上释放掉
else{
if(p->key <y->key) y->left=p;
else y->right=p;
}
}
void InOrder(Tree *t)
{
if(t)
{
InOrder(t->left);
cout <<t->key <<" ";
InOrder(t->right);
}
}

int main()
{
Tree *t(NULL);
// InitTree(t);

InsertNode(&t,12);
InsertNode(&t,6);
InsertNode(&t,15);
InsertNode(&t,18);
InsertNode(&t,2);
InOrder(t); //内存管理没加
system("PAUSE");
return 0;
}
askcyg 2009-06-14
  • 打赏
  • 举报
回复
自己看下下面的代码:

#include <iostream>
using namespace std;

typedef struct Node
{
int key;
Node *patent;
Node *left;
Node *right;
}Node,Tree;

void InitTree(Tree *t)
{
t->key=NULL;
t->patent=NULL;
t->left=NULL;
t->right=NULL;
}
void InsertNode(Tree *t,int key)
{
Node *x,*y;
Node *p=new Node;
p->key=key;
p->left=NULL;
p->right=NULL;
p->patent=NULL;
x=t;
y=NULL;
while(x->key!=NULL)
{
if(x->key>p->key ) {
if(x->left != NULL)
x=x->left;
else {
y=x;
break;
}

}
else {
if(x->right != NULL)
x=x->right;
else {
y=x;
break;
}
}
}
p->patent=y;
if(y==NULL)
*t=*p;
else
if(p->key < y->key)
y->left=p;
else
y->right=p;
}
void InOrder(Tree *t)
{
if(t!=NULL)
{
InOrder(t->left);
cout <<t->key <<" ";
InOrder(t->right);
}

}

void main()
{
Tree *t=new Tree;
InitTree(t);

InsertNode(t,12);
InsertNode(t,6);
InsertNode(t,15);
InsertNode(t,18);
InsertNode(t,2);
}
luozike_110110 2009-06-14
  • 打赏
  • 举报
回复
若有兴趣,麻烦各位调试一下,怎样才能使上面的程序中插入节点成功!
luozike_110110 2009-06-14
  • 打赏
  • 举报
回复
插入节点时内存分配出问题!我没找到本质原因
光宇广贞 2009-06-14
  • 打赏
  • 举报
回复
……看不出问题来……除了堆内存没管理之外……
ltc_mouse 2009-06-14
  • 打赏
  • 举报
回复
x=t;
y=NULL;
---------------
y的初始值也应该为t吧。对这种问题,lz应该单步调试,看看哪里出错了,再慢慢分析~~
mengde007 2009-06-14
  • 打赏
  • 举报
回复
没有释放内存呗;
  • 打赏
  • 举报
回复
问题是什么?可以编译,可以运行啊

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧