二叉樹的一些題
阿新 • • 發佈:2018-12-29
A.求二叉樹的層次遍歷
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct tree
{
char data;
struct tree*left,*right;
}*link[54];
char str1[54],str2[54];
struct tree*getbuild(int len,char *str1,char *str2)
{
if(len==0)return NULL;//後序遍歷和還原二叉樹,在還原的過程中就後序遍歷了
struct tree*root;
root=(struct tree*)malloc(sizeof(struct tree));
root->data=str1[0];
int i;
for(i=0;i<len;i++)
{
if(str2[i]==root->data)
break;//找結點,知道後就退出
}
root->left=getbuild(i,str1+1,str2);//左子樹的長度,左子樹在str1中的位置,左子樹在str2中的位置
root->right=getbuild(len-i-1,str1+i+1,str2+i+1 );
return root;
}
void ceng(struct tree*root)//層序遍歷,入隊,先讓自己的左右孩子入隊後自己再出隊
{
if(root)
{
int i=0,j=0;
link[j++]=root;
while(i<j)//當沒有可以入隊的元素的時候,就出隊
{
if(link[i])
{
link[j++]=link[i]->left;
link[j++]=link[i]->right;
printf ("%c",link[i]->data);
}
i++;
}
}
}
int main()
{
int t,len;
scanf("%d",&t);
while(t--)
{
struct tree*root;
scanf("%s%s",str1,str2);
len=strlen(str2);
root=getbuild(len,str1,str2);
ceng(root);
printf("\n");
}
return 0;
}
B.資料結構實驗之求二叉樹後序遍歷和層次遍歷
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct tree
{
char data;
struct tree*left,*right;
}*link[54];
char str1[54],str2[54];
struct tree*getbuild(int len,char *str1,char *str2)
{
if(len==0)return NULL;//後序遍歷和還原二叉樹,在還原的過程中就後序遍歷了
struct tree*root;
root=(struct tree*)malloc(sizeof(struct tree));
root->data=str1[0];
int i;
for(i=0;i<len;i++)
{
if(str2[i]==root->data)
break;//找結點,知道後就退出
}
root->left=getbuild(i,str1+1,str2);//左子樹的長度,左子樹在str1中的位置,左子樹在str2中的位置
root->right=getbuild(len-i-1,str1+i+1,str2+i+1);
printf("%c",root->data);
return root;
}
void ceng(struct tree*root)//層序遍歷,入隊,先讓自己的左右孩子入隊後自己再出隊
{
if(root)
{
int i=0,j=0;
link[j++]=root;
while(i<j)//當沒有可以入隊的元素的時候,就出隊
{
if(link[i])
{
link[j++]=link[i]->left;
link[j++]=link[i]->right;
printf("%c",link[i]->data);
}
i++;
}
}
}
int main()
{
int t,len;
scanf("%d",&t);
while(t--)
{
struct tree*root;
scanf("%s%s",str1,str2);
len=strlen(str2);
root=getbuild(len,str1,str2);
printf("\n");
ceng(root);
printf("\n");
}
return 0;
}
C.求二叉樹的先序遍歷
#include <stdio.h>
#include <stdlib.h>
struct tree
{
char data;
struct tree *left,*right;
}*root;
char str1[54],str2[54];
struct tree*getbuild(int len,char *str1,char *str2)
{
if(len==0)
{
return NULL;
}
struct tree *root;
root=(struct tree *)malloc(sizeof(struct tree));
root->data=str2[len-1];
int i;
for(i=0; i<len; i++)
{
if(str1[i]==root->data)
{
break;
}
}
printf("%c",root->data);
root->left=getbuild(i,str1,str2);
root->right=getbuild(len-1-i,str1+i+1,str2+i);
return root;
};
int main()
{
int t,len;
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%s %s",str1,str2);
len=strlen(str2);
root=(struct tree *)malloc(sizeof(struct tree));
getbuild(len,str1,str2);
printf("\n");
}
}
return 0;
}
D.資料結構實驗之二叉樹四:(先序中序)還原二叉樹
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct tree
{
char data;
struct tree*left,*right;
}*root;
char str1[51],str2[52];
struct tree*getbuild(int len,char *str1,char *str2)
{
if(len==0)return NULL;
struct tree*root;
root=(struct tree*)malloc(sizeof(struct tree));
root->data=str1[0];
int i;
for(i=0;i<len;i++)
{
if(str2[i]==root->data)
break;
}
root->left=getbuild(i,str1+1,str2);
root->right=getbuild(len-i-1,str1+i+1,str2+i+1);
return root;
}//利用先序和中序來實現後序二叉樹的還原
int depth(struct tree*root)
{
int x,y;
if(root==NULL)return 0;
else
{
x=depth(root->left);
y=depth(root->right);
return (x>y)?(x+1):(y+1);
}//執行到最底層,從最底層開始往上加
}
int main()
{
int n,len,dep;
while(~scanf("%d",&n))
{
scanf("%s%s",str1,str2);
len=strlen(str2);
root=getbuild(len,str1,str2);
dep=depth(root);
printf("%d\n",dep);
}
return 0;
}
E.資料結構實驗之二叉樹三:統計葉子數
#include<stdio.h>
#include<stdlib.h>
struct tree
{
char data;
struct tree*left,*right;
}*root;
char s[52];
int ans,count;
struct tree*front_create()
{
struct tree*root;//必須要有
char c=s[ans++];
if(c==',')return NULL;
else
{
root=(struct tree*)malloc(sizeof(struct tree));
root->data=c;
root->left=front_create();
root->right=front_create();
}
return root;//每次都是創造一個新的root,然後對root進行賦值,一層層的把root賦給左孩子,右孩紙,,最後賦值給總的root,一棵樹就形成了
}
int searchleaf(struct tree*root)
{
if(root)
{
if(root->left==NULL&&root->right==NULL)
count++;
searchleaf(root->left);
searchleaf(root->right);
}
return count;
}
int main()
{
while(~scanf("%s",s))
{
ans=0;
count=0;
root=front_create();
printf("%d\n",searchleaf(root));
}
return 0;
}
F.資料結構上機測試4.1:二叉樹的遍歷與應用1
#include<stdio.h>
#include<stdlib.h>
struct tree
{
char data;
struct tree*left,*right;
}*root;
char s[52];
int ans,count;
struct tree*front_create()
{
struct tree*root;//必須要有
char c=s[ans++];
if(c==',')return NULL;
else
{
root=(struct tree*)malloc(sizeof(struct tree));
root->data=c;
root->left=front_create();
root->right=front_create();
}
return root;//每次都是創造一個新的root,然後對root進行賦值,一層層的把root賦給左孩子,右孩紙,,最後賦值給總的root,一棵樹就形成了
}
int searchleaf(struct tree*root)
{
if(root)
{
if(root->left==NULL&&root->right==NULL)
count++;
searchleaf(root->left);
searchleaf(root->right);
}
return count;
}
int main()
{
while(~scanf("%s",s))
{
ans=0;
count=0;
root=front_create();
printf("%d\n",searchleaf(root));
}
return 0;
}
G.資料結構實驗之二叉樹五:層序遍歷
#include<stdio.h>
#include<stdlib.h>
struct tree
{
char data;
struct tree*left,*right;
}*root,*link[54];
char s[54];
int ans;
struct tree*front_create()
{
struct tree*root;
char c=s[ans++];
if(c==',')root=NULL;
else
{
root=(struct tree*)malloc(sizeof(struct tree));
root->data=c;
root->left=front_create();
root->right=front_create();
}
return root;
}//先序建立一個樹
void ceng(struct tree*root)
{
if(root)
{
int i=0,j=0;
link[j++]=root;//先把頭放上
while(i<j)
{
if(link[i])
{
link[j++]=link[i]->left;
link[j++]=link[i]->right;
printf("%c",link[i]->data);
}
i++;
}
}
}//利用佇列層序輸出
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
ans=0;
root=front_create();
ceng(root);
printf("\n");
}
return 0;
}
H.資料結構實驗之二叉樹的建立與遍歷
#include<stdio.h>
#include<stdlib.h>
struct tree
{
char data;
struct tree*right,*left;
}*link[54];
char s[54];
int ans,flag;
struct tree*front_create()
{
struct tree*root;
char c=s[ans++];
if(c==',')root=NULL;//遞迴的邊界,輸入的數的最後肯定是','
else
{
root=(struct tree*)malloc(sizeof(struct tree));
root->data=c;
root->left=front_create();
root->right=front_create();
}
return root;
}
int depth(struct tree*root)//求深度
{
int x,y;
if(!root)return 0;
else
{
x=depth(root->left);
y=depth(root->right);
return x>y?x+1:y+1;
}
}
void mid(struct tree*root)//中序遍歷
{
if(root)
{
mid(root->left);
printf("%c",root->data);
mid(root->right);
}
}
void after(struct tree*root)//後序遍歷
{
if(root)
{
after(root->left);
after(root->right);
printf("%c",root->data);
}
}
int searchleaf(struct tree*root)//尋找葉子結點,在層序遍歷的基礎上加點操作即可
{
if(root)
{
int i=0,j=0;
link[j++]=root;
while(i<j)
{
if(link[i])
{
if(link[i]->left==NULL&&link[i]->right==NULL)
flag++;
else
{
link[j++]=link[i]->left;
link[j++]=link[i]->right;
}
}
i++;
}
}
return flag;
}
int main()
{
scanf("%s",s);
flag=0,ans=0;
struct tree*root;
root=front_create();
mid(root);
printf("\n");
after(root);
printf("\n");
printf("%d\n",searchleaf(root));
printf("%d\n",depth(root));
return 0;
}
I.資料結構實驗之二叉樹一:樹的同構
#include<stdio.h>
#include<stdlib.h>
#define max 21
int n1,n2;
struct node
{
char key;
int left,right;
}tree1[max],tree2[max];
void buildtree(struct node tree[],int n)
{
int i;
char s[2];
for(i=0;i<n;i++)
{
scanf("%s",s);
tree[i].key=s[0];
scanf("%s",s);
if(s[0]=='-')
{
tree[i].left=11;
}
else tree[i].left=s[0]-'0';
scanf("%s",s);
if(s[0]=='-')
{
tree[i].right=11;
}
else tree[i].right=s[0]-'0';
}
}
int checkc(int i,int j)
{
if(tree1[tree1[i].left].key==tree2[tree2[j].left].key&&tree1[tree1[i].right].key==tree2[tree2[j].right].key)
return 1;
if(tree1[tree1[i].left].key==tree2[tree2[j].right].key&&tree1[tree1[i].right].key==tree2[tree2[j].left].key)
return 1;
return 0;
}
int check(struct node tree1[],int n1,struct node tree2[],int n2)
{
int i,j;
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(tree1[i].key==tree2[j].key)
{
if(checkc(i,j))break;//左右孩子相同或者交換之後的左右對應相同,就行了,看下一個結點
else return 0;//一旦有一個點不相同,就不是同構
}
}
if(j==n2)return 0;//如果找遍了都沒找到,那就是沒有了
}
return 1;//整棵樹都滿足就是同構了
}
int main()
{
while(~scanf("%d",&n1))
{
buildtree(tree1,n1);
scanf("%d",&n2);
buildtree(tree2,n2);
if(n1!=n2)printf("No\n");
else if(check(tree1,n1,tree2,n2))
printf("Yes\n");
else printf("No\n");
}
return 0;
}
J.資料結構實驗之二叉樹八:(中序後序)求二叉樹的深度
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct tree
{
char data;
struct tree *left,*right;
}*root;
char str1[54],str2[54];
struct tree*getbuild(int len,char *str1,char *str2)
{
if(len==0)
{
return NULL;
}
struct tree *root;
root=(struct tree *)malloc(sizeof(struct tree));
root->data=str2[len-1];
int i;
for(i=0; i<len; i++)
{
if(str1[i]==root->data)
{
break;
}
}
root->left=getbuild(i,str1,str2);
root->right=getbuild(len-1-i,str1+i+1,str2+i);
return root;
};
int depth(struct tree *root)
{
int x,y;
if(root==NULL)
{
return 0;
}
else
{
x=depth(root->left);
y=depth(root->right);
return (x>y)?(x+1):(y+1);
}
}
int main()
{
int t,len,m;
while(~scanf("%d",&t))
{
while(t--)
{
struct tree *root;
scanf("%s %s",str1,str2);
len=strlen(str2);
root=getbuild(len,str1,str2);
m=depth(root);
printf("%d\n",m);
}
}
return 0;
}
K.資料結構實驗之二叉樹七:葉子問題
#include<stdio.h>
#include<stdlib.h>
struct tree
{
char data;
struct tree*left,*right;
}*link[54],*root;
char s[54];
int ans;
struct tree*front_create()
{
struct tree*root;
char c=s[ans++];
if(c==',')root=NULL;
else
{
root=(struct tree*)malloc(sizeof(struct tree));
root->data=c;
root->left=front_create();
root->right=front_create();
}
return root;
}
void ceng(struct tree*root)
{
if(root)
{
int i=0,j=0;
link[j++]=root;
while(i<j)
{
if(link[i])
{
if(link[i]->left==NULL&&link[i]->right==NULL)
printf("%c",link[i]->data);
else
{
link[j++]=link[i]->left;
link[j++]=link[i]->right;
}
}
i++;
}
}
}
int main()
{
while(~scanf("%s",s))
{
ans=0;
struct tree*root;
root=front_create();
ceng(root);
printf("\n");
}
return 0;
}
L.資料結構實驗之二叉樹二:遍歷二叉樹
#include<stdio.h>
#include<stdlib.h>
struct tree
{
char data;
struct tree*right,*left;
}*link[54];
char s[54];
int ans,flag;
struct tree*front_create()
{
struct tree*root;
char c=s[ans++];
if(c==',')root=NULL;//遞迴的邊界,輸入的數的最後肯定是','
else
{
root=(struct tree*)malloc(sizeof(struct tree));
root->data=c;
root->left=front_create();
root->right=front_create();
}
return root;
}
void mid(struct tree*root)//中序遍歷
{
if(root)
{
mid(root->left);
printf("%c",root->data);
mid(root->right);
}
}
void after(struct tree*root)//後序遍歷
{
if(root)
{
after(root->left);
after(root->right);
printf("%c",root->data);
}
}
int main()
{
while(~scanf("%s",s))
{
ans=0;
struct tree*root;
root=front_create();
mid(root);
printf("\n");
after(root);
printf("\n");
}
return 0;
}