#include
using namespace std;
struct node
{
struct node *lchild ;
struct node *rchild ;
char data ;
};
typedef struct node * BTREE ;
void CreateBT(BTREE & T)
{
char ch;
cin >> ch;
if (ch == '#')
T = NULL;
else
{
T = new node;
T -> data = ch;
CreateBT (T -> lchild);
CreateBT (T -> rchild);
}
}
void InOrder (BTREE BT )
{
if ( BT != NULL)
{
InOrder ( BT->lchild ) ;
cout<< BT->data <<" ";
InOrder ( BT->rchild ) ;
}
}
void Print(BTREE BT)
{
if (BT != NULL)
{
cout << BT->data; //输出根结点的值
if (BT->lchild != NULL || BT->rchild != NULL)
{
cout << '('; //输出左括号
Print(BT->lchild); //输出左子树
if (BT->rchild != NULL)
cout << ','; //若右子树不为空则输出逗号分隔符
Print(BT->rchild); //输出右子树
cout << ')'; //输出右括号
}
}
}
int main()
{
BTREE root;
cout << "先序输入‘#’代表叶子节点:"< CreateBT (root ); //创建二叉树 cout << "广义表表示:"; Print(root); cout << endl; //以广义表的形root式输出二叉树 cout<<"中序遍历"; InOrder (root ); cout< return 0; } 运行结果 希望能帮到你