SDUT---OJ《数据结构与算法》实践能力专题训练7 查找

这篇博客探讨了多种数据结构实验中的查找技术,包括二叉排序树、平衡二叉树、树的种类统计、二分查找、哈希表和顺序查找。通过实例分析和题目描述,介绍了如何建立和操作这些数据结构,以及如何解决查找问题。

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

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

Output 

Yes
No
No

Hint

#include <iostream>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1010;
int r[55];
int p[55];
int top1,top2;
typedef struct node
{
   int data;
   struct node *right,*left;
}tree;
tree*creat(tree*root,int x)
{
   if(!root)
   {
      root=(tree*)malloc(sizeof(tree));
      root->data=x;
      root->left=root->right=NULL;
      return root;
   }
   else
   {
      if(x<root->data)
      {
         root->left=creat(root->left,x);
      }
      else
      {
        root->right=creat(root->right,x);
      }
   }
   return root;
}
void first1(tree*root)
{
   if(root)
   {
      r[top1++]=root->data;
      first1(root->left);
      first1(root->right);
   }
}
void first2(tree*root)
{
  if(root)
   {
      p[top2++]=root->data;
      first2(root->left);
      first2(root->right);
   }
}
int main()
{
    int n,m;
    while(~scanf("%d",&n))
    {
        if(n==0)
        {
          break;
        }
        scanf("%d",&m);
        tree*root1;
        top1=0;
        int x;
        root1=NULL;
        for(int i=0;i<n;i++)
        {
           scanf("%d",&x);
           root1=creat(root1,x);
        }
        first1(root1);
        for(int i=0;i<m;i++)
        {
           tree*root2;
           root2=NULL;
           top2=0;
           for(int j=0;j<n;j++)
           {
              scanf("%d",&x);
              root2=creat(root2,x);
           }
           first2(root2);
           int flag=0;
           for(int j=0;j<n;j++)
           {
              if(r[i]!=p[i])
              {
                 flag=1;
                 break;
              }
           }
           if(flag==0)
           {
              printf("Yes\n");
           }
           else
           {
              printf("No\n");
           }

        }

    }
    return 0;
}

B - 数据结构实验之查找二:平衡二叉树

Description

根据给定的输入序列建立一棵平衡二叉树,求出建立的平衡二叉树的树根。

Input

输入一组测试数据。数据的第1行给出一个正整数N(n <= 20),N表示输入序列的元素个数;第2行给出N个正整数,按数据给定顺序建立平衡二叉树。

Output

输出平衡二叉树的树根。

Sample

Input 

5
88 70 61 96 120

Output 

70

Hint

#include <iostream>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1010;
typedef struct node
{
   int data;
   int deepth;
   struct node *left,*right;
}tree;
int deep(tree*root)
{
   if(root)
   {
      return root->deepth;
   }
   else
   {
      return -1;
   }
}
tree*LL(tree*root)
{
   tree*p=root->left;
   root->left=p->right;
   p->right=root;
   root->deepth=max(deep(root->left),deep(root->right))+1;
   p->deepth=max(deep(p->left),deep(p->right))+1;
   return p;
}
tree*RR(tree*root)
{
   tree*p=root->right;
   root->right=p->left;
   p->left=root;
   root->deepth=max(deep(root->left),deep(root->right))+1;
   p->deepth=max(deep(p->left),deep(p->right))+1;
   return p;
}
tree*LR(tree*root)
{
   root->left=RR(root->left);
   return LL(root);
}
tree*RL(tree*root)
{
   root->right=LL(root-
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值