类C题程编

@C

先序输出叶结点 (15 分)

void PreorderPrintLeaves( BinTree BT ){
	if(BT){
		if(!BT->Left&&!BT->Right)
			printf(" %c",BT->Data);
		if(BT->Left) PreorderPrintLeaves( BT->Left );
		if(BT->Right) PreorderPrintLeaves( BT->Right );
	}
}

邻接矩阵存储图的深度优先遍历

void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) ){
	Visited[V]=true;
	Visit(V);
	for(int i=0;i<Graph->Nv;i++){
		if(Graph->G[V][i]!=INFINITY&&Visited[i]==false)
		DFS(Graph,i,Visit); 
	}
}

邻接表存储图的广度优先遍历

void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ){
	int ii=0,jj=0;//头追尾模拟队列 
	int a[MaxVertexNum+1];
	Visited[S]=true;
	Visit(S);
	a[jj++]=S;//顶点先入队列尾 
	while(ii!=jj){
		PtrToAdjVNode p=Graph->G[a[ii++]].FirstEdge;//ii(头)追jj(尾) 
		while(p){
			Vertex flag = p->AdjV;
            if(!Visited[flag])//flag没被输出过
            {
                Visit(flag);
                Visited[flag] = 1;
                a[jj++]=flag;
            }
            p=p->Next;
		}
	}
}

二叉树的遍历

void InorderTraversal( BinTree BT ){
	if(BT){
		InorderTraversal( BT->Left );
		printf(" %c",BT->Data);
		InorderTraversal( BT->Right );
	}
}
void PreorderTraversal( BinTree BT ){
	if(BT){
		printf(" %c",BT->Data);
		PreorderTraversal( BT->Left );
		PreorderTraversal(  BT->Right );
	}
	
}
void PostorderTraversal( BinTree BT ){
	if(BT){
		PostorderTraversal( BT->Left );
		PostorderTraversal( BT->Right );
		printf(" %c",BT->Data);
	}
}
void LevelorderTraversal( BinTree BT ){
	BinTree a[100];//模拟队列
	BinTree p;//临时变量 
	int ii,jj;//头 尾
	if(BT){
		a[jj++]=BT;//初始化尾部入队 
		while(ii!=jj){
			p=a[ii++];//头元素出队 
			printf(" %c",p->Data);
			if(p->Left) a[jj++]=p->Left;
			if(p->Right) a[jj++]=p->Right;
		}	
	} 
}

7-1 树的遍历 (25 分)

#include<bits/stdc++.h>
using namespace std;
//1,以后序数列最后一位拆中序数列和后序序列,且此位为树的根节点
//2,将拆得的中序序列1按照拆得的后序序列1最后一位拆中序数列1和后序数列1,且此位为子树根节点 
//3,将拆得的中序序列2按照拆得的后序序列2最后一位拆中序数列2和后序数列2,且此位为子树根节点 
//重复1,2,3;递归进行 (分治思想) 
struct node {
    int index, value;
};
bool cmp(node a, node b) {
    return a.index < b.index;
}
vector<int> post, in;
vector<node> ans;
void pre(int root, int start, int end, int index) {
    if (start > end) return;
    int i = start;
    while (i < end && in[i] != post[root]) i++;
    ans.push_back({index, post[root]});
    pre(root - 1 - end + i, start, i - 1, 2 * index + 1);
    pre(root - 1, i + 1, end, 2 * index + 2);
}
int main() {
    int n;
    scanf("%d", &n);
    post.resize(n);
    in.resize(n);
    for (int i = 0; i < n; i++) scanf("%d", &post[i]);
    for (int i = 0; i < n; i++) scanf("%d", &in[i]);
    pre(n - 1, 0, n - 1, 0);
    sort(ans.begin(), ans.end(), cmp);
    for (int i = 0; i < ans.size(); i++) {
        if (i != 0) cout << " ";
        cout << ans[i].value;
    }
    return 0;
}

7-2 还原二叉树 (25 分)

#include<bits/stdc++.h>
using namespace std;
char str1[110],str2[110];
int dfs(char a[],char b[],int m)
{
    if(m==0)
    {
        return 0;//没有节点,为空树
    }
    int i;
    for(i=0;i<m;i++)
    {
        if(b[i]==a[0])//找到根节点在中序的位置
        {
             break;
        }
    }
    int c=dfs(a+1,b,i)+1;//左子树的深度
    int d=dfs(a+i+1,b+i+1,m-i-1)+1;//右子树的深度
    return c>d?c:d;
}
int main()
{
    int n;
    cin>>n;//节点个数
    cin>>str1>>str2;
    cout<<dfs(str1,str2,n)<<endl;
    return 0;
 
}

7-3 根据后序和中序遍历输出先序遍历 (25 分)

#include<bits/stdc++.h> 
using namespace std;
int in[31],post[31];
typedef struct BiTNode{
	int data;
	struct BiTNode *lchild;
	struct BiTNode *rchild;
}BiTNode,*BiTree;
BiTree Build(int *in,int *post,int n){//第一个参数是中序序列的起始位置,第二个参数是后序序列的起始位置,n是长度 
	if(n <= 0)//如果长度小于等于0,直接返回即可 
		return NULL;
	int *p = in;//定义一个指向in的指针 
	while(p){//找到根节点在中序中的位置 
		if(*p == *(post + n - 1))
			break;
		p++;
	}
	BiTree T = new BiTNode;
	T->data = *p;
	int len = p - in;
	T->lchild = Build(in,post,len);
	T->rchild = Build(p + 1,post + len,n - len - 1);
	return T; 
}
void PreOrder(BiTree T){
	if(T){
		printf(" %d",T->data);
		PreOrder(T->lchild);
		PreOrder(T->rchild);
	}
	return;
}
int main(){
	int n;
	BiTree T;//只需要定义,不需要赋值->[BiTree T = new BiTNode;] 
	scanf("%d",&n);
	for(int i = 0;i < n;i++)
		scanf("%d",&post[i]);
	for(int i = 0;i < n;i++)
		scanf("%d",&in[i]);
	T = Build(in,post,n);
	printf("Preorder:");
	PreOrder(T);
	return 0;
}

