快速建立的一种方法:
#include <iostream>
#include <cstdlib>
using namespace std;
#define type char
typedef struct TREE
{
type value;
struct TREE *LSubTree;
struct TREE *RSubTree;
}TreeNode, *PtNode;
PtNode BuildTree()
{
PtNode root;
char ch;
root=new TreeNode;
if(!root)
exit(-1);
cin>>ch;
if(ch=='@')
root=NULL;
else
{
root=new TreeNode;
root->value=ch;
root->LSubTree=BuildTree();
root->RSubTree=BuildTree();
}
return root;
}
void Inorder(PtNode root)
{
if(root!=NULL)
{
Inorder(root->LSubTree);
cout<< root ->value<<endl;
Inorder (root ->RSubTree);
}
}
void Postorder(PtNode root)
{
if(root!=NULL)
{
Inorder( root->LSubTree);
Inorder( root->RSubTree);
cout<< root ->value<<endl;
}
}
/////////////////////////////////////////////
int main()
{
PtNode tree;
cout<<"Test began !\n"
<<"Enter some letters for the tree:(SYMBOL '@' MEANS BLANK)\n";
tree=BuildTree();
Inorder( tree);
Postorder( tree);
return 0;
}