一个二叉树创建的问题

jack_za123 2009-02-17 02:38:54
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct stu
{
int id;
char name[32];
}STU;

typedef struct tree
{
STU student;
struct tree *lchild,*rchild;
}Tree;

Tree **creatTree(Tree **root,const int n)//改
{
Tree **temp;//改

if (root == NULL)
{
root = (Tree**)malloc(sizeof(Tree)*n);//改

if ( root == NULL )
{
cout<<"内存分配不成功!"<<endl;
exit(1);
}
else
{
temp = root;
for (int i=0;i<n;i++)
{
root[i]->student.id=i;
}

for ( int j=0;j<((n+1)/2-1);j++ )
{
root[j]->lchild = root[j+1];
root[j]->rchild = root[j+2];
}

for( int k=((n+1)/2);k<n;k++)
{
root[k]->lchild = root[k]->rchild = NULL;
}
}

}
return root;
}


void showTree(Tree * root)
{
if(root != NULL)
{

if(root->lchild != NULL)
showTree(root->lchild);
if(root->rchild != NULL)
showTree(root->rchild);

cout<<root->student.id<<endl;
}
}
void main()
{
Tree **root=NULL;
int n;
cin>>n;
root = creatTree(root,n);

showTree(*root);
}

程序可以运行,但输入节点数后,程序崩溃,请高手半我看看改怎么改
...全文
94 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
renyaoming 2009-02-17
  • 打赏
  • 举报
回复
up混点分
nullah 2009-02-17
  • 打赏
  • 举报
回复
ps:错误的句子贴错了

错误在LZ代码中分配内存 楼主可以自己调试哈看看在哪句中断的
nullah 2009-02-17
  • 打赏
  • 举报
回复
不是没有free的问题 是你分配内存错误
调试的时候是这句内存写入错误root = (Tree*)malloc(sizeof(Tree)*n); 所以明显是内存没分配对
跟释放根本没关系
而且你初始化树的时候有错误 详细见我修改的地方

#include <iostream>
#include <stdlib.h>

using namespace std;

typedef struct stu
{
int id;
char name[32];
}STU;

typedef struct tree
{
STU student;
struct tree *lchild,*rchild;
}Tree;

Tree *creatTree(Tree *&root,const int n)//改
{
//Tree **temp;//改

if (root == NULL)
{
root = (Tree*)malloc(sizeof(Tree)*n);//改

if ( root == NULL )
{
cout << "内存分配不成功!" <<endl;
exit(1);
}
else
{
//temp = root;
for (int i=0;i<n;i++)
{
root[i].student.id = i;
}

for ( int j=0; j<n; j++ )
{
//root[j].lchild = &root[j+1];
//root[j].rchild = &root[j+2];
if((2*j+1)<n)
{
root[j].lchild = &root[2*j+1];
}
else
{
root[j].lchild = NULL;
}

if((2*j+2)<n)
{
root[j].rchild = &root[2*j+2];
}
else
{
root[j].rchild = NULL;
}
}

for( int k=((n+1)/2);k <n;k++)
{
root[k].lchild = root[k].rchild = NULL;
}
}

}
return root;
}


void showTree(Tree * root)
{
/*if(root != NULL)
{

if(root->lchild != NULL)
showTree(root->lchild);
if(root->rchild != NULL)
showTree(root->rchild);

cout << root->student.id <<endl;
}*/
if(root == NULL)
{
//do nothing
}
else
{
cout << root->student.id << endl;
showTree(root->lchild);
showTree(root->rchild);
}
}
void main()
{
Tree *root = NULL;
int n;
cin>>n;
root = creatTree(root,n);

showTree(root);
}

构造的完全二叉树
输出的按照前序遍历
VS2008下output:
5
0
1
3
4
2
按任意键继续......
waizqfor 2009-02-17
  • 打赏
  • 举报
回复
[Quote=引用楼主 zhiyou007 的帖子:]
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct stu
{
int id;
char name[32];
}STU;

typedef struct tree
{
STU student;
struct tree *lchild,*rchild;
}Tree;

Tree **creatTree(Tree **root,const int n)//改
{
Tree **temp;//改

if (root == NULL)
{
root = (Tree**)malloc(sizeof(Tree)*n);//改


[/Quote]
你程序崩溃是因为 你malloc 完了 没用free吧
sea_sharka_17 2009-02-17
  • 打赏
  • 举报
回复
你创建了内存空间 但为什么不回收咧? malloc free 这是一对 不要残忍的把他们分开
bfhtian 2009-02-17
  • 打赏
  • 举报
回复
调试下吧

65,211

社区成员

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

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