创建普通二叉树的方法:
具体可以看代码:
- //交谈中请勿轻信汇款、中奖信息、陌生电话,勿使用外挂软件。
- //
- #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;
- }