zoukankan      html  css  js  c++  java
  • 顺序查找,折半查找,二叉排序树的建立,哈希表的建立

    #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();
    }
    
  • 相关阅读:
    thrift ssl 证书整理
    snmp学习笔记
    你必须了解的Session的本质
    安装大文件提示错误
    说说大型高并发高负载网站的系统架构(更新)
    【错误】未能找到类型或命名空间名称XXXX和未能解析引用的程序
    js调用后台代码的几种方式
    HTTP 错误 401.1
    万能的编程语言不要习惯性地把工具当作你天性缺陷的遮羞布
    PostgreSQL与MySQL比较
  • 原文地址:https://www.cnblogs.com/Duskcl/p/3819292.html
Copyright © 2011-2022 走看看