• 顺序查找,折半查找,二叉排序树的建立,哈希表的建立


    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    class dijiuzhang
    {
    public:
        int a[10];
        int haxitem[10];
        struct Node
        {
            int data;
            Node * rchild;
            Node * lchild;
            Node(int a):data(a),rchild(NULL),lchild(NULL) {}
            Node():rchild(NULL),lchild(NULL) {}
        };
        Node * head;
        Node * cur;
        dijiuzhang()
        {
            Node * head = new Node();
            for(int i = 0 ; i < 10 ; i ++)
            {
                int tem;
                while(1)
                {
                    int flag = 0;
                    tem = rand()%10;
                    for(int j = 0 ; j < i ; j++)
                    {
                        if(tem == a[j])
                        {
                            flag = 1;
                            break;
                        }
                    }
                    if(flag==0)
                    {
                        a[i] = tem;
                        break;
                    }
    
                }
                cout<<a[i]<<" ";
                for(int i = 0 ; i < 10 ; i++)
                {
                    haxitem[i] = a[i];
                }
            }
            cout<<endl;
        }
        void shunxu(int b)//顺序查找
        {
            cout<<"顺序查找"<<endl;
            for(int i = 0 ; i < 10 ; i++)
            {
                if(a[i] == b)
                {
                    cout<<"Find!"<<endl;
                    cout<<"i="<<i<<endl;
                    return;
                }
            }
            cout<<"not find"<<endl;
        }
        void zheban(int b)//折半查找
        {
            cout<<"折半查找:"<<endl;
            int low,high,mid;
            low = 0;
            high = 9;
            sort(a,a+10);
            while(low <= high)
            {
                mid = (low + high) / 2;
                if(a[mid] == b)
                {
                    cout<<"Find!"<<endl;
                    cout<<"i="<<mid<<endl;
                    return;
                }
                else if(a[mid] > b)
                {
                    high = mid - 1;
                }
                else
                {
                    low = mid + 1;
                }
            }
            cout<<"not find"<<endl;
            return;
        }
        void erchapaixushu()//二叉排序树
        {
            cout<<"二叉排序树"<<endl;
            for(int i = 0 ; i < 10 ; i++)
            {
                Node * tem = new Node(a[i]);
                if(i==0)
                {
                    head = tem;
                    cur = tem;
                    continue;
                }
                while(1)
                {
                    if(cur -> data <= tem -> data && cur -> rchild == NULL)
                    {
                        cur -> rchild = tem;
                        cur = head;
                        break;
                    }
                    else if(cur -> data <= tem -> data)
                    {
                        cur = cur -> rchild;
                    }
                    if(cur -> data > tem -> data && cur -> lchild == NULL)
                    {
                        cur -> lchild = tem;
                        cur = head;
                        break;
                    }
                    else if(cur -> data > tem -> data)
                    {
                        cur = cur -> lchild;
                    }
                }
            }
        }
        void dfs(Node * tem)
        {
            if(tem)
            {
                dfs(tem -> lchild);
                cout<<tem -> data<<" ";
                dfs(tem -> rchild);
            }
        }
        void haxi()//哈希表
        {
            int tem[10];
            int haxibiao[10];
            int chongtu[20] = {1,-1,2,-2,3,-3,4,-4,5,-5,6,-6,7,-7,8,-8,9,-9,10,-10};
            memset(haxibiao, 0 , sizeof(int)*10);
            for(int i = 0 ; i < 10 ; i++)tem[i] = haxitem[i];
            for(int j = 0 ; j < 10 ; j ++)
            {
                int temi = 0;
                int temchongtu = 0;
    
                while(1)
                {
                    int z = tem[j]% 7 + temchongtu;
                    if(haxibiao[tem[j]%7+temchongtu]==0&&z >= 0)
                    {
                        haxibiao[tem[j]%7+temchongtu] = tem[j];
                        if(tem[j] == 0)
                        {
                            haxibiao[tem[j]%7+temchongtu] = -1;
                        }
                        temchongtu = 0;
                        temi = 0;
                        break;
                    }
                    else
                    {
                        temchongtu = chongtu[temi++];
                    }
                }
            }
            cout<<endl;
            cout<<"建立的哈希表:"<<endl;
            for(int i = 0 ; i < 10 ; i++)
            {
                if(haxibiao[i] == -1)haxibiao[i] = 0;
            }
            for(int i = 0 ; i < 10 ; i++)
            {
                cout<<haxibiao[i]<<" ";
            }
    
        }
    
    };
    int main()
    {
            dijiuzhang duskcl;
            cout<<"请输入要查找的元素"<<endl;
            int a;
            cin>>a;
            duskcl.zheban(a);
            duskcl.shunxu(a);
            duskcl.erchapaixushu();
            duskcl.dfs(duskcl.head);
            duskcl.haxi();
    }
    
  • 相关阅读:
    获取路径的方式
    读取XML文件(XmlNode和XmlElement区别)
    jQuery 选择器大全
    JS中Null与Undefined的区别
    浅谈JS中的!=、== 、!==、===的用法和区别
    sql面试 查找每个班级的前5名学生(取分类数据的前几条数据)
    C#动态方法调用
    C# 匿名对象(匿名类型)、var、动态类型 dynamic——实用之:过滤类属性、字段实用dynamic
    前言2
    2019-1-17 前言 C#高级编程(第11版)
  • 原文地址:https://www.cnblogs.com/Duskcl/p/3819292.html
Copyright © 2020-2023  润新知