二叉搜索树的习题 pta

时间:2022-02-07 12:26:24

今天鸡血打完了,直接贴代码就不解释了

根据后序和中序遍历输出先序遍历

点击打开链接

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#define MAXINT 32767
using namespace std;
struct BtreeNode{
int data;
struct BtreeNode* left;
struct BtreeNode* right;
};
typedef struct BtreeNode* node;
typedef struct BtreeNode btree;
int hou[35],in[35],n;
node creat(int *hou,int *in,int n){
int root,i;
if(n==-1)return NULL;
root=hou[n];
node t;
t=new btree;
t->data=root;
for(i=0;i<=n;i++)
if(in[i]==root)
break;
t->left=creat(hou,in,i-1);
t->right=creat(hou+i,in+i+1,n-i-1);
return t;
}
void qianxu(node tt){
if(tt){
cout<<" "<<tt->data;
qianxu(tt->left);
qianxu(tt->right);
}
}
int main()
{
node tt;
int i;
cin>>n;
for(i=0;i<n;i++)
cin>>hou[i];
for(i=0;i<n;i++)
cin>>in[i];
tt=creat(hou,in,n-1);
cout<<"Preorder:";
qianxu(tt);
cout<<endl;
return 0;
}
是否同一棵二叉搜索树
点击打开链接

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
struct BtreeNode{
int data;
struct BtreeNode *left;
struct BtreeNode *right;
};
typedef struct BtreeNode* node;
typedef struct BtreeNode btree;
node creat(node t,int n){
int m,i;
node p,q,s;
for(i=0;i<n;i++){
cin>>m;
s=new btree;
s->data=m; s->left=NULL; s->right=NULL;
if(t==NULL) t=s;
else{
p=t;
while(p!=NULL){
q=p;
if(m<p->data) p=p->left;
else p=p->right;
}
if(m<q->data) q->left=s;
else q=q->right=s;
}
}


return t;
}
int equeal(node t1,node t2){
if(t1==NULL&&t2==NULL) return 1;
if(t1!=NULL&&t2==NULL) return 0;
if(t1==NULL&&t2!=NULL) return 0;
if(t1!=NULL&&t2!=NULL){
if(t1->data!=t2->data) return 0;
if(equeal(t1->left,t2->left)==0) return 0;
if(equeal(t1->right,t2->right)==0) return 0;
}
return 1;
}
int main()
{
int n,m,i,flag;
node t1,t2;
while(cin>>n&&n){
cin>>m;
t1=NULL;
t1=creat(t1,n);
for(i=0;i<m;i++){
t2=NULL; t2=creat(t2,n);
flag=equeal(t1,t2);
if(flag==1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
return 0;
}
还原二叉树

点击打开链接

/***
9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
***/
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#define N 55
using namespace std;
struct BtreeNode{
char data;
struct BtreeNode* left;
struct BtreeNode* right;
};
typedef struct BtreeNode* node;
typedef struct BtreeNode btree;
char qian[N],in[N];
int n;
node creat(char qian[],char in[],int n){
int i;
char root;
if(n==0) return NULL;
root=qian[0]; ///root=qian[1];
node t;
t=new btree;
t->data=root;
for(i=0;i<n;i++) ///for(i=1;i<=n;i++)
if(in[i]==root)
break;
t->left=creat(qian+1,in,i); ///t->left=creat(qian,in,i-1)
t->right=creat(qian+i+1,in+i+1,n-i-1); ///t->right=creat(qian+i,in+i,n-i);
return t;
}
/*
void houxu(node tt){
if(tt){
houxu(tt->left);
houxu(tt->right);
printf("%c ",tt->data);
}
}
int Depth(node &tt){
int dep=0,depl=0,depr=0;
if(!tt) dep=0;
else{
depl=Depth(tt->left);
depr=Depth(tt->right);
dep=1+(depl>depr?depl:depr);
}
return dep;
}
*/
int Depth(node &tt){
int dep=0,del=0,der=0;
if(!tt)
dep=0;
else{
del=Depth(tt->left);
der=Depth(tt->right);
dep=1+(del>der?del:der);
}
return dep;
}
int main()
{
int dep;
scanf("%d%*c",&n);
node tt=NULL;
///从0开始 从1开始
scanf("%s",qian);
scanf("%s",in);
tt=creat(qian,in,n); ///tt=creat(qian,in,n);
///houxu(tt);
///printf("\n");
dep=Depth(tt);
cout<<dep<<endl;
return 0;
}


树种统计

点击打开链接

用了map,不了解先学map

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <iomanip>
#include <map>
using namespace std;
int main()
{
int n,i;
char s[150];
map<string,int>M;
cin>>n;
getchar();
M.clear();
for(i=0;i<n;i++){
gets(s);
M[s]++;
}
map<string,int>::iterator it;
for(it=M.begin();it!=M.end();it++)
cout<<it->first<<" "<<fixed<<setprecision(4)<<it->second*100.0/n<<"%"<<endl;
return 0;
}
笛卡尔树

点击打开链接

#include <iostream>
#include <queue>
#include <string.h>
#define N 1005
#define INF 0x3f3f3f3f
using namespace std;
int a[N][4];
int p1[N],p2[N],j=1,l=1;
struct tree{
int x1,x2;
struct tree *h;
struct tree *left;
struct tree *right;
}u,v;
queue<tree>q;
struct tree* newtree(int n){
if(n==-1)
return NULL;
tree *t=new tree;
t->x1=a[n][0]; t->x2=a[n][1]; t->right=NULL; t->left=NULL;
t->left=newtree(a[n][2]);
t->right=newtree(a[n][3]);
return t;
}
void min1(struct tree *t){
if(t==NULL) return ;
min1(t->left);
p1[j]=t->x1;
++j;
min1(t->right);
}
int main(){
int n;
cin>>n;
int k=0;
memset(p2,INF,sizeof(p2));
for(int i=0;i<n;i++){
k+=i;
cin>>a[i][0]>>a[i][1]>>a[i][2]>>a[i][3];
if(a[i][2]>=0)
k=k-a[i][2];
if(a[i][3]>=0)
k=k-a[i][3];

}
struct tree *t,*p;
t=newtree(k);
min1(t); u.h=t; u.x1=1; q.push(u); p=t;
while(!q.empty()){
u=q.front(); q.pop();
p=u.h;
p2[u.x1]=p->x2;
if(p->left!=NULL) {
v.h=p->left;
v.x1=u.x1*2;
q.push(v);
}
if(p->right!=NULL) {
v.h=p->right;
v.x1=u.x1*2+1;
q.push(v);
}
}
for(int i=1;i<=n;i++){
if(i<=n-1&&p1[i]>p1[i+1]){
cout<<"NO"<<endl;
return 0;
}
if(p2[i]>p2[2*i]||p2[i]>p2[2*i+1]){
cout<<"NO"<<endl;
return 0;
}
}
cout<<"YES"<<endl;
}
顺序存储的二叉树的最近的公共祖先问题

点击打开链接

#include <iostream>
#define N 1010
using namespace std;

int main()
{
int n,i,j,k,x;
int a[N];
cin>>n;
for(k=1;k<=n;k++)
cin>>a[k];

cin>>i>>j;
while(i!=j){
if(a[i]==0){
x=i;
break;
}
if(a[j]==0){
x=j;
break;
}
if(i>j)
i=i/2;
else
j=j/2;

}
if(a[i]==0||a[j]==0)
cout<<"ERROR: T["<<x<<"] is NULL"<<endl;
else
cout << i <<" "<< a[j] << endl;
return 0;
}


List Leaves

点击打开链接

#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[20],l[20],r[20];
struct BtreeNode{
int parent;
struct BtreeNode* left;struct BtreeNode* right;
}u,v;
typedef struct BtreeNode* node;
typedef struct BtreeNode btree;
node creat(int i){
node s;
if(i==-1)
return NULL;
s=new btree;
s->parent=i;
s->left=creat(l[i]);
s->right=creat(r[i]);
return s;
}
int uuu=0;
void bfs(node t){
queue<struct BtreeNode>q;
node p,w;
u.parent=t->parent;
u.left=t->left;
u.right=t->right;
q.push(u);
while(!q.empty()){
u=q.front();q.pop();
p=u.left;
w=u.right;
if(p==NULL&&w==NULL){
uuu++;
if(uuu==1)
cout<<u.parent;
else
cout<<" "<<u.parent;
}
if(p!=NULL){
v.parent=p->parent;
v.left=p->left;
v.right=p->right;
q.push(v);
}

if(w!=NULL){
v.parent=w->parent;
v.left=w->left;
v.right=w->right;
q.push(v);
}
}

}
int main()
{
int n,i;
char c1,c2;
cin>>n;
getchar();
for(i=0;i<n;i++){
scanf("%c %c%*c",&c1,&c2);
if(c1=='-') l[i]=-1;
else{
l[i]=c1-'0';
a[l[i]]=1;
}
if(c2=='-') r[i]=-1;
else{
r[i]=c2-'0';
a[r[i]]=1;
}
}
for(i=0;i<n;i++)
if(a[i]!=1)
break;

node t=NULL;
t=creat(i);
bfs(t);
return 0;
}