#include<iostream>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode* lchild, * rchild;
}BiTNode, * BiTree;
void CreatBitree(BiTree& T, char e)
{
if (T == NULL)
{
T = new BiTNode;
T->data = e;
T->lchild = NULL;
T->rchild = NULL;
}
else if (T->data < e)
CreatBitree(T->rchild, e);
else if (T->data > e)
CreatBitree(T->lchild, e);
}
void Output(BiTree& T)
{
if (T)
{
cout << T->data;
if (T->lchild && T->rchild)
{
cout << "(";
Output(T->lchild);
cout << ",";
Output(T->rchild);
cout << ")";
}
else if (T->lchild)
{
cout << "(";
Output(T->lchild);
cout << ")";
}
else if (T->rchild)
{
cout << "(";
Output(T->rchild);
cout << ")";
}
}
}
int main()
{
BiTree T;
T = NULL;
char s[200];
cin >> s;
for (int i = 0; i < strlen(s); i++)
{
CreatBitree(T, s[i]);
}
Output(T);
return 0;
}
二叉树的嵌套括号输出(c++)
于 2022-03-25 16:13:05 首次发布