• 哈希表-线性探测插入删除


    插入删除接近常量,大o表示法最快的方式
    哈希表查询也快,但是底层存储结构是数组,一旦创建无法改变大小
    哈希表无法用来有序遍历
    冲突的解决方法:开放地址法(线性探测,二次探测,再哈希)和链地址法

    //数据项(key值)
    public class DataItem {
        private int iData;
        public DataItem(int ii) {
            iData=ii;
        }
        public int getkey() {
            return iData;
        }
        
    
    }
    //哈希表
    public class HashTable {
        private DataItem[] hashArray;//数组
        private int arraySize;//哈希表的大小
        private DataItem nonItem;//标志删除之后,该位置存入的数据项
        public HashTable(int size) {//初始化
            arraySize=size;
            hashArray=new DataItem[arraySize];
            nonItem=new DataItem(-1);
        }
        //打印
        public void displayTable() {
            System.out.print("table:");
            for(int j=0;j<arraySize;j++)
                if(hashArray[j]!=null)
                    System.out.print(hashArray[j].getkey()+" ");
                else//如果没有值,就打印**
                    System.out.print("** ");
        }
        //哈希化
        public int hashFunc(int key) {
            return key%arraySize;
        }
        //插入
        public void insert(DataItem item) {
            int key=item.getkey();
            int hashVal=hashFunc(key);//哈希化
            //线性探测插入
            while(hashArray[hashVal]!=null &&  hashArray[hashVal].getkey()!=-1)//如果不是空的,也不是删除之后可以插入的
                {hashVal++;//当前位置被占,寻找下一个位置
                 hashVal=hashVal%arraySize;//保证没有超出索引
                }
            hashArray[hashVal]=item;
            
                
            
        }
        //删除(需要判断哈希化后的位置有值.并且key是不是那个值)
        public DataItem delete(int key) {
            int hashVal=hashFunc(key);
            while(hashArray[hashVal]!=null) {
                if(hashArray[hashVal].getkey()==key) {
                    DataItem temp=hashArray[hashVal];
                    hashArray[hashVal]=nonItem;
                    return temp;
                }
                hashVal++;
                hashVal=hashVal%arraySize;
            }
            return null;//没有找到
        }
        //查找
        public DataItem find(int key) {
            int hashVal=hashFunc(key);
            while(hashArray[hashVal]!=null) {
                if(hashArray[hashVal].getkey()==key) {
        
                    return hashArray[hashVal];
                }
                hashVal++;
                hashVal=hashVal%arraySize;
            }
            return null;
        }
        
        
        
        
        
        
        
        
        
        
        
        
    
        
        
    
    }
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Test {
        public static void main(String [] agrs) throws IOException {
            DataItem aDataItem;
            int akey,size,n,keysPerCell;
            System.out.print("Enter:");
            size=getInt();
            System.out.println("初始化:");
            n=getInt();
            keysPerCell=10;
            HashTable theHashTable=new HashTable(size);
            for(int j=0;j<n;j++) {
                akey=(int)(java.lang.Math.random()*keysPerCell*size);
                aDataItem=new DataItem(akey);
                theHashTable.insert(aDataItem);
            }
            while(true) {
                System.out.print("Enter first of show,isnert,delete ,find:");
                char choice=getChar();
                switch(choice){
                    case 's':
                        theHashTable.displayTable();
                        break;
                    case 'i':
                        System.out.print("insert:");
                        akey=getInt();
                        aDataItem=new DataItem(akey);
                        theHashTable.insert(aDataItem);
                        break;
                    case 'd':
                        System.out.println("输入要删除的key");
                        akey=getInt();
                        theHashTable.delete(akey);
                        break;
                    case 'f':
                        System.out.println("输入要查找的key");
                        akey=getInt();
                        aDataItem=theHashTable.find(akey);
                        if(aDataItem!=null)
                            System.out.println("found"+akey);
                        else
                            System.out.println("没有找到");
                        break;
                        default:
                            System.out.println("无效的输入");
                }
            }
            
            
            
        }
        public static String getString() throws IOException{
            InputStreamReader isr=new InputStreamReader(System.in);
            BufferedReader br=new BufferedReader(isr);
            return br.readLine();
        }
        public static char getChar() throws IOException{
            return getString().charAt(0);
        }
        public static int getInt() throws IOException{
            return Integer.parseInt(getString());
        }
    
    }
  • 相关阅读:
    stl-序列式容器
    BFS
    Hash
    二分法
    草稿1
    红黑树的左旋、右旋和颜色变换
    动态规划
    自动驾驶-安全
    二叉树

  • 原文地址:https://www.cnblogs.com/S-Mustard/p/7717264.html
Copyright © 2020-2023  润新知