數據結構與算法題目集(中文) 7-4 是否同一棵二叉搜索樹 (25分)

在這裏插入圖片描述

4 2
3 1 4 2
3 4 1 2
3 2 4 1
2 1
2 1
1 2
0

題解:存儲樹的兩種不同方式

方法一:定義結構體

#include <stdio.h>
#include <stdlib.h>
typedef struct TNode *BinTree;
struct TNode
{
    int data;
    BinTree Left;
    BinTree Right;
};
BinTree Insert(int x, BinTree BST)
{
    if (!BST)
    { //原樹爲空,生成並返回只有1個結點的樹
        BST = (BinTree)malloc(sizeof(struct TNode));
        BST->data = x;
        BST->Left = NULL;
        BST->Right = NULL;
    }
    else
    { //判斷插入位置
        if (x < BST->data)
            BST->Left = Insert(x, BST->Left); //遞歸插入左子樹
        else if (x > BST->data)
            BST->Right = Insert(x, BST->Right); //遞歸插入右子樹
    }
    return BST;
}
int Compare(BinTree a, BinTree b)
{
    if (a == NULL && b == NULL) //同時爲空相同
        return 1;
    else if ((a == NULL && b != NULL) && (a != NULL && b == NULL)) //不同時爲空,不同
        return 0;
    else if (a->data != b->data) //數值不相同,不同
        return 0;
    else
    {
        int flag1 = 0;
        int flag2 = 0;
        flag1 = Compare(a->Left, b->Left); //判斷左子樹
        if (flag1)
        {                                        //相同
            flag2 = Compare(a->Right, b->Right); //在左子樹相同的情況下判斷右子樹
            if (flag2)
                return 1; //都相同返回1
            else
                return 0; //右子樹不同
        }
        else
            return 0; //左子樹不同
    }
}
int main()
{
    int N, L, i, j, x;
    scanf("%d", &N);
    while (N)
    {
        scanf("%d", &L);
        BinTree T[L + 1];
        for (i = 0; i <= L; i++)
            T[i] = NULL;
        for (j = 0; j < L + 1; j++)
        {
            for (i = 0; i < N; i++)
            {
                scanf("%d", &x);
                T[j] = Insert(x, T[j]);
            }
        }
        for (i = 1; i < L + 1; i++)
        {
            if (Compare(T[0], T[i]))
                printf("Yes\n");
            else
                printf("No\n");
        }
        scanf("%d", &N);
    }
    return 0;
}

方法二:數組

注意:數組足夠大

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxn 1024
int a[maxn], b[maxn]; //數組存儲二叉樹
int Insert(int data, int p, int *tree) //給輸入的值找到存入數組中的下標
{
    if (tree[p] == -1)
        return p;
    else if (tree[p] > data)
        return Insert(data, 2 * p, tree); //到左子樹上找
    else
        return Insert(data, 2 * p + 1, tree); //到右子樹上找
}

void Create1(int N)
{
    int x, i;
    memset(a, -1, sizeof(a));
    for (i = 1; i <= N; i++)
    {
        scanf("%d", &x);
        int d = Insert(x, 1, a); //從根節點開始遞歸查找插入位置
        a[d] = x;
    }
}
void Create2(int N)
{
    int y, i;
    memset(b, -1, sizeof(b));
    for (i = 1; i <= N; i++)
    {
        scanf("%d", &y);
        int d = Insert(y, 1, b); //從根節點開始遞歸查找插入位置
        b[d] = y;
    }
}

int Compare(int *a, int *b) //判斷給定的二叉搜索樹在數組中的序列與要判斷的搜索樹序列是否相同
{
    int i;
    for (i = 1; i < maxn; i++)
    { //maxn應該爲1024,剛開始我寫的512,N最大時(N=2^10)答案錯誤
        if (a[i] != b[i])
            return 0;
    }
    return 1;
}
int main()
{
    int N, L, i, j;
    scanf("%d", &N);
    while (N)
    {
        scanf("%d", &L);
        int flag[L];
        Create1(N);
        for (j = 0; j < L; j++)
        {
            Create2(N);
            if (Compare(a, b))
                flag[j] = 1;
            else
                flag[j] = 0;
        }
        for (i = 0; i < L; i++)
        {
            if (flag[i])
                printf("Yes\n");
            else
                printf("No\n");
        }
        scanf("%d", &N);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章