C++ 创建二叉树问题。。。

ujn_uestc 2011-02-26 11:07:18
源代码:

#include "stdafx.h"

#include <iostream>
using namespace std;
typedef char elemtype;
struct node
{
elemtype data;
struct node *left;
struct node *right;
};



node * createbitr(node **r)
{
char ch;
cout<<"input the value of tree node "<<endl;
cin>>ch;
if (ch=='q')
*r=NULL;
else
if(!(*r=new node))
cout<<"内存分配失败"<<endl;
(*r)->data=ch;
createbitr(&(*r)->left);
createbitr(&(*r)->right);

return *r;
delete *r;
}


node * preorder(node **r)
{
if(*r)
{
cout<<(*r)->data<<'\t';
preorder(&(*r)->left);
preorder(&(*r)->right);
}
return *r;

}
int _tmain(int argc, _TCHAR* argv[])
{
node *root;
createbitr(&root);
preorder(&root);

return 0;
}


运行时有个警告:
bintree.cpp
c:\documents and settings\administrator.china-df4701fb0\桌面\bintree\bintree.cpp(34) : warning C4717: 'createbitr' : recursive on all control paths, function will cause runtime stack overflow
Linking...


运行时可以输入数据,但是退出时程序崩溃。。
大家帮忙改下嘛。。谢谢
本来程序只用的一重指针,参考了下
http://topic.csdn.net/u/20110103/20/71fbaa09-cf4c-4840-9a3a-da9b6ed222b9.html
改成跟他的差不多的,也没调出来。。帮忙看下,给我个可以完整调处结果的程序,谢谢了。。
...全文
324 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ujn_uestc 2011-02-26
  • 打赏
  • 举报
回复
不过还是非常感谢大家给我的帮助。。^_^
ujn_uestc 2011-02-26
  • 打赏
  • 举报
回复
不对,你们的程序怎么都结束不了呢???- -
maoxing63570 2011-02-26
  • 打赏
  • 举报
回复

// csdn2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>
using namespace std;
typedef char elemtype;

struct node
{
elemtype data;
struct node *left;
struct node *right;
};



void createbitr(node **r)
{
char ch;
cout<<"input the value of tree node "<<endl;
cin>>ch;
if (ch=='q')
{
*r=NULL;
return;
}
else
{
if(!(*r=new node))
cout<<"内存分配失败"<<endl;
(*r)->data=ch;
}
createbitr(&(*r)->left);
createbitr(&(*r)->right);

//return *r;
//delete *r;
}


void preorder(node *r)
{
if(r)
{
cout<<r->data<<'\t';
preorder(r->left);
preorder(r->right);
}
//return *r;

}
int _tmain(int argc, _TCHAR* argv[])
{
node *root=NULL;
createbitr(&root);
preorder(root);

return 0;
}
ujn_uestc 2011-02-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 delphiwcdj 的回复:]
C# code