二叉搜索树的结构 (30 分)

*输入在第一行给出一个正整数N(≤100),随后一行给出N个互不相同的整数,数字间以空格分隔,要求将之顺次插入一棵初始为空的二叉搜索树。之后给出一个正整数M(≤100),随后M行,每行给出一句待判断的陈述句。陈述句有以下6种:

A is the root,即"A是树的根";
A and B are siblings,即"A和B是兄弟结点";
A is the parent of B,即"A是B的双亲结点";
A is the left child of B,即"A是B的左孩子";
A is the right child of B,即"A是B的右孩子";
A and B are on the same level,即"A和B在同一层上"。
题目保证所有给定的整数都在整型范围内。

输出格式:
对每句陈述,如果正确则输出Yes,否则输出No,每句占一行。

输入样例:
5
2 4 1 3 0
8
2 is the root
1 and 4 are siblings
3 and 0 are on the same level
2 is the parent of 4
3 is the left child of 4
1 is the right child of 2
4 and 0 are on the same level
100 is the right child of 3
输出样例:
Yes
Yes
Yes
Yes
Yes
No
No
No*

#include<bits/stdc++.h>
using namespace std;
 
const int INF=0x3f3f3f3f;
typedef int ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode
{
    ElementType Data;
    Position parent;
    BinTree Left;
    BinTree Right;
};
 
void InsertTree(BinTree &T,int k)
{
    if(!T)
    {
        T=(BinTree)malloc(sizeof(TNode));
        T->Data=k;
        T->Left=T->Right=T->parent=NULL;
    }
    else
    {
        Position p=T;
        Position pre=NULL;
        while(p)
        {
            pre=p;
            if(p->Data<k)
                p=p->Right;
            else
                p=p->Left;
        }
        p=(Position)malloc(sizeof(TNode));
        p->Data=k;
        p->Left=p->Right=NULL;
        p->parent=pre;
        if(pre->Data>k)
            pre->Left=p;
        else if(pre->Data<k)
            pre->Right=p;
        else
            return ;
    }
}
 
Position Search(BinTree T,int k)
{
    Position p=T;
    while(p)
    {
        if(p->Data>k)
            p=p->Left;
        else if(p->Data<k)
            p=p->Right;
        else
            return p;
    }
    return NULL;
}
int GetNumber(BinTree T,int k)
{
    int sum=1;
    Position p=T;
    while(p)
    {
        if(p->Data>k)
            p=p->Left;
        else if(p->Data<k)
            p=p->Right;
        else
            return sum;
        sum++;
    }
    return INF;
}
int main()
{
    BinTree T=NULL;
    int n;
    scanf("%d",&n);
    int v;
    for(int i=0; i<n; i++)
    {
        scanf("%d",&v);
        InsertTree(T,v);
    }
    int m;
    scanf("%d",&m);
    getchar();
    char s[110];
    int v1,v2;
    for(int i=0; i<m; i++)
    {
        scanf("%d %s",&v1,s);
        if(!strcmp(s,"is"))
        {
            scanf("%s",s);
            scanf("%s",s);
 
            if(!strcmp(s,"root"))
            {
                if(T&&T->Data==v1)
                    printf("Yes\n");
                else
                    printf("No\n");
                continue;
            }
            else if(!strcmp(s,"left"))
            {
                Position p1=Search(T,v1);
                scanf("%s",s);
                scanf("%s %d",s,&v2);
                Position p2=Search(T,v2);
                if(p2&&p1&&p2->Left==p1)
                    printf("Yes\n");
                else
                    printf("No\n");
            }
            else if(!strcmp(s,"right"))
            {
                Position p1=Search(T,v1);
                scanf("%s",s);
                scanf("%s %d",s,&v2);
                Position p2=Search(T,v2);
                if(p2&&p1&&p2->Right==p1)
                    printf("Yes\n");
                else
                    printf("No\n");
            }
            else if(!strcmp(s,"parent"))
            {
                Position p1=Search(T,v1);
                scanf("%s %d",s,&v2);
                Position p2=Search(T,v2);
                if(p2&&p1&&p2->parent==p1)
                    printf("Yes\n");
                else
                    printf("No\n");
            }
        }
        else if(!strcmp(s,"and"))
        {
            scanf("%d %s",&v2,s);
            scanf("%s",s);
            if(!strcmp(s,"siblings"))
            {
                Position p1=Search(T,v1);
                Position p2=Search(T,v2);
                if(p1&&p2&&p1->parent==p2->parent)
                    printf("Yes\n");
                else
                    printf("No\n");
            }
            else if(!strcmp(s,"on"))
            {
                scanf("%s",s);
                scanf("%s",s);
                scanf("%s",s);
                int n1=GetNumber(T,v1);
                //printf("%d\n",n1);
                int n2=GetNumber(T,v2);
                if(n1==n2&&n1!=INF)
                    printf("Yes\n");
                else
                    printf("No\n");
            }
        }
    }
    return 0;
}

功能快捷键

撤销:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + B
斜体:Ctrl/Command + I
标题:Ctrl/Command + Shift + H
无序列表:Ctrl/Command + Shift + U
有序列表:Ctrl/Command + Shift + O
检查列表:Ctrl/Command + Shift + C
插入代码:Ctrl/Command + Shift + K
插入链接:Ctrl/Command + Shift + L
插入图片:Ctrl/Command + Shift + G

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值