#include<iostream> #define DaType int #define ElemData int using namespace std; typedef struct BNode{ DaType data; struct BNode *Lchild; struct BNode *Rchild; }BNode,*BTree; //遍历 void Show(BTree t) { if(t) { //Show(t->Lchild); cout<<" "<<t->data<<endl; //Show(t->Rchild); } } //插入 int Insert(BTree &t,ElemData elem) { if(t==NULL) { t= new BNode; t->data = elem; t->Lchild = NULL; t->Rchild = NULL; return 0; } if(elem < t->data) { if(t->Lchild==NULL)//如果为空 则填入 { t->Lchild=new BNode;//new空间 t->Lchild->data = elem;//赋值 return 0; } else Insert(t->Lchild,elem);//不符合则 以当前为节点递归 } else { if(t->Rchild==NULL) { t->Rchild = new BNode; t->Rchild->data = elem; return 0; } else Insert(t->Rchild,elem); } return 0; } //创建 void Creat(BTree &t,int a) { int i; for(i=0;i<a;i++) { ElemData data; cin>>data; Insert(t,data); } } int main() { BTree t2; t2=NULL; int a; cout<<"请输入二叉树的个数"; cin>>a; cout<<"输入二叉树"<<endl; Creat(t2,a); Show(t2); if(t2->Lchild==NULL) cout<<"sss"; system("pause"); }
全部评论
(2) 回帖