【数据结构】创建二叉树的方法

来源:互联网 发布:淘宝买大麻暗语2016 编辑:程序博客网 时间:2024/05/01 11:31

创建普通二叉树的方法:

具体可以看代码:

//交谈中请勿轻信汇款、中奖信息、陌生电话,勿使用外挂软件。//#include <iostream>using namespace std;typedef struct BiTNode{char data;struct BiTNode *lchild,*rchild;}BiTNode;BiTNode *CreateBinTree (){ char ch;//scanf("%c",&ch);cin>>ch;BiTNode *root = (BiTNode*)malloc(sizeof(BiTNode));//根节点if(ch=='#') root = NULL; //将相应指针置空 else{ root->data=ch;root->lchild=CreateBinTree(); //构造左子树root->rchild=CreateBinTree(); //构造右子树}return root;}void preOrder(BiTNode *root){if (root==NULL)return;cout<<root->data<<" ";preOrder(root->lchild);preOrder(root->rchild);}int main(){BiTNode *root = NULL;cout<<"Please Input The Node:"<<endl;root = CreateBinTree();cout<<endl;cout<<"The PreOrder is:";preOrder(root);cout<<endl;return 0;}


 

原创粉丝点击