数据结构实验之查找一:二叉排序树

数据结构实验之查找一:二叉排序树

Problem Description
对应给定的一个序列可以唯一确定一棵二叉排序树。然而,一棵给定的二叉排序树却可以由多种不同的序列得到。例如分别按照序列{3,1,4}和{3,4,1}插入初始为空的二叉排序树,都得到一样的结果。你的任务书对于输入的各种序列,判断它们是否能生成一样的二叉排序树。

Input
输入包含若干组测试数据。每组数据的第1行给出两个正整数N (n < = 10)和L,分别是输入序列的元素个数和需要比较的序列个数。第2行给出N个以空格分隔的正整数,作为初始插入序列生成一颗二叉排序树。随后L行,每行给出N个元素,属于L个需要检查的序列。
简单起见,我们保证每个插入序列都是1到N的一个排列。当读到N为0时,标志输入结束,这组数据不要处理。

Output
对每一组需要检查的序列,如果其生成的二叉排序树跟初始序列生成的二叉排序树一样,则输出"Yes",否则输出"No"。

Sample Input
4 2
3 1 4 2
3 4 1 2
3 2 4 1
2 1
2 1
1 2
0

Sample Output
Yes
No
No

不知道为什么,我看着这种风格的代码特别舒服,简单易懂;
不过可能以后不常见了hhh

#include <bits/stdc++.h>
using namespace std;

struct node
{
    int data;
    struct node *l,*r;
};

struct node *creat(struct node *root,int t)
{
    if(root==NULL)
    {
        root= new node;
        root->data=t;
        root->l=NULL;
        root->r=NULL;
    }
    else
    {
        if(t>root->data)
            root->r=creat(root->r,t);
        else if (t<root->data)
            root->l=creat(root->l,t);
    }
    return root;
}

int ok(struct node *t1,struct node *t2)
{
    if(t1==NULL&&t2==NULL)
        return 1;
    else if (t1!=NULL&&t2!=NULL)
    {
        if(t1->data!=t2->data)
            return 0;
        else if(ok(t1->l,t2->l)&&ok(t1->r,t2->r))
            return 1;
    }
    return 0;
}

int main()
{
    int n,m;
    int a[55],b[55];
    while(scanf("%d",&n)!=EOF&&n)
    {
        scanf("%d",&m);
        struct node *root1,*root2;
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        root1=new node;
        root1->data=a[0];
        root1->l=root1->r=NULL;
        for(int i=1; i<n; i++)
            root1=creat(root1,a[i]);
        while(m--)
        {
            for(int i=0; i<n; i++)
                scanf("%d",&b[i]);
            root2=new node;
            root2->data=b[0];
            root2->l=root2->r=NULL;
            for(int i=1; i<n; i++)
                root2=creat(root2,b[i]);
            int f=ok(root1,root2);
            if(f)
                printf("Yes\n");
            else
                printf("No\n");
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉排序树是一棵二叉树,其每个节点的左子树节点的值都小于该节点的值,右子树节点的值都大于该节点的值。 插入操作的伪代码如下: ``` // 插入一个值为value的节点 insert(value): if root is NULL: // 如果树为空,则插入为根节点 root = new Node(value) else: // 从根节点开始遍历,找到合适的位置插入 current = root while current is not NULL: if value < current.value: // 插入节点的值小于当前节点,向左子树遍历 if current.left is NULL: // 如果左子树为空,则插入为左子节点 current.left = new Node(value) return else: current = current.left else: // 插入节点的值大于等于当前节点,向右子树遍历 if current.right is NULL: // 如果右子树为空,则插入为右子节点 current.right = new Node(value) return else: current = current.right ``` 查找操作也很简单,从根节点开始遍历,如果找到了相应的节点就返回,否则返回空值。删除操作稍微复杂一些,需要分情况讨论。 如果要删除的节点没有子节点,直接删除即可;如果要删除的节点只有一个子节点,则用其子节点替代该节点;如果要删除的节点有两个子节点,则需要找到该节点的前驱或后继节点来代替该节点。具体实现可以参考下面的伪代码: ``` // 删除值为value的节点 delete(value): // 查找要删除的节点 current = root parent = NULL while current is not NULL and current.value != value: parent = current if value < current.value: current = current.left else: current = current.right if current is NULL: // 没有找到要删除的节点 return if current.left is NULL and current.right is NULL: // 要删除的节点没有子节点 if current == root: // 如果要删除的节点是根节点 root = NULL else: if parent.left == current: parent.left = NULL else: parent.right = NULL else if current.left is NULL or current.right is NULL: // 要删除的节点只有一个子节点 if current.left is not NULL: child = current.left else: child = current.right if current == root: // 如果要删除的节点是根节点 root = child else: if parent.left == current: parent.left = child else: parent.right = child else: // 要删除的节点有两个子节点 // 找到前驱节点 predecessor = current.left while predecessor.right is not NULL: predecessor_parent = predecessor predecessor = predecessor.right // 用前驱节点替代要删除的节点 current.value = predecessor.value // 删除前驱节点 if predecessor_parent.left == predecessor: predecessor_parent.left = predecessor.left else: predecessor_parent.right = predecessor.left ``` 这些操作都可以在 $O(h)$ 的时间内完成,其 $h$ 是树的高度。如果树平衡,则 $h$ 为 $log_2(n)$,其 $n$ 是树节点的数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值