基于二叉链表的二叉树的的嵌套括号表示
描述
二叉树的嵌套括号表示定义为:如果是叶子结点,则用一个字母表示,否则每个字母后面带有一对括号,括号中逗号左边是左子树,逗号右边是右子树,下图所示的二叉树的嵌套括号表示为:A(B(D,),C)
输入
多组数据,每组数据有一行,为一个二叉树的先序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。
输出
每组数据输出一行。输出二叉树的嵌入括号表示。
输入样例 1
ABD00E00C00 A0B0C00 ABC0000 0
输出样例 1
A(B(D,E),C) A(,B(,C)) A(B(C,),)
示例一(超级大佬)
#include<iostream>
using namespace std;
typedef struct BiNode{
char data;
struct BiNode *lchild,*rchild;
}BiNode,*BiTree;
void CreateBiTree(BiTree &T,char s[],int &i)
{
if(s[i]=='0')T=NULL;
else
{
T = new BiNode;
T->data=s[i];
CreateBiTree(T->lchild,s,++i);
CreateBiTree(T->rchild,s,++i);
}
}
void match(BiTree &T,int x)
{
if(T)
{
cout<<T->data;
if(T->lchild&&T->rchild)
{
cout<<"(";
match(T->lchild,x);
cout<<",";
match(T->rchild,x);
cout<<")";
}
else if(T->lchild&&T->rchild==NULL)
{
cout<<"(";
match(T->lchild,x);
cout<<",";
cout<<")";
}
else
if(T->lchild==NULL&&T->rchild)
{
cout<<"(";
cout<<",";
match(T->rchild,x);
cout<<")";
}
}
}
int main()
{
char s[100];
while(cin>>s&&s[0]!='0')
{
int i=-1;
BiTree T;
CreateBiTree(T,s,++i);
match(T,0);
cout<<endl;
}
return 0;
}
示例二
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T,string s,int &i)
{
if(s[i]=='0')
T=NULL;
else
{
T=new BiTNode;
T->data=s[i];
CreateBiTree(T->lchild,s,++i);
CreateBiTree(T->rchild,s,++i);
}
}
void Show(BiTree T)
{
if(T)
{
cout<<T->data;
if(T->lchild||T->rchild)
{
cout<<"(";
Show(T->lchild);
cout<<",";
Show(T->rchild);
cout<<")";
}
}
}
int main()
{
string s;
cin>>s;
while(s!="0")
{
BiTree T;
int i=-1;
CreateBiTree(T, s, ++i);
Show(T);
cout<<endl;
cin>>s;
}
return 0;
}
示例三、
//11/23/2020, 14:05
//基于二叉链表的二叉树的的嵌套括号表示
#include <iostream>
#define MAX 100
using namespace std;
typedef struct BiTNode {
char data;
struct BiTNode* lchild, * rchild;
}BiTNode, * BiTree;
//先序遍历:根左右
void CreateBiTree(BiTree& T, char* s, int& i) {
if (s[i] == '0')
T = NULL;
else {
T = new BiTNode;
T->data = s[i]; //根
CreateBiTree(T->lchild, s, ++i);//递归创建左子树
CreateBiTree(T->rchild, s, ++i);
}
}
//先序遍历,输出带括号的二叉树
void PreOrderTraverse(BiTree T) {
if (T) {
cout << T->data;
if (T->lchild || T->rchild)
cout << '(';
PreOrderTraverse(T->lchild);
if (T->lchild || T->rchild)
cout << ',';
PreOrderTraverse(T->rchild);
if (T->lchild || T->rchild)
cout << ')';
}
}
int main()
{
char ch[MAX];
while (cin >> ch && ch[0] != '0') {
BiTree T;
int i = -1;
CreateBiTree(T, ch, ++i);
PreOrderTraverse(T);
cout << endl;
}
return 0;
}
示例四、
#include<iostream>
using namespace std;
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T)
{
char ch;
cin>>ch;
if(ch=='0')
T=NULL;
else
{
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void Match(BiTree &T)
{
if(T)
{
if(!T->lchild&&!T->rchild)
cout<<T->data;
else
{
cout<<T->data<<"(";
Match(T->lchild);
cout<<",";
Match(T->rchild);
cout<<")";
}
}
}
int main()
{
BiTree T;
while(1)
{
CreateBiTree(T);
if(T==NULL)
break;
Match(T);
cout<<endl;
}
return 0;
}
示例五
#include<iostream>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}*BiTree;
void Create(BiTree &T)
{
char ch;
cin>>ch;
if(ch=='0')
T=NULL;
else
{
T=new BiTNode;
T->data=ch;
Create(T->lchild);
Create(T->rchild);
}
}
void shuchu(BiTree &T,int x)
{
if(T)
{
cout<<T->data;
if(T->lchild!=NULL&&T->rchild!=NULL)
{
cout<<"(";
shuchu(T->lchild,x);
cout<<",";
shuchu(T->rchild,x);
cout<<")";
}
else
if(T->lchild!=NULL&&T->rchild==NULL)
{
cout<<"(";
shuchu(T->lchild,x);
cout<<",)";
}
else//abcd00e00f00ig00h00
if(T->lchild==NULL&&T->rchild!=NULL)
{
cout<<"(,";
shuchu(T->rchild,x);
cout<<")";
}
/* else
if(T->lchild==NULL&&T->rchild==NULL)
{
cout<<",";
}*/
}
}
int main()
{
BiTree T;
while(1)
{
Create(T);
if(T==NULL)
break;
shuchu(T,0);
cout<<endl;
}
return 0;
}
示例六、
#include<iostream>
using namespace std;
string s;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild ,*rchild;
}BiTNode,*BiTree;
void createBiTree(BiTree&t)
{
char temp;
cin>>temp;
if(temp=='0')
t=NULL;
else
{
t=new BiTNode;
t->data=temp;
createBiTree(t->lchild);
createBiTree(t->rchild);
}
}
void qiantao(BiTree T)
{
if(!T)
return ;
else if(!T->lchild&&!T->rchild)
cout<<T->data;
else
{
cout<<T->data;
cout<<"(";
qiantao(T->lchild);
cout<<",";
qiantao(T->rchild);
cout<<")";
}
}
int main()
{
while(1)
{
s="";
BiTree t1;
createBiTree(t1);
if(!t1)
break;
qiantao(t1);
cout<<endl;
}
return 0;
}
示例七、
#include<iostream>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T,char S[],int &i)
{
if(S[i]=='0')
T=NULL;
else
{
T=new BiTNode;
T->data=S[i];
CreateBiTree(T->lchild,S,++i);
CreateBiTree(T->rchild,S,++i);
}
}
void OutPut(BiTree T)
{
if(T)
{
cout<<T->data;
if(T->lchild||T->rchild)
{
cout<<'(';
OutPut(T->lchild);
cout<<',';
OutPut(T->rchild);
cout<<')';
}
}
}
int main()
{
char S[100];
while(cin>>S&&S[0]!='0')
{
BiTree T;
int i=0;
CreateBiTree(T,S,i);
OutPut(T);
cout<<endl;
}
return 0;
}
示例八、
#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode, *BiTree;
void CreateBiTree(BiTree &T)
{
char ch;cin>>ch;
if(ch=='0')
T=NULL;
else
{
T=new BiTNode;
T->data =ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void QianTao(BiTree &T)
{
if(T)
{
if(T->lchild==NULL&&T->rchild==NULL)
cout<<T->data;
else
{
cout<<T->data;
cout<<"(";
QianTao(T->lchild);
cout<<",";
QianTao(T->rchild);
cout<<")";
}
}
}
int main()
{
BiTree T;
while(1)
{
T=NULL;
CreateBiTree(T);
if(T==NULL) break;
QianTao(T);
cout<<endl;
}
return 0;
}
示例十、
#include<iostream>
using namespace std;
typedef struct binode
{
char data;
binode *lchild, *rchild;
}binode, *bitree;
void create(bitree &T)
{
char ch;
cin >> ch;
if (ch == '0')
T = NULL;
else
{
T = new binode;
T->data = ch;
create(T->lchild);
create(T->rchild);
}
}
void Show(bitree &T)
{
if (T)
{
cout << T->data;
if (T->lchild||T->rchild)
{
cout << '(';
Show(T->lchild);
cout << ',';
Show(T->rchild);
cout << ')';
}
}
}
int main()
{
while (1)
{
bitree T;
create(T);
if (T == NULL)
break;
Show(T);
cout << endl;
}
return 0;
}
示例十一
#include<bits/stdc++.h>
using namespace std;
typedef struct BiTNode
{
char data;
BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void Int(BiTree & T)
{
char c;
cin>>c;
if(c == '0') T=NULL;
else
{
T = new BiTNode;
T->data = c;
Int(T->lchild);
Int(T->rchild);
}
}
void Show(BiTree &T)
{
if(T)
{
cout<<T->data;
if(T->lchild||T->rchild) cout<<'(';
Show(T->lchild);
if(T->lchild||T->rchild) cout<<',';
Show(T->rchild);
if(T->lchild||T->rchild) cout<<')';
}
}
int main()
{
BiTree T;
Int(T);
while(T)
{
Show(T);
cout<<endl;
Int(T);
}
return 0;
}
示例十二
#include<iostream>
using namespace std;
typedef struct BiTNode{
struct BiTNode *lchild,*rchild;
char data;
}BiTNode,*Bitree;
void CreateTree(Bitree &T,char ch[],int &i){
if(ch[i]=='0')
T=NULL;
else{
T=new BiTNode;
T->data=ch[i];
CreateTree(T->lchild,ch,++i);
CreateTree(T->rchild,ch,++i);
}
}
void PreOrderTraverse(Bitree& T) {
if(T==NULL)return ;
else
{
cout << T->data;
if(T->lchild!=NULL||T->rchild!=NULL)cout<<'(';
PreOrderTraverse(T->lchild);
if(T->lchild!=NULL||T->rchild!=NULL)cout<<',';
PreOrderTraverse(T->rchild);
if(T->lchild!=NULL||T->rchild!=NULL)cout<<')';
}
}
int main(){
while(1){
char ch[100];
cin>>ch;
if(ch[0]=='0')
break;
Bitree bt;
int i=-1;
CreateTree(bt,ch,++i);
PreOrderTraverse(bt);
cout<<endl;
}
return 0;
}
示例十三、
/*#include<iostream>
using namespace std;
int a,b,c;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T,char S[],int &i)
{
if(S[i]=='0')
T=NULL;
else
{
T=new BiTNode;
T->data=S[i];
CreateBiTree(T->lchild,S,++i);
CreateBiTree(T->rchild,S,++i);
}
}
void Count(BiTree T)
{
if(T)
{
if(T->lchild&&T->rchild)
c++;
else if(T->lchild||T->rchild)
b++;
else
a++;
Count(T->lchild);
Count(T->rchild);
}
}
int main()
{
char S[100];
while(cin>>S&&S[0]!='0')
{
a=b=c=0;
int i=-1;
BiTree T;
CreateBiTree(T,S,++i);
Count(T);
cout<<a+b+c<<endl;
}
return 0;
}*/
/*
#include<iostream>
using namespace std;
typedef struct BiNode
{
char data;
struct BiNode *lchild;
struct BiNode *rchild;
}BiNode,*BiTree;
void CreatTree(BiTree &T)
{
char ch;
cin>>ch;
if(ch=='0')
T=NULL;
else
{
T=new BiNode;
T->data=ch;
CreatTree(T->lchild);
CreatTree(T->rchild);
}
}
void ThreeTraverse(BiTree &T)
{
if(T)
{
cout<<T->data;
ThreeTraverse(T->lchild);
//ThreeTraverse(T->lchild);
cout<<T->data;
ThreeTraverse(T->rchild);
//ThreeTraverse(T->rchild);
cout<<T->data;
}
}
int main()
{
BiTree T;
while(1)
{ //BiTree T;
CreatTree(T);
if(T==NULL)
break;
ThreeTraverse(T);
cout<<endl;
}
return 0;
}*/
#include<iostream>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreatTree(BiTree &T)
{
char ch;
cin>>ch;
if(ch=='0')
T=NULL;
else
{
T=new BiTNode;
T->data=ch;
CreatTree(T->lchild);
CreatTree(T->rchild);
}
}
void Output(BiTree T)
{
if(T&&T->lchild==NULL&&T->rchild==NULL)
cout<<T->data;
else
if(T)
{
cout<<T->data;
cout<<'(';
Output(T->lchild);
cout<<',';
Output(T->rchild);
cout<<')';
}
}
int main()
{
while(1)
{
BiTree T;
CreatTree(T);
if(T==NULL)
break;
Output(T);
cout<<endl;
}
return 0;
}