数据结构(陈越 、何钦铭)--第三周编程作业

03-树1 树的同构 (25分)

给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。
数据结构(陈越 、何钦铭)--第三周编程作业_第1张图片
数据结构(陈越 、何钦铭)--第三周编程作业_第2张图片
现给定两棵树,请你判断它们是否是同构的。

输入格式:
输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号);随后N行,第i行对应编号第i个结点,给出该结点中存储的1个英文大写字母、其左孩子结点的编号、右孩子结点的编号。如果孩子结点为空,则在相应位置上给出“-”。给出的数据间用一个空格分隔。注意:题目保证每个结点中存储的字母是不同的。

输出格式:
如果两棵树是同构的,输出“Yes”,否则输出“No”。

输入样例1(对应图1):
8
A 1 2
B 3 4
C 5 -
D - -
E 6 -
G 7 -
F - -
H - -
8
G - 4
B 7 6
F - -
A 5 1
H - -
C 0 -
D - -
E 2 -

输出样例1:
Yes

输入样例2(对应图2):
8
B 5 7
F - -
A 0 3
C 6 -
H - -
D - -
G 4 -
E 1 -
8
D 6 -
B 5 -
E - -
H - -
C 0 2
G - 3
F - -
A 1 4

输出样例2:
No

#include 
#define N 10
#define ElementType char
#define Tree int
#define Null -1

typedef struct TreeNode
{
    ElementType Element;
    Tree Left;
    Tree Right;
}TREENODDE;
TREENODDE T1[N];
TREENODDE T2[N];

Tree BuildTree(TREENODDE T[]);
int Issame(Tree R1, Tree R2);

int main()
{
    Tree R1, R2;

    R1 = BuildTree(T1);
    R2 = BuildTree(T2);
    if(Issame(R1, R2))  printf("Yes");
    else                printf("No");

    return 0;
}

Tree BuildTree(TREENODDE T[])
{
    int maxsize;
    char cl, cr;
    int check[N], i, Root = Null;

    scanf("%d", &maxsize);

    if(maxsize)
    {
        for(i = 0; i < maxsize; i++)  check[i] = 0;
        for(i = 0; i < maxsize; i++)
        {
            scanf("\n%c %c %c", &T[i].Element, &cl, &cr);
            if(cl != '-')
            {
                T[i].Left = cl - '0';
                check[T[i].Left] = 1;
            }
            else T[i].Left = Null;
            if(cr != '-')
            {
                T[i].Right = cr - '0';
                check[T[i].Right] = 1;
            }
            else T[i].Right = Null;
        }

        for(i = 0; i < maxsize; i++)
        {
            if(!check[i])  break;
        }
        Root = i;
    }
    return Root;
}

int Issame(Tree R1, Tree R2)
{
    if((R1 == Null) && (R2 == Null))
        return 1;
    if(((R1 == Null) && (R2 != Null)) || ((R1 != Null) && (R2 == Null)))
        return 0;
    if(T1[R1].Element != T2[R2].Element)
        return 0;
    if((T1[R1].Left == Null) && (T2[R2].Left == Null))
        return Issame(T1[R1].Right, T2[R2].Right);
    if(((T1[R1].Left != Null) && (T2[R2].Left != Null)) &&
       ((T1[T1[R1].Left].Element) == (T2[T2[R2].Left].Element)))
        return (Issame(T1[R1].Left, T2[R2].Left) && Issame(T1[R1].Right, T2[R2].Right));
    else
        return (Issame(T1[R1].Left, T2[R2].Right) && Issame(T1[R1].Right, T2[R2].Left));
}

03-树2 List Leaves (25分)

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a “-” will be put at the position. Any pair of children are separated by a space.

Output Specification:
For each test case, print in one line all the leaves’ indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:

8
1-
0 -
2 7
__
__
5 -
4 6
Sample Output:
4 1 5

#include 
#include 
#define MAX_SIZE 10
#define Tree int
#define Null -1

typedef struct TreeNode
{
    Tree Left;
    Tree Right;
}TREENODDE;
TREENODDE T[MAX_SIZE];

struct Node
{
    Tree Data;
    struct Node *Next;
};
struct QNode
{
    struct Node *rear;
    struct Node *front;
};
typedef struct QNode *Queue;

Tree BuildTree(TREENODDE T[], int N);
void OutputLeaves(Tree Root, TREENODDE T[]);

Queue CreatQueue();
void AddQ(Queue Q, int item);
int DeleteQ(Queue Q);

int main()
{
    Tree Root;
    int N;

    scanf("%d", &N);
    if(!N){
        return 0;
    }

    Root = BuildTree(T, N);
    OutputLeaves(Root,T);

    return 0;
}

Tree BuildTree(TREENODDE T[], int N)
{
    int i, check[MAX_SIZE];
    char cl, cr;

    if(N)
    {
        for(i = 0; i < N; i++)
            check[i] = 0;
        for(i = 0; i < N; i++)
        {
            scanf("\n%c %c", &cl, &cr);
            if(cl != '-')
            {
                T[i].Left = cl - '0';
                check[T[i].Left] = 1;
            }
            else
                T[i].Left = Null;
            if(cr != '-')
            {
                T[i].Right = cr - '0';
                check[T[i].Right] = 1;
            }
            else
                T[i].Right = Null;

        }
    }
    for(i = 0; i < N; i++)
    {
        if(!check[i])
            break;
    }
    return i;
}

