哈希表-再哈希法插入删除

二次探测:每次加的步长随机或者是一定规律的数值
再哈希法:哈希化有两个,第二个哈希化不能输出0,不能与第一个哈希化相同
第二个哈希化得到的结果是当前冲突时,索引需要加的值,即是在二次探测的基础上将步长的改进

//数据项(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;//标志删除之后,该位置存入的数据项(-1)
    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("** ");
        System.out.println();
    }
    //哈希化
    public int hashFunc(int key) {
        return key%arraySize;
    }
    //第二次哈希化
    public int hashFunc2(int key) {
        //constant-(key%constant) constant为质数
        return 5-key%5; //key%5 会小于5,那么5-key%5肯定大于0
    }
    //插入
    public void insert(int key,DataItem item) {
        int hashVal=hashFunc(key);//第一次哈希化
        int stepSize=hashFunc2(key);//再哈希得到步长
        //再哈希法插入
        while(hashArray[hashVal]!=null &&  hashArray[hashVal].getkey()!=-1)//如果不是空的,也不是删除之后可以插入的
            {hashVal+=stepSize;//当前位置被占,寻找下一个位置(再哈希法) 
             hashVal=hashVal%arraySize;//保证没有超出索引
            }
        hashArray[hashVal]=item;
        
            
        
    }
    //删除(需要判断哈希化后的位置有值.并且key是不是那个值)
    public DataItem delete(int key) {
        int hashVal=hashFunc(key);//哈希化
        int stepSize=hashFunc2(key);//第二次哈希化
        while(hashArray[hashVal]!=null) {//不等于空
            if(hashArray[hashVal].getkey()==key) {//数据项相同
                DataItem temp=hashArray[hashVal];//要删除的数据项
                hashArray[hashVal]=nonItem;
                return temp;
            }
            hashVal+=stepSize;//数据项不同
            hashVal=hashVal%arraySize;
        }
        return null;//没有找到,第一次就是空的,或者再哈希法也没有找到
    }
    //查找
    public DataItem find(int key) {
        int hashVal=hashFunc(key);
        int stepSize=hashFunc2(key);//第二次哈希化
        while(hashArray[hashVal]!=null) {
            if(hashArray[hashVal].getkey()==key) {
    
                return hashArray[hashVal];
            }
            hashVal+=stepSize;
            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;
        System.out.print("Enter:");
        size=getInt();
        System.out.println("初始化:");
        n=getInt();
        HashTable theHashTable=new HashTable(size);
        for(int j=0;j<n;j++) {
            akey=(int)(java.lang.Math.random()*2*size);
            aDataItem=new DataItem(akey);
            theHashTable.insert(akey,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(akey,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());
    }

}

 

posted @ 2017-10-24 14:52  S-Mustard  阅读(2543)  评论(0编辑  收藏  举报