【データ構造】ツリーを作成する方法
通常のツリーを作成する方法:
具体的には、コードを参照してください.
具体的には、コードを参照してください.
// 、 、 , 。
//
#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;
}