基于二叉链表的树结构相等的判断
描述
设二叉树中每个结点的元素均为一个字符,按先序遍历的顺序建立二叉链表,按此方法创建两棵二叉树,然后编写递归算法判断这两棵树是否相等。
输入
多组数据,每组数据有两行。每行为一个二叉树的先序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。
输出
每组数据输出一行。若两个二叉树相等输出“YES”,否则输出“NO”。
输入样例 1
abcd00e00f00ig00h00 abcd00e00f00ig00h00 abd00e00cf00g00 abd00e00cf00h00 0输出样例 1
YES NO#include<bits/stdc++.h> using namespace std; int flag; typedef struct BiNode { char data; BiNode *lchild,*rchild; }BiNode,*BiTree; void Create(BiTree &bt,char s[],int &i) { if(s[i]=='0') { bt=NULL; } else { bt=new BiNode; bt->data=s[i]; Create(bt->lchild,s,++i); Create(bt->rchild,s,++i); } } void Compare(BiTree b1,BiTree b2) { if((!b1&&b2)||(b1&&!b2)) { flag=0; } if(b1&&b2) { if(b1->data!=b2->data) { flag=0; } Compare(b1->lchild,b2->lchild); Compare(b1->rchild,b2->rchild); } } int main() { char s1[100],s2[100]; while(cin>>s1&&s1[0]!='0') { cin>>s2; BiTree t1,t2; int i=-1,j=-1; Create(t1,s1,++i); Create(t2,s2,++j); flag=1; Compare(t1,t2); if(flag==0) cout<<"NO"<<endl; else cout<<"YES"<<endl; } }
基于二叉链表的二叉树左右孩子的交换
描述
设二叉树中每个结点的元素均为一个字符,按先序遍历的顺序建立二叉链表,编写递归算法交换该二叉树的左右孩子。
输入
多组数据。每组数据一行,为二叉树的先序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。
输出
每组数据输出一行。为交换左右孩子后的二叉树的先序序列。
输入样例 1
abcd00e00f00ig00h00 abd00e00cf00g00 0输出样例 1
aihgbfced acgfbed#include<bits/stdc++.h> using namespace std; typedef struct BiNode { char data; BiNode *lchild,*rchild; }BiNode,*BiTree; void Create(BiTree &bt,char ch[],int &i) { if(ch[i]=='0') { bt=NULL; } else { bt=new BiNode; bt->data=ch[i]; Create(bt->lchild,ch,++i); Create(bt->rchild,ch,++i); } } void Swap(BiTree &bt) { if(bt) { Swap(bt->lchild); Swap(bt->rchild); BiNode *p=bt->lchild; bt->lchild=bt->rchild; bt->rchild=p; } } void PreOrder(BiTree &bt) { if(bt) { cout<<bt->data; PreOrder(bt->lchild); PreOrder(bt->rchild); } } int main() { char ch[1000]; while(cin>>ch&&ch[0]!='0') { int i=-1; BiTree bt; Create(bt,ch,++i); Swap(bt); PreOrder(bt); cout<<endl; } }
基于二叉链表的二叉树的双序遍历
描述
设二叉树中每个结点的元素均为一个字符,按先序遍历的顺序建立二叉链表,编写递归算法实现该二叉树的双序遍历(双序遍历是指对于二叉树的每一个结点来说,先访问这个结点,再按双序遍历它的左子树,然后再一次访问这个结点,接下来按双序遍历它的右子树)。
输入
多组数据。每组数据一行,为二叉树的先序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。
输出
每组数据输出一行,为双序遍历法得到的二叉树序列。
输入样例 1
ab000 ab00c00 0输出样例 1
abba abbacc#include<bits/stdc++.h> using namespace std; typedef struct BiNode { char data; BiNode *lchild,*rchild; }BiNode,*BiTree; void Create(BiTree &bt,char ch[],int &i) { if(ch[i]=='0') { bt=NULL; } else { bt=new BiNode; bt->data=ch[i]; Create(bt->lchild,ch,++i); Create(bt->rchild,ch,++i); } } void TwoOrder(BiTree &bt) { if(bt) { cout<<bt->data; TwoOrder(bt->lchild); cout<<bt->data; TwoOrder(bt->rchild); } } int main() { char ch[1000]; while(cin>>ch&&ch[0]!='0') { int i=-1; BiTree bt; Create(bt,ch,++i); TwoOrder(bt); cout<<endl; } }
基于二叉链表的二叉树最大宽度的计算
描述
设二叉树中每个结点的元素均为一个字符,按先序遍历的顺序建立二叉链表,编写算法计算该二叉树的最大宽度(二叉树的最大宽度是指二叉树所有层中结点个数的最大值)。
输入
多组数据。每组数据一行,为二叉树的先序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。
输出
每组数据输出一行。为二叉树的最大宽度。
输入样例 1
abcd00e00f00ig00h00 abd00e00cf00g00 0输出样例 1
4 4#include<bits/stdc++.h> using namespace std; typedef struct BiNode { char data; BiNode *lchild,*rchild; }BiNode,*BiTree; void Create(BiTree &bt,char ch[],int &i) { if(ch[i]=='0') { bt=NULL; } else { bt=new BiNode; bt->data=ch[i]; Create(bt->lchild,ch,++i); Create(bt->rchild,ch,++i); } } void Width(BiTree bt) { BiTree q[100]; int level[100]; int rear=-1,fro=-1; BiTree p=bt; rear++; q[rear]=p; level[rear]=1; while(rear!=fro) { fro++; p=q[fro]; int k=level[fro]; if(p->lchild) { rear++; q[rear]=p->lchild; level[rear]=k+1; } if(p->rchild) { rear++; q[rear]=p->rchild; level[rear]=k+1; } } int k=1,i=0,maxx=0; while(i<=rear) { int n=0; while(i<=rear&&k==level[i]) { i++; n++; } k=level[i]; if(n>maxx) { maxx=n; } } cout<<maxx<<endl; } int main() { char ch[1000]; while(cin>>ch&&ch[0]!='0') { int i=-1; BiTree bt; Create(bt,ch,++i); Width(bt); } }
基于二叉链表的二叉树叶子结点到根结点的路径的求解
描述
设二叉树中每个结点的元素均为一个字符,按先序遍历的顺序建立二叉链表,编写算法求出每个叶子结点到根结点的路径。
输入
多组数据。每组数据一行,为二叉树的先序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。
输出
每组数据输出n行(n为叶子结点的个数),每行为一个叶子结点到根节点的路径(按照叶子结点从左到右的顺序)。
输入样例 1
abcd00e00f00ig00h00 abd00e00cf00g00 0输出样例 1
dcba ecba fba gia hia dba eba fca gca#include<bits/stdc++.h> using namespace std; typedef struct BiNode { char data; BiNode *lchild,*rchild; }BiNode,*BiTree; char path[100]; void Create(BiTree &bt,char ch[],int &i) { if(ch[i]=='0') { bt=NULL; } else { bt=new BiNode; bt->data=ch[i]; Create(bt->lchild,ch,++i); Create(bt->rchild,ch,++i); } } void DFS(BiTree bt,int n) { if(bt) { path[n]=bt->data; if(bt->lchild==NULL&&bt->rchild==NULL) { for(int i=n;i>=0;i--) { cout<<path[i]; } cout<<endl; } else { DFS(bt->lchild,n+1); DFS(bt->rchild,n+1); } } } int main() { char ch[1000]; while(cin>>ch&&ch[0]!='0') { int i=-1; BiTree bt; Create(bt,ch,++i); DFS(bt,0); } }
基于二叉链表的二叉树最长路径的求解
描述
设二叉树中每个结点的元素均为一个字符,按先序遍历的顺序建立二叉链表,编写算法求出该二叉树中第一条最长的路径。
输入
多组数据。每组数据一行,为二叉树的先序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。
输出
每组数据输出一行,第一行为二叉树的最长路径长度,第二行为此路径上从根到叶结点的各结点的值。
输入样例 1
abcd00e00f00ig00h00 abd00e00cf00g00 0输出样例 1
4 abcd 3 abd#include<bits/stdc++.h> using namespace std; typedef struct BiNode { char data; BiNode *lchild,*rchild; }BiNode,*BiTree; char path[100]; char a[100]; int len; void Create(BiTree &bt,char ch[],int &i) { if(ch[i]=='0') { bt=NULL; } else { bt=new BiNode; bt->data=ch[i]; Create(bt->lchild,ch,++i); Create(bt->rchild,ch,++i); } } void DFS(BiTree bt,int n) { if(bt) { a[n]=bt->data; if(bt->lchild==NULL&&bt->rchild==NULL) { if(n+1>len) { len=n+1; for(int i=0;i<=n;i++) { path[i]=a[i]; } } } else { DFS(bt->lchild,n+1); DFS(bt->rchild,n+1); } } } int main() { char ch[1000]; while(cin>>ch&&ch[0]!='0') { int i=-1; BiTree bt; Create(bt,ch,++i); len=0; DFS(bt,len); cout<<len<<endl; for(i=0;i<len;i++) cout<<path[i]; cout<<endl; } }
基于二叉链表的二叉树的遍历
描述
设二叉树中每个结点的元素均为一个字符,按先序遍历的顺序建立二叉链表,编写三个递归算法分别实现二叉树的先序、中序和后序遍历。
输入
多组数据。每组数据一行,为二叉树的前序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。
输出
每组数据输出三行,为二叉树的先序、中序和后序序列。
输入样例 1
abcd00e00f00ig00h00 abd00e00cf00g00 0输出样例 1
abcdefigh dcebfagih decfbghia abdecfg dbeafcg debfgca#include<bits/stdc++.h> using namespace std; typedef struct BiNode { char data; BiNode *lchild,*rchild; }BiNode,*BiTree; void Create(BiTree &bt,char ch[],int &i) { if(ch[i]=='0') { bt=NULL; } else { bt=new BiNode; bt->data=ch[i]; Create(bt->lchild,ch,++i); Create(bt->rchild,ch,++i); } } void PreOrder(BiTree bt) { if(bt) { cout<<bt->data; PreOrder(bt->lchild); PreOrder(bt->rchild); } } void InOrder(BiTree bt) { if(bt) { InOrder(bt->lchild); cout<<bt->data; InOrder(bt->rchild); } } void PostOrder(BiTree bt) { if(bt) { PostOrder(bt->lchild); PostOrder(bt->rchild); cout<<bt->data; } } int main() { char ch[1000]; while(cin>>ch&&ch[0]!='0') { int i=-1; BiTree bt; Create(bt,ch,++i); PreOrder(bt); cout<<endl; InOrder(bt); cout<<endl; PostOrder(bt); cout<<endl; } }
基于二叉链表的二叉树结点个数的统计
描述
设二叉树中每个结点的元素均为一个字符,按先序遍历的顺序建立二叉链表,编写三个递归算法对二叉树的结点(度为0、1、2)个数进行统计。
输入
多组数据。每组数据一行,为二叉树的前序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。
输出
每组数据输出一行,每行三个数分别为二叉树的度为0、1、2的结点个数。每两个数用空格分隔。
输入样例 1
abcd00e00f00ig00h00 abd00e00cf00g00 0输出样例 1
5 0 4 4 0 3#include<bits/stdc++.h> using namespace std; typedef struct BiNode { char data; BiNode *lchild,*rchild; }BiNode,*BiTree; void Create(BiTree &bt,char ch[],int &i) { if(ch[i]=='0') { bt=NULL; } else { bt=new BiNode; bt->data=ch[i]; Create(bt->lchild,ch,++i); Create(bt->rchild,ch,++i); } } int Zero(BiTree bt) { if(bt==NULL) { return 0; } else if(bt->lchild==NULL&&bt->rchild==NULL) { return Zero(bt->lchild)+Zero(bt->rchild)+1; } else { return Zero(bt->lchild)+Zero(bt->rchild); } } int One(BiTree bt) { if(bt==NULL) { return 0; } else if((bt->lchild==NULL&&bt->rchild!=NULL)||(bt->lchild!=NULL&&bt->rchild==NULL)) { return One(bt->lchild)+One(bt->rchild)+1; } else { return One(bt->lchild)+One(bt->rchild); } } int Two(BiTree bt) { if(bt==NULL) { return 0; } else if(bt->lchild!=NULL&&bt->rchild!=NULL) { return Two(bt->lchild)+Two(bt->rchild)+1; } else { return Two(bt->lchild)+Two(bt->rchild); } } int main() { char ch[1000]; while(cin>>ch&&ch[0]!='0') { int i=-1; BiTree bt; Create(bt,ch,++i); cout<<Zero(bt)<<" "; cout<<One(bt)<<" "; cout<<Two(bt)<<endl; } }
基于二叉链表的二叉树高度的计算
描述
设二叉树中每个结点的元素均为一个字符,按先序遍历的顺序建立二叉链表,,编写递归算法计算二叉树的高度。
输入
多组数据。每组数据一行,为二叉树的前序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。
输出
每组数据分别输出一行,为二叉树的高度。
输入样例 1
abcd00e00f00ig00h00 abd00e00cf00g00 0输出样例 1
4 3#include<bits/stdc++.h> using namespace std; typedef struct BiNode { char data; BiNode *lchild,*rchild; }BiNode,*BiTree; void Create(BiTree &bt,char ch[],int &i) { if(ch[i]=='0') { bt=NULL; } else { bt=new BiNode; bt->data=ch[i]; Create(bt->lchild,ch,++i); Create(bt->rchild,ch,++i); } } int Hight(BiTree bt) { if(bt==NULL) { return 0; } int lhight=Hight(bt->lchild); int rhight=Hight(bt->rchild); if(lhight>rhight) { return lhight+1; } else { return rhight+1; } } int main() { char ch[1000]; while(cin>>ch&&ch[0]!='0') { int i=-1; BiTree bt; Create(bt,ch,++i); cout<<Hight(bt)<<endl; } }
基于邻接矩阵的新顶点的增加
描述
给定一个无向图,在此无向图中增加一个新顶点。
输入
多组数据,每组m+2行。第一行有两个数字n和m,代表有n个顶点和m条边。顶点编号为1到n。第二行到第m+1行每行有两个数字h和k,代表边依附的两个顶点。第m+2行有一个数字f,代表新插入的顶点编号。当n和m都等于0时,输入结束。
输出
每组数据输出n+1行。为增加顶点后的邻接矩阵。每两个数字之间用空格隔开。
输入样例 1
3 2 1 2 2 3 4 2 1 1 2 4 0 0输出样例 1
0 1 2 3 4 1 0 1 0 0 2 1 0 1 0 3 0 1 0 0 4 0 0 0 0 0 1 2 4 1 0 1 0 2 1 0 0 4 0 0 0#include<bits/stdc++.h> using namespace std; typedef struct { int vex[100]; int edge[100][100]; int vexnum,edgenum; }Graph; void Create(Graph &g,int n,int m) { g.vexnum=n; g.edgenum=m; for(int i=0;i<=n;i++) { g.edge[0][i]=g.edge[i][0]=i; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { g.edge[i][j]=0; } } int x,y; for(int i=0;i<m;i++) { cin>>x>>y; g.edge[x][y]=1; g.edge[y][x]=1; } } void Insert(Graph &g,int x) { g.vexnum=g.vexnum+1; g.edge[0][g.vexnum]=x; g.edge[g.vexnum][0]=x; for(int i=1;i<=g.vexnum;i++) { g.edge[i][g.vexnum]=0; g.edge[g.vexnum][i]=0; } } void Print(Graph g) { for(int i=0;i<=g.vexnum;i++) { for(int j=0;j<g.vexnum;j++) { cout<<g.edge[i][j]<<" "; } cout<<g.edge[i][g.vexnum]<<endl; } } int main() { int n,m; while(cin>>n>>m&&n!=0&&m!=0) { Graph g; Create(g,n,m); int x; cin>>x; Insert(g,x); Print(g); } }
基于邻接矩阵的顶点的删除
描述
给定一个无向图,在此无向图中删除一个顶点。
输入
多组数据,每组m+2行。第一行有两个数字n和m,代表有n个顶点和m条边。顶点编号为1到n。第二行到第m+1行每行有两个数字h和k,代表边依附的两个顶点。第m+2行有一个数字f,代表删除的顶点编号。当n和m都等于0时,输入结束。
输出
每组数据输出n-1行。为删除顶点后的邻接矩阵。每两个数字之间用空格隔开。
输入样例 1
3 2 1 2 2 3 1 2 1 1 2 2 0 0输出样例 1
0 2 3 2 0 1 3 1 0 0 1 1 0#include<bits/stdc++.h> using namespace std; typedef struct { int vex[100]; int edge[100][100]; int vexnum,edgenum; }Graph; void Create(Graph &g,int n,int m) { g.vexnum=n; g.edgenum=m; for(int i=0;i<=n;i++) { g.edge[0][i]=g.edge[i][0]=i; } for(int i=1;i<=n;i++) { for(int j=1;j<n;j++) { g.edge[i][j]=0; } } int x,y; for(int i=0;i<m;i++) { cin>>x>>y; g.edge[x][y]=1; g.edge[y][x]=1; } } void Delete(Graph &g,int x) { for(int i=0;i<=g.vexnum;i++) { for(int j=x;j<g.vexnum;j++) { g.edge[i][j]=g.edge[i][j+1]; } } for(int i=x;i<g.vexnum;i++) { for(int j=0;j<=g.vexnum;j++) { g.edge[i][j]=g.edge[i+1][j]; } } g.vexnum=g.vexnum-1; } void Print(Graph g) { for(int i=0;i<=g.vexnum;i++) { for(int j=0;j<g.vexnum;j++) { cout<<g.edge[i][j]<<" "; } cout<<g.edge[i][g.vexnum]<<endl; } } int main() { int n,m; while(cin>>n>>m&&n!=0&&m!=0) { Graph g; Create(g,n,m); int x; cin>>x; Delete(g,x); Print(g); } }
基于邻接矩阵的新边的增加
描述
给定一个无向图,在此无向图中增加一条边。
输入
多组数据,每组m+2行。第一行有两个数字n和m,代表有n个顶点和m条边。顶点编号为1到n。第二行到第m+1行每行有两个数字h和k,代表边依附的两个顶点。第m+2行有两个数字f和g,代表增加的边所依附的两个顶点。当n和m都等于0时,输入结束。
输出
每组数据输出n行。为增加边后的邻接矩阵。每两个数字之间用空格隔开。
输入样例 1
3 2 1 2 2 3 3 1 3 1 1 2 1 3 0 0输出样例 1
0 1 2 3 1 0 1 1 2 1 0 1 3 1 1 0 0 1 2 3 1 0 1 1 2 1 0 0 3 1 0 0#include<bits/stdc++.h> using namespace std; typedef struct { int vex[100]; int edge[100][100]; int vexnum,edgenum; }Graph; void Create(Graph &g,int n,int m) { g.vexnum=n; g.edgenum=m; for(int i=0;i<=n;i++) { g.edge[0][i]=g.edge[i][0]=i; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { g.edge[i][j]=0; } } int x,y; for(int i=0;i<m;i++) { cin>>x>>y; g.edge[x][y]=1; g.edge[y][x]=1; } } void Insert(Graph &g,int x,int y) { g.edge[x][y]=1; g.edge[y][x]=1; } void Print(Graph g) { for(int i=0;i<=g.vexnum;i++) { for(int j=0;j<g.vexnum;j++) { cout<<g.edge[i][j]<<" "; } cout<<g.edge[i][g.vexnum]<<endl; } } int main() { int n,m; while(cin>>n>>m&&n!=0&&m!=0) { Graph g; Create(g,n,m); int x,y; cin>>x>>y; Insert(g,x,y); Print(g); } }
基于邻接矩阵的边的删除
描述
给定一个无向图,在此无向图中增加一条边。
输入
多组数据,每组m+2行。第一行有两个数字n和m,代表有n个顶点和m条边。顶点编号为1到n。第二行到第m+1行每行有两个数字h和k,代表边依附的两个顶点。第m+2行有两个数字f和g,代表删除的边所依附的两个顶点。当n和m都等于0时,输入结束。
输出
每组数据输出n行。为删除边后的邻接矩阵。每两个数字之间用空格隔开。
输入样例 1
3 2 1 2 2 3 3 2 3 1 1 2 1 2 0 0输出样例 1
0 1 2 3 1 0 1 0 2 1 0 0 3 0 0 0 0 1 2 3 1 0 0 0 2 0 0 0 3 0 0 0#include<bits/stdc++.h> using namespace std; typedef struct { int vex[100]; int edge[100][100]; int vexnum,edgenum; }Graph; void Create(Graph &g,int n,int m) { g.vexnum=n; g.edgenum=m; for(int i=0;i<=n;i++) { g.edge[0][i]=g.edge[i][0]=i; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { g.edge[i][j]=0; } } int x,y; for(int i=0;i<m;i++) { cin>>x>>y; g.edge[x][y]=1; g.edge[y][x]=1; } } void Delete(Graph &g,int x,int y) { g.edge[x][y]=0; g.edge[y][x]=0; } void Print(Graph g) { for(int i=0;i<=g.vexnum;i++) { for(int j=0;j<g.vexnum;j++) { cout<<g.edge[i][j]<<" "; } cout<<g.edge[i][g.vexnum]<<endl; } } int main() { int n,m; while(cin>>n>>m&&n!=0&&m!=0) { Graph g; Create(g,n,m); int x,y; cin>>x>>y; Delete(g,x,y); Print(g); } }