void OutputLeaves(Tree Root, TREENODDE T[])
{
    int t, flag = 0;
    Queue Q;
    Q = CreatQueue();
    if((T[Root].Left == Null) && (T[Root].Right == Null)){
        printf("%d", Root);
        return;
    }
    AddQ(Q, Root);
    while(Q->front != NULL)
    {
        t = DeleteQ(Q);
        if((T[t].Left == Null) && (T[t].Right == Null))
        {
            if(flag)
                printf(" ");
            printf("%d", t);
            flag = 1;
        }
        if(T[t].Left != Null)
            AddQ(Q, T[t].Left);
        if(T[t].Right != Null)
            AddQ(Q, T[t].Right);
    }
    free(Q);
}

Queue CreatQueue()
{
    Queue Q;
    Q = (Queue)malloc(sizeof(struct QNode));
    Q->front = NULL;
    Q->rear = NULL;
    return Q;
}

void AddQ(Queue Q, int item)
{
    struct Node *s;
    s = (struct Node*)malloc(sizeof(struct Node));
    s->Data = item;  s->Next = NULL;

    /*看添加的节点是不是第一个节点*/
    if(Q->front == NULL)
    {
        Q->front = s;
        Q->rear = s;
    }
    else
    {
        Q->rear->Next = s;
        Q->rear = s;
    }
}

/*如果Q为空,返回Null,即-1*/
int DeleteQ(Queue Q)
{
    int s;
    struct Node *item;
    if(Q->front == NULL)
    {
        return Null;
    }
    else
    {
        s = Q->front->Data;
        item = Q->front;
        Q->front = Q->front->Next;
        free(item);
    }
    return s;
}

03-树3 Tree Traversals Again (25分)

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.
数据结构(陈越 、何钦铭)--第三周编程作业_第3张图片
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: “Push X” where X is the index of the node being pushed onto the stack; or “Pop” meaning to pop one node from the stack.

Output Specification:
For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:
6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop

Sample Output:
3 4 2 6 5 1

/*
一,程序的思路如下:
    堆栈输入实际上包含了两种二叉树的遍历方法,即前序遍历和中序遍历
    Push是前序,即1 2 3 4 5 6
    Pop是中序,即3 2 4 1 6 5
    而我们要做到就是已知两种遍历求后序遍历的情况
    方法就不细说了前序第一个元素就是根节点,从中序序列中找到根节点,之前的就是左子树,之后的就是右子树,对左右子树进行递归循环即可
二。实际上本题用C++语言来编写简单好看,但本人还没学习C++,所以程序较为繁琐
    但其实,只要理解了题目到底要干什么,对遍历操作有基本的认识,就可以做题了
*/
#include 
#include 
#define MAXSIZESTR 4
#define MAXSIZE 30

int flag = 0;
void PrintPost(int Preorder[], int Inorder[], int N);

int main()
{

    int N, Preorder[MAXSIZE], PreorderT[MAXSIZE], Inorder[MAXSIZE];
    char str[MAXSIZESTR + 1];   int item;
    int i, Prei = 0, Ini = 0, PreiT = 0;

    scanf("%d", &N);
    if(N)
    {
        for(i = 0; i < 2*N; i++)
        {
            scanf("\n%s", str);
            if(strcmp(str, "Push") == 0)
            {
                scanf(" %d", &item);
                Preorder[Prei++] = item;
                PreorderT[PreiT++] = item;
            }
            else
            {
                Inorder[Ini++] = PreorderT[--PreiT];
            }
        }
    }

    PrintPost(Preorder, Inorder, N);

    return 0;
}

void PrintPost(int Preorder[], int Inorder[], int N)
{
    int Root, i, j;
    int P[MAXSIZE], I[MAXSIZE];
    if(N)
    {
        for(i = 0; i < N; i++)
    {
        P[i] = Preorder[i];
        I[i] = Inorder[i];
    }
    int Pleft[MAXSIZE], Ileft[MAXSIZE], Nleft;
    int Pright[MAXSIZE], Iright[MAXSIZE], Nright;
    Root = P[0];
    for(i = 0; i < N; i++)
    {
        if(I[i] == Root)
        {
            break;
        }
    }
    Nleft = i;  Nright = N-i-1;
    for(j = 0; j < Nleft; j++)
    {
        Pleft[j] = P[j+1];
        Ileft[j] = I[j];
    }
    for(j = 0; j < Nright; j++)
    {
        Pright[j] = P[j + Nleft + 1];
        Iright[j] = I[j + Nleft + 1];
    }
    PrintPost(Pleft, Ileft, Nleft);
    PrintPost(Pright, Iright, Nright);
    if(flag)
    {
        printf(" ");
    }
    printf("%d", Root);
    flag = 1;
    }
    return;
}

你可能感兴趣的:(中国大学MOOC-陈越,何钦铭-数据结构-2020夏,数据结构,c语言)