当前位置:文档之家› 数据结构第二次单元测试

数据结构第二次单元测试

0980 输出利用先序遍历创建的二叉树的层次遍历序列(中)#include <iostream>#include <malloc.h>using namespace std;typedef struct node{char data;node *leftchild;node *rightchild;}Node;void Init(Node *&L){L = (Node *)malloc(sizeof(Node));}void PreCreate(Node *&L){char ch;cin>>ch;if(ch != '#'){Init(L);L->data = ch;L->leftchild = NULL;L->rightchild = NULL;PreCreate(L->leftchild);PreCreate(L->rightchild);}else{L = NULL;}}void levelT(Node *L){Node *p;Node *q[100];int fornt, rear;fornt = -1;rear = -1;if(L != NULL){rear = (rear+1)%100;q[rear] = L;while(fornt != rear){fornt = (fornt+1)%100;p = q[fornt];cout<<p->data;if(p->leftchild != NULL){rear = (rear+1)%100;q[rear] = p->leftchild;}if(p->rightchild != NULL){rear = (rear+1)%100;q[rear] = p->rightchild;}}}}int main(){Node *p;PreCreate(p);levelT(p);return 0;}0981统计利用二叉树存储的森林中树的棵树(易)#include <iostream>#include <malloc.h>using namespace std;typedef struct node{char data;node *leftchild;node *rightchild;}Node;void InitTree(Node *&L){L = (Node *)malloc(sizeof(Node));}void preCreate(Node *&L){InitTree(L);char ch;cin>>ch;if(ch != '#'){L->data = ch;L->leftchild = NULL;L->rightchild = NULL;preCreate(L->leftchild);preCreate(L->rightchild);}elseL = NULL;}int Ftree(Node *&L){if(L == NULL){return 0;}else if(L->rightchild == NULL){return 1;}else{return Ftree(L->rightchild)+1;}}int main(){Node *p;preCreate(p);cout<<Ftree(p);return 0;}0982 输出利用二叉树存储的普通树的度(中)#include <iostream>#include <malloc.h>using namespace std;typedef struct node{char data;node *leftchild;node *rightchild;}Node;void Init(Node *&L){L = (Node *)malloc(sizeof(Node));}void preCreate(Node *&L){char ch;cin>>ch;if(ch != '#'){Init(L);L->data = ch;L->leftchild = NULL;L->rightchild = NULL;preCreate(L->leftchild);preCreate(L->rightchild);}else{L = NULL;}}int findG(Node *&L){int max = 0, a, b;if(L == NULL){return max;}else{a = (findG(L->rightchild)+1);b = findG(L->leftchild);(a >= b)? max = a : max = b;}return max;}int main(){Node *p;preCreate(p);if(p->rightchild != NULL){cout<<"ERROR";}else{cout<<findG(p);}return 0;}0983利用二叉树中序及后序遍历确定该二叉树的先序序列(难)#include <iostream>#include <malloc.h>#include <string.h>using namespace std;typedef struct node{char data;node *leftchild;node *rightchild;}Node;void InitTree(Node *&L){L = (Node *)malloc(sizeof(Node));L->leftchild = NULL;L->rightchild = NULL;}void preCreate(Node *&L, char *pos, char *in, int n) {char *p;int k;if(n <= 0){InitTree(L);L = NULL;return;}InitTree(L);L->data = *(pos+n-1);for(p = in; p < in +n; p++){if(*p == *(pos+n-1))break;}k = p -in;preCreate(L->leftchild, pos, in, k);preCreate(L->rightchild, pos+k, p+1, n-k-1);}void post(Node *&L){if(L != NULL){cout<<L->data;post(L->leftchild);post(L->rightchild);}}int main(){Node *p;int n;char pos[100], in[100];scanf("%s",in);scanf("%s",pos);n = strlen(in);preCreate(p, pos, in, n);post(p);return 0;}0984利用二叉树中序及先序遍历确定该二叉树的后序序列(难)#include <iostream>#include <malloc.h>#include <string.h>using namespace std;typedef struct node{char data;node *leftchild;node *rightchild;}Node;void InitTree(Node *&L){L = (Node *)malloc(sizeof(Node));L->leftchild = NULL;L->rightchild = NULL;}void preCreate(Node *&L, char *pre, char *in, int n){char *p;int k;if(n <= 0){InitTree(L);L = NULL;return;}InitTree(L);L->data = *pre;for(p = in; p < in +n; p++){if(*p == *pre)break;}k = p -in;preCreate(L->leftchild, pre+1, in, k);preCreate(L->rightchild, pre+k+1, p+1, n-k-1);}void post(Node *&L){if(L != NULL){post(L->leftchild);post(L->rightchild);cout<<L->data;}}int main(){Node *p;int n;char pre[100], in[100];scanf("%s",in);scanf("%s",pre);n = strlen(in);preCreate(p, pre, in, n);post(p);return 0;}0986 哈夫曼译码0987输出用先序遍历创建的二叉树是否为完全二叉树的判定结果(难)1053输出利用先序遍历创建的二叉树中的指定结点的度(易)1052 输出利用先序遍历创建的二叉树中的指定结点的双亲结点(中)#include<stdlib.h>#include<malloc.h>#include<iostream>using namespace std;typedef struct Node{char data;Node*lc,*rc;}node;char x;void precreat(node*&root){char c;cin>>c;if(c=='#'){root=NULL;}else{root=(node*)malloc(sizeof(node));root->data=c;root->lc=NULL;root->rc=NULL;precreat(root->lc);precreat(root->rc);}}void PreOrder(node*root){if(root!=NULL){if(root->lc!=NULL&&root->lc->data==x){cout<<root->data;exit(0);}if(root->rc!=NULL&&root->rc->data==x){cout<<root->data;exit(0);}PreOrder(root->lc);PreOrder(root->rc);}else{cout<<"#";exit(0);}}int main(){node*root;precreat(root);cin>>x;PreOrder(root);return 0;}1051 输出利用先序遍历创建的二叉树中的指定结点的孩子结点(易)#include<iostream>#include<malloc.h>using namespace std;typedef struct node{char data;node *left,*right;}treenode;void pre(treenode *&root){char c;cin>>c;if(c=='#') root=NULL;else{root=(treenode *)malloc(sizeof(treenode));root->data=c;root->left=NULL;root->right=NULL;pre(root->left);pre(root->right);}}void inorder(treenode *&root,char c){if(root==NULL)return;else{if(root->data==c){if(root->left==NULL)cout<<"L:#,";elsecout<<"L:"<<root->left->data<<",";if(root->right==NULL)cout<<"R:#";elsecout<<"R:"<<root->right->data;return;}inorder(root->left,c);inorder(root->right,c);}}int main(){char c;treenode *root;pre(root);cin>>c;inorder(root,c);return 0;}1105交叉二叉树的孩子结点(易)1055邻接矩阵到邻接表(易)1056邻接表到邻接矩阵(易)1057有向图的出度计算(易)1058无向图的出度计算(易)1059有向图的最大出度计算(易1060无向图的最大出度计算(易)1061有向图的k出度计算(中)1062有向图的边存在判断(易)#include <iostream>#include <stdio.h>using namespace std;int main(){int a[50][50], i, j, n, row, col;cin>>n;cin>>row;cin>>col;for(i = 0; i < n; i++){for(j = 0; j < n; j++){cin>>a[i][j];}}if(a[row][col]==1)cout<<"yes";elsecout<<"no";return 0;}1063带权有向图计算(易) 1064带权无向图存储判定(中) #include <iostream>#include <stdio.h>using namespace std;void Jug(int a[][50], int n){int i, j;for(i = 0; i < n; i++){if(a[i][i] != 0){cout<<"no";return;}for(j = 0; j < n; j++){if(a[i][j] != a[j][i]){cout<<"no";return;}}}cout<<"yes";}int main(){int a[50][50], i, j, n;cin>>n;for(i = 0; i < n; i++){for(j = 0; j < n; j++){cin>>a[i][j];}}Jug(a,n);return 0;}1065无向图的连通分量计算(中)1067 有向图的邻接表存储强连通判断(中)#include <iostream>#include <string.h>using namespace std;int str[1000][1000],n;int main (void){int i,a,b,n,m;cin>>m>>n;for (i=0;i<n;i++){cin>>a>>b;str[a][b]=1;}int mark=1;for (i=0;i<m-1;i++){if (str[i][i+1]!=1)mark=0;}if (mark)cout<<"yes";elsecout<<"no";return 0;}1068图的按录入顺序深度优先搜索(难)#include<iostream>using namespace std;int visited[100]={0};typedef struct{int n;char data[500];int edge[500][500];}Mgraph;void create(Mgraph &G,int n){int i,j;for(i=0;i<n;i++)cin>>G.data[i];for(i=0;i<n;i++)for(j=0;j<n;j++)cin>>G.edge[i][j];G.n=n;}void dfs(Mgraph &g,char c){cout<<c;int i,j;for(i=0;i<g.n;i++)if(g.data[i]==c){j=i;visited[j]=1;break;}for(i=0;i<(g.n);i++){if(visited[i]==0&&g.edge[j][i]!=0)dfs(g,g.data[i]);}}int main(){int n;char e;Mgraph my;cin>>n;create(my,n);cin>>e;dfs(my,e);return 0;}1069图的按录入顺序广度优先搜索(难)#include<stdio.h>#include<iostream>#include<stdlib.h>using namespace std;typedef struct{char vexs[20];int edges[20][20];int n,e;}MGraph;int CreatMGraph(MGraph *G){int i,j;char a,b[50];int k;cin>>G->n;cin>>b;for(i=0;i<G->n;i++){G->vexs[i]=b[i];}for(i=0;i<G->n;i++){for(j=0;j<G->n;j++){cin>>G->edges[i][j];}}cin>>a;for(i=0;i<G->n;i++){if(G->vexs[i]==a){k=i;}}return k;}void BFS(MGraph *G,int k){int i,j,f=-1,r=-1;int visited[20];int queue[20];for(i=0;i<G->n;i++)visited[i]=0;cout<<G->vexs[k];visited[k]=1;r=(r+1)%20;queue[r]=k;while(f!=r){f=(f+1)%20;i=queue[f];for(j=0;j<G->n;j++)if(G->edges[i][j]!=0 && visited[j]==0){cout<<G->vexs[j];visited[j]=1;r=(r+1)%20;queue[r]=j;}}}int main(){int q;MGraph * G;G=(MGraph *)malloc(sizeof(MGraph));q=CreatMGraph(G);BFS(G,q);return 0;}1070邻接矩阵存储简单路径(难)#include <iostream>#include <stdio.h>using namespace std;int path[100], visited[100] = {0}, top = -1;void DFS(int a[][100], int v1, int v2, int n) {visited[v1] = 1;path[++top] = v1;int i , j;for(j = 0; j < n; j++){if(a[v1][j] != 0 && visited[j] == 0) {if(j == v2){for(i = 0; i <= top; i++)cout<<path[i];cout<<j;cout<<endl;}elseDFS(a,j,v2,n);}}top--;visited[v1] = 0;}int main(){int i, j, n, v1, v2, a[100][100];cin>>n;cin>>v1>>v2;for(i = 0; i < n; i++){for(j = 0; j < n; j++){cin>>a[i][j];}}DFS(a,v1,v2,n);return 0;}1071有向图的邻接矩阵存储顶点删除(难)#include <iostream>#include <malloc.h>#include <stdio.h>using namespace std;int visited[100];typedef struct{int no;int n;int edges[100][100];}MGraph;void Delet(MGraph *&G){G = (MGraph *)malloc(sizeof(MGraph)); int e, i, j;cin>>G->n;cin>>e;for(i = 0; i < G->n; i++){G->no = i;for(j = 0; j < G->n; j++){cin>>G->edges[i][j];}}cout<<G->n-1;cout<<endl;for(i = 0; i < G->n; i++){if(i != e)cout<<i;}cout<<endl;for(i = 0; i < G->n; i++){if(i == e)continue;for(j = 0; j < G->n; j++){if(j == e)continue;cout<<G->edges[i][j];}cout<<endl;}}int main(){MGraph *G;Delet(G);return 0;}1072 有向图的邻接矩阵存储根计算(难)#include <iostream>#include <queue>#define MAXSIZE 100using namespace std;bool bfs(const bool (*arr)[MAXSIZE],const int &n,const int &beg=0); int main(){bool arr[MAXSIZE][MAXSIZE]={0};int n;cin >> n;for(int i=0;i!=n;++i)for(int j=0;j!=n;++j)cin >> arr[i][j];for(int i=0;i!=n;++i)if(bfs(arr,n,i))cout << i;return 0;}bool bfs(const bool (*arr)[MAXSIZE],const int &n,const int &beg) {bool visited[MAXSIZE]={false};int cur,ct(0);queue<int> q;q.push(beg);visited[beg]=true;while(!q.empty()){++ct;cur=q.front();q.pop();for(int i=0;i!=n;++i)if(!visited[i]&&arr[cur][i]){q.push(i);visited[i]=true;}}return n==ct?true:false;}1075求最小生成树(Prim算法)(难)#include<cstdio>#include<cstdlib>#include<memory.h>#include<limits.h>#include<iostream>#define INF 30000using namespace std;int n,m,map[100][100];char a[100];int get(char e){for(int i=0;i<n;i++)if(a[i]==e)return i;}void read(){int i,w;char x,y;memset(map,INF,sizeof(map));cin>>n>>m;for(i=0;i<n;i++)cin>>a[i];for(i=0;i<m;i++){cin>>x>>y>>w;map[get(x)][get(y)]=w;map[get(y)][get(x)]=w;}}void prim(){int lowcost[100],adjvex[100],i,j,k; for(i=0;i<n;i++){lowcost[i]=map[0][i];adjvex[i]=0;}for(i=1;i<n;i++){int min=INF;for(j=1;j<n;j++){if(lowcost[j] && lowcost[j]<min){min=lowcost[j];k=j;}}cout<<"("<<a[adjvex[k]]<<","<<a[k]<<")"; lowcost[k]=0;for(j=0;j<n;j++){if(map[k][j] && map[k][j]<lowcost[j]) {lowcost[j]=map[k][j];adjvex[j]=k;}}}}void print(){for(int i=0;i<n;i++){for(int j=0;j<n;j++)cout<<map[i][j]<<' ';cout<<endl;}}int main(){read();//print();prim();//cout<<endl;return 0;}1076判断给定有向图是否存在回路(难)1010折半查找的实现(易)1012哈希表(链地址处理冲突)(中)1013哈希表(开放定址法处理冲突)(中)#include<iostream>using namespace std;typedef struct Data{int num;int n;}data;int n;data hash[100];void build(){int m;int i;cin>>n;for(i=0;i<n;i++)hash[i].n=0;cin>>m;for(i=0;i<m;i++){int e,t,e_;int mid=1;cin>>e;t=e%n;e_=e;while(hash[t].n!=0){mid++;t=(e_+1)%n;e_++;}hash[t].num=e;hash[t].n=mid;}}int deal(){int e;cin>>e;int e_;e_=e;while (e_%n!=e%n-1){if(hash[e_%n].num==e){cout<<e_%n<<","<<hash[e_%n].n;return 0;}elsee_++;}return -1;}int main(void){build();if(deal()==-1)cout<<-1;return 0;}1014交换排序算法的设计与实现(中) 1015堆排序算法(难)1016插入排序算法实现(易)1098堆的判断(易)1099希尔排序算法实现(中)1077平衡二叉树的判定(中)#include <iostream>#include <string.h>#include <malloc.h>using namespace std;typedef struct node{char data;node *leftchild;node *rightchild;}Node;void Init(Node *N){N = (Node *)malloc(sizeof(Node));}void CreateT(Node *&N){char ch;cin>>ch;if(ch == '#'){N = (Node *)malloc(sizeof(Node));N = NULL;return;}N = (Node *)malloc(sizeof(Node));N->data = ch;CreateT(N->leftchild);CreateT(N->rightchild);}char pre[100], in[100];int i=0, j=0;void preOrder(Node *&N){if(N == NULL)return;pre[i++] = N->data;preOrder(N->leftchild);preOrder(N->rightchild);}void inOrder(Node *&N){if(N == NULL)return;inOrder(N->leftchild);in[j++] = N->data;inOrder(N->rightchild);}int juge(char *pre, char *in, int n) {char *p;int k;if(n <= 1)return 1;for(p = in; p <= in+n; p++){if(*p == *pre)break;}k = p - in;if(n-2*k<=2 && n-2*k>=0){juge(pre,in,k);juge(pre+k,p,n-k-1);return 1;}elsereturn 0;}int main(){Node *N;CreateT(N);preOrder(N);inOrder(N);int n, flag;n = strlen(in);flag = juge(pre, in, n);if(flag == 0){cout<<"no!";return 0;}else{cout<<"yes!";return 0;}}1011二叉排序树的实现和查找(中)#include<stdio.h>#include<malloc.h>typedef struct node{int key;struct node *lchild,*rchild;}BSTNode;int insertBST(BSTNode *&p,int k){if(p==NULL){p=(BSTNode*)malloc(sizeof(BSTNode));p->key=k;p->lchild=p->rchild=NULL;return 1;}else if(k==p->key)return 0;else if(k<p->key)return insertBST(p->lchild,k);elsereturn insertBST(p->rchild,k);}BSTNode *CreatBST(int A[],int n){BSTNode *bt=NULL;int i=0;while (i<n){insertBST(bt,A[i]);i++;}return bt;}int sum=0;BSTNode *SearchBST(BSTNode *bt,int k){ sum++;if(bt==NULL){sum=-1;return bt;}if (bt->key==k)return bt;if (k<bt->key){return SearchBST(bt->lchild,k);}else{return SearchBST(bt->rchild,k);}}int main(){BSTNode *bt;int A[100],n,i,m,j;scanf("%d",&n);for(i=0;i<n;i++){scanf("%d",&A[i]);}scanf("%d",&m);bt=CreatBST(A,n);SearchBST(bt,m);printf("%d",sum);return 0;}。

相关主题