2015级计科软件《数据结构与算法》实践能力专题训练7查找

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

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;

struct TreeSort
{
    int data;
    struct TreeSort *left, *right;
};
int n, m;

void Create(struct TreeSort *root)
{
    int i, h;
    root->left = NULL;
    root->right = NULL;
    scanf ( "%d", &root->data );
    for ( i = 1;i < n; i++ )
    {
        struct TreeSort *q = root;
        scanf ( "%d", &h );
        while ( true )
        {
            if ( q->data > h )
            {
                if ( q->right )
                    q = q->right;
                else
                {
                    struct TreeSort *p = new TreeSort;
                    p->data = h;
                    p->right = NULL;
                    p->left = NULL;
                    q->right = p;
                    break;
                }
            }
            else
            {
                if ( q->left )
                    q = q->left;
                else
                {
                    struct TreeSort *p = new TreeSort;
                    p->data = h;
                    p->right = NULL;
                    p->left = NULL;
                    q->left = p;
                    break;
                }
            }
        }
    }
};

bool compare(struct TreeSort *root, struct TreeSort *root1)
{
    if ( !root&&!root1 )
        return true;
    if ( (root&&root1) && root->data == root1->data && (compare(root->left,root1->left)) && (compare(root->right,root1->right)) )
        return true;
    return false;
}

int main()
{
    int i;
    while ( ~scanf ( "%d", &n )&& n != 0 )
    {
        scanf ( "%d", &m );
        struct TreeSort *root = new TreeSort;
        Create(root);
        for ( i = 0;i < m; i++ )
        {
            struct TreeSort *root1 = new TreeSort;
            Create(root1);
            printf ( compare(root, root1) ? "Yes\n" : "No\n" );
        }
    }
}

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

自搜答案

数据结构实验之查找三:树的种类统计

#include <cstring>
#include <cstdio>
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int top;
char ch[30];
char str[100005][30];

bool Find(char s[])
{
    int i;
    for ( i = 0; i < top; i++ )
    {
        if ( strcmp(str[i], s) == 0 )
            return false;
    }
    return true;
}

int cmp(const void *a,const void*b)
{
    return ( strcmp((char*)a,(char*)b) );
}

int main()
{
    int n, i, j;
    top = 0, j = 0;
    scanf ( "%d", &n );
    map<string, int> ma;
        getchar();
    while ( gets(ch) != NULL )
    {
        int len = strlen(ch);
        for ( i = 0; i < len; i++ )
            ch[i] = tolower(ch[i]);
        if ( Find(ch) )
            strcpy(str[top++], ch);
        string s = (string)ch;
        ma[s]++;
        if ( j++ == n-1 )
            break;
    }
    qsort(str, top, sizeof(str[0]), cmp);
    for ( i = 0; i < top; i++ )
    {
        string s = (string)str[i];
        printf ( "%s %.2lf", str[i], (double)ma[s]/(double)n*100 );
        cout<<"%"<<endl;
    }
}
数据结构实验之查找四:二分查找

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;

int n, m;
int a[1000005];

int dichotomy(int x)
{
    int high = n-1;
    int low = 0;
    while ( high >= low )
    {
        int mid = (high+low)/2;
        if ( a[mid] > x )
            high = mid-1;
        else if ( a[mid] < x )
            low = mid+1;
        else
            return mid;
    }
    return -1;
}

int main()
{
    int i, h;
    scanf ( "%d %d", &n, &m );
    for ( i = 0;i < n; i++ )
    {
        scanf ( "%d", &a[i] );
    }
    for ( i = 0;i < m; i++ )
    {
        scanf ( "%d", &h );
        int sum = dichotomy(h);
        printf ( "%d\n", sum );
    }
}


数据结构实验之查找五:平方之哈希表

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;

int Hash[502];

int main()
{
    int h;
    int n, m, i, j;
    while ( ~scanf ( "%d %d", &n, &m ) )
    {
        memset ( Hash, 0, sizeof(Hash) );
        for ( i = 0;i < n; i++ )
        {
            scanf ( "%d", &h );
            int sum = h%m;
            if ( Hash[sum]++ == 0 )
                printf ( "%d", sum );
            else
            {
                for ( j = 1;j < m; j++ )
                {
                    if ( Hash[(sum+j*j)%m] == 0 )
                    {
                        printf ( "%d", (sum+j*j)%m );
                        Hash[(sum+j*j)%m]++;
                        break;
                    }
                    else if ( Hash[(sum-j*j)%m] == 0 )
                    {
                        printf ( "%d", (sum-j*j)%m );
                        Hash[(sum-j*j)%m]++;
                        break;
                    }
                }
            }
            if ( i == n-1 )
                printf ( "\n" );
            else
                printf ( " " );
        }
    }
    return 0;
}

数据结构实验之查找六:顺序查找

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
    int n, m, i, j;
    int k;
    while ( ~scanf ( "%d %d", &n, &m ) )
    {
        k = -1;
        for ( i = 0;i < n; i++ )
        {
            scanf ( "%d", &j );
            if ( j == m )
            {
                k = i;
            }
        }
        if ( k == -1 )
            printf ( "No\n" );
        else
            printf ( "%d %d\n", k+1, n-k );
    }
}

数据结构实验之查找七:线性之哈希表

#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

int Hash[3500];

int main()
{
    int n, m, i, j;
    int h;
    while ( ~scanf ( "%d %d", &n, &m ) )
    {
        memset ( Hash, -1, sizeof(Hash) );
        for ( i = 0;i < n; i++ )
        {
            scanf ( "%d", &h );
            int sum = h%m;
            if ( Hash[sum] == -1 )
            {
                printf ( "%d", sum );
                Hash[sum] = h;
            }
            else
            {
                int ok = 1;
                for ( j = 0;j < m; j++ )
                {
                    if ( Hash[j] == h )
                    {
                        printf ( "%d", j );
                        ok = 0;
                        break;
                    }
                }
                if ( ok == 1 )
                {
                    while ( Hash[sum%m] != -1 )
                        sum++;
                    printf ( "%d", sum%m );
                    Hash[sum%m] = h;
                }
            }
            if ( i == n-1 )
                printf ( "\n" );
            else
                printf ( " " );
        }
    }
    return 0;
}

代码菜鸟,如有错误,请多包涵!!!

如有帮助记得支持我一下,谢谢!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值