node * createbitr(node **r)
{
char ch;
cout<<"input the value of tree node "<<endl;
cin>>ch;
if (ch=='q')
//*r=NULL;
return NULL;
else
{
……
[/Quote]


好像不对,运行后提示一直输入,都退不出来了- -
無_1024 2011-02-26
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<malloc.h>
#define MaxSize 100
typedef struct node{
char data;
struct node *lchild;
struct node *rchild;
}BTree;
void creatree(BTree *&b,char *str)//由广义表样式创建二叉树
{
BTree *stack[MaxSize],*p;
int top=-1,j=0;//栈空
int k;//左右指标志
char ch;
b=NULL;
ch=str[j];
while(ch!='\0')
{
switch(ch)
{
case '(': top++;stack[top]=p;k=1;break;
case ')': top--;break;
case ',': k=2;break;
default: p=(BTree*)malloc(sizeof(BTree));
p->data=ch;p->lchild=p->rchild=NULL;
if(b==NULL)
b=p;//根结点
else
{
if(k==1) stack[top]->lchild=p;
if(k==2) stack[top]->rchild=p;
}
}
j++;
ch=str[j];
}
}
void printree(BTree *b)//由广义表形式输出二叉树
{
if(b!=NULL)
{
printf("%c",b->data);
if(b->lchild!=NULL||b->rchild!=NULL)
{
printf("(");
printree(b->lchild);
if(b->rchild!=NULL)
printf(",");
printree(b->rchild);
printf(")");
}
}
}
void Preorder(BTree *b){
if(b!=NULL){
printf("%c",b->data);
Preorder(b->lchild);
Preorder(b->rchild);
}
}
void zhongxu(BTree *b){
if(b){
zhongxu(b->lchild);
printf("%c",b->data);
zhongxu(b->rchild);
}
}

void houxu(BTree *b)
{
if(b!=NULL){
houxu(b->lchild);
houxu(b->rchild);
printf("%c",b->data);
}
}

int Depth(BTree *b){
int depl,depr;
if(!b)
return 0;
else{
depl=Depth(b->lchild);
depr=Depth(b->rchild);
return (depl>depr)?(depl+1):(depr+1);
}

}

int main()
{
BTree *b;
creatree(b,"(A(B(,C),D(E,F)))");
printf("广义表表示法为:\n");
printree(b);
system("pause");
}

無_1024 2011-02-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 delphiwcdj 的回复:]
C# code

node * createbitr(node **r)
{
char ch;
cout<<"input the value of tree node "<<endl;
cin>>ch;
if (ch=='q')
//*r=NULL;
return NULL;
else
{
……
[/Quote]
+1赞同这种
delphiwcdj 2011-02-26
  • 打赏
  • 举报
回复

node * createbitr(node **r)
{
char ch;
cout<<"input the value of tree node "<<endl;
cin>>ch;
if (ch=='q')
//*r=NULL;
return NULL;
else
{
if(!(*r=new node))
cout<<"内存分配失败"<<endl;
(*r)->data=ch;
createbitr(&(*r)->left);
createbitr(&(*r)->right);

return *r;
}
//delete *r;
}
龙哥依旧 2011-02-26
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef char elemtype;
struct node
{
elemtype data;
struct node *left;
struct node *right;
};



node * createbitr(node **r)
{
char ch;
cout<<"input the value of tree node "<<endl;
cin>>ch;
if (ch=='q')
*r=NULL;
else
{
if(!(*r=new node))
cout<<"内存分配失败"<<endl;
(*r)->data=ch;
createbitr(&(*r)->left);
createbitr(&(*r)->right);
}
return *r;
// delete *r;
}


node * preorder(node **r)
{
if(*r)
{
cout<<(*r)->data<<'\t';
preorder(&(*r)->left);
preorder(&(*r)->right);
}
return *r;

}
int main(int argc, char* argv[])
{
node *root;
createbitr(&root);
preorder(&root);

return 0;
}
ujn_uestc 2011-02-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 delphiwcdj 的回复:]
乱了
应该这样输入:
比如二叉树为
1
/ \
2 3
/ \ / \
q q q q
即输入1 2 q q 3 q q
要按先序输入,这样才能结束……
[/Quote]

明白了,谢谢各位了^_^

moorsf 2011-02-26
  • 打赏
  • 举报
回复
node * createbitr(node **r)
{
char ch;
cout<<"input the value of tree node "<<endl;
cin>>ch;
if (ch=='q') *r=NULL;
else
{
if(!(*r=new node)) cout<<"内存分配失败"<<endl;
else
{
(*r)->data=ch;
createbitr(&(*r)->left);
createbitr(&(*r)->right);
}
}

return *r;
//delete *r;
}
delphiwcdj 2011-02-26
  • 打赏
  • 举报
回复
乱了
应该这样输入:
比如二叉树为
1
/ \
2 3
/ \ / \
q q q q
即输入1 2 q q 3 q q
要按先序输入,这样才能结束……
delphiwcdj 2011-02-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ujn_uestc 的回复:]

不对,你们的程序怎么都结束不了呢???- -
[/Quote]
应该这样输入:
比如二叉树为 1
/ \
2 3
/ \ / \
q q q q
即输入1 2 q q 3 q q
要按先序输入,这样才能结束……

64,654

社区成员

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

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