二叉树问题,求解为什么输出不了
#include<iostream>
#include<string>
#include<valarray>
#include<vector>
#include<malloc.h>
#include<math.h>
using namespace std;
typedef struct Node
{
int data;
struct Node* LChild;
struct Node* RChild;
}BiTree;
void CreateTree(BiTree *t)
{
int a;
cin>>a;
if (a==0)
{
t=NULL;
}
else {
while(a!=0)
{
t=new BiTree;
t->data=a;
CreateTree(t->LChild);
CreateTree(t->RChild);
}
}
}
void pri(BiTree *t)
{
if(t!=NULL)
{
cout<<t->data;
pri(t->LChild);
pri(t->RChild);
}
}
int main()
{
BiTree *T=new BiTree;
cout<<"请输入数据,将以完全二叉树形式建立"<<endl;
CreateTree(T);
pri(T);
cout<<T->data;
return 0;
}
#include<iostream>
using namespace std;
typedef struct Node
{
int data;
struct Node* LChild;
struct Node* RChild;
}BiTree;
//要修改指针,参数用指针的引用
//结构没有定义父指针,用个标志来表明添加左指针还是右指针
void CreateTree(BiTree*& t, int flag)
{
int a;
//这里要处理cin输入出错的情况
cin >> a;
while(!cin)
{
cin.clear();
cin.ignore();
cout << "输入错误,重新输入:\n";
cin >> a;
}
if (a == 0)
{
return;
}
BiTree* child;
if (t == NULL) //NULL是根节点
{
t = new BiTree;
child = t;
}
else
{
child = new BiTree;
//连接上父节点
if (flag == 0)
t->LChild = child;
else
t->RChild = child;
}
child->data = a;
child->LChild = NULL;
child->RChild = NULL;
CreateTree(child, 0);
CreateTree(child, 1);
}
void pri(BiTree* t)
{
if(t!=NULL)
{
cout << t->data << ' ';
pri(t->LChild);
pri(t->RChild);
}
}
int main()
{
BiTree *T = NULL;
cout<<"请输入数据,将以完全二叉树形式建立"<<endl;
CreateTree(T, 0);
pri(T);
cout << endl;
return 0;
}