您的位置:首页 > 编程语言 > C语言/C++

建立二叉树和输出

2015-08-18 17:06 381 查看
<span style="font-size:18px;">#include<iostream>
using namespace std;
typedef char elemtype;
typedef struct Node
{
elemtype data;
Node *Leftchild,*Rightchild;
}Node;
//建立二叉树
Node* creatTree()
{
elemtype ch;
cin>>ch;
Node*T;
if(ch=='#')
T=NULL;
else
{
T=(Node*)malloc(sizeof(Node));
T->data=ch;
T->Leftchild=creatTree();
T->Rightchild=creatTree();
}
return T;
}
void preorder(Node *t)
{
if(t!=NULL)
{
printf("%c ",t->data);
preorder(t->Leftchild);
preorder(t->Rightchild);
}
}
int main()
{
Node *T=creatTree();
preorder(T);
return 0;
}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++