二叉排序树SDUT

SDUT题目链接二叉排序树
做这道题的时候WA了好多遍,在网上找到了一些思路,在此和我的思路进行对比。

九度提供的算法设计思路是:对输入的数字序列构建二叉排序树,并对它们进行前序和中序的遍历,依次比较遍历结果是否相同,若相同则说明两棵二叉排序树相同,否则不同。

这个设计思路是没有问题的,但是有点画蛇添足的成份。那么这个“蛇足”是什麽呢?试想一下二叉排序树的性质,如果对二叉排序树来说,对其进行中序遍历,那么无论一组数字按照怎样的顺序构建,其中序遍历后得到的序列都是一样的。例如题目给的测试实例,{5,6,7,4,3,2}和{5,7,6,3,4,2},他们构造的二叉排序树如下:

其中序遍历得到序列都是{2,3,4,5,6,7}。所以说进行中序遍历作为判断的一项是“蛇足”。

对于该题,我们只须对建立的二叉树进行先序遍历,或者后序遍历得到它们的遍历序列进行比较即可。

//preMain保存原二叉树先序遍历的序列
//preOther保存与原二叉树比较的二叉树的先序遍历的序列
char preMain[11], preOther[11];
struct BSTNode{
    int value;
    BSTNode *lchild;
    BSTNode *rchild;
};

BSTNode* allocateNode()
{
    if (BSTNode *node = (BSTNode*)malloc(sizeof(BSTNode)))
    {
        node->lchild = nullptr;
        node->rchild = nullptr;
        return node;
    }
    else
        return nullptr;
}

BSTNode* builtBSTree(BSTNode*T, int value)
{
    if (T == nullptr)
    {
        T = allocateNode();
        T->value = value;
        return T;
    }
    else if (T->value > value)
        T->lchild = builtBSTree(T->lchild, value);
    else if (T->value < value)
        T->rchild = builtBSTree(T->rchild, value);
    return T;
}

int size = 0;
void preOrderBST(BSTNode *root, char preArray[])
{
    if (root != nullptr)
    {
        preArray[size++] = root->value + '0';
    }
    if (root->lchild != nullptr)
    {
        preOrderBST(root->lchild, preArray);
    }
    if (root->rchild != nullptr)
    {
        preOrderBST(root->rchild, preArray);
    }
}

以上内容转载于https://www.cnblogs.com/tgycoder/p/4974497.html
下面是我A的代码
具体思路:1.先对第一个序列建树。
2.对所需要比较的序列建树。
3.然后比较两者的树。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct tree
{
    char data;
    struct tree *l,*r;
};
char a[20],b[20];
struct tree *creat(struct tree *root,char key)
{
    if(root==NULL)
    {
        root=(struct tree *)malloc(sizeof(struct tree ));
        root->data=key;
        root->l=NULL;
        root->r=NULL;
        return root;
    }
    if(key>root->data)
        root->r=creat(root->r,key);
    else
        root->l=creat(root->l,key);
    return root;
}
int cmp(struct tree *root1,struct tree *root2)
{
    if(root1==NULL&&root2==NULL)		**//如果比较到最后一个,则可以满足条件**
    return 1;
    else if(root1!=NULL&&root2!=NULL)
    {
        if(root1->data!=root2->data)
        {
        return 0;
        }
        else if(cmp(root1->l,root2->l)&&cmp(root1->r,root2->r))
        return 1;
    }
    else		**//这个else指的是当其中一个为空时,即两者长度不一样,返回值当然也是0**
    return 0;
}		**//下面还有另一种比较函数的写法,思路也是一样的,只不过换了一种形式**
int main()
{
    int t,n,m,i;
    struct tree *root1,*root2;
    while(~scanf("%d",&t))
    {
        if(t==0)
            break;
        scanf("%s",a);
        n=strlen(a);
        root1=NULL;
        for(i=0;i<n;i++)
        {
            root1=creat(root1,a[i]);
        }
        while(t--)
        {
            root2=NULL;
            scanf("%s",b);
            m=strlen(b);
            for(i=0;i<m;i++)
            {
                root2=creat(root2,b[i]);
            }
            if(cmp(root1,root2)==1)
            printf("YES\n");
            else
            printf("NO\n");
        }
    }
    return 0;
}

这是另外一种比较函数的形式,相比上面那个更精简一点,也易懂。

int cmp(struct tree *root1,struct tree *root2)
{
    if(root1==NULL&&root2==NULL)
        return 1;
    else if(root1==NULL||root2==NULL)
        return 0;
    else if(root1->data!=root2->data)
        return 0;
    else if(cmp(root1->l,root2->l)&&cmp(root1->r,root2->r))
        return 1;
}

猜你喜欢

转载自blog.csdn.net/qq_44011946/article/details/85239756
今日推荐