數據結構-哈希表

哈希衝突的解決方法 鏈地址法

在Java8開始,當哈希衝突達到必定的程度,每個位置從鏈表轉化爲紅黑樹。性能

時間複雜度分析

clipboard.png

哈希表的動態空間處理

  1. 平均每一個地址承載的元素多過必定程度,即擴容(N/M >= upperTol)
  2. 平均每一個地址承載的元素少過必定程度,即縮容(N/M <= lowerTol)

哈希表複雜度分析

clipboard.png

剛開始咱們在擴容的時候直接是2*M,它可能形成擴容後的哈希表分佈不均勻,能夠按着下面這個表格來設置M值。this

clipboard.png

代碼實現

public class HashTable<K, V> {

    private final int[] capacity
        = {53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
        49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469, 
        12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 805306457, 1610612741 };
    
    private static final int upperTol = 10;
    private static final int lowerTol = 2;
    private static final int initCapacity = 7;
    private int CapacityIndex = 0;
    
    private TreeMap<K, V>[] hashtable;
    private int size;
    private int M;    //hash表的長度,即具體有多少個位置(選擇一個合適的素數)

    public HashTable(){
        //this.M = M;
        this.M = capacity[CapacityIndex];
        size = 0;
        hashtable = new TreeMap[M];
        for(int i = 0 ; i < M ; i ++)
            hashtable[i] = new TreeMap<>();
    }

    /*public HashTable(){
        this(initCapacity);
    }*/

    private int hash(K key){
        //key.hashCode() & 0x7fffffff 取key.hashCode()的絕對值
        return (key.hashCode() & 0x7fffffff) % M;
    }

    public int getSize(){
        return size;
    }

    public void add(K key, V value){
        TreeMap<K, V> map = hashtable[hash(key)];
        if(map.containsKey(key))    //修改
            map.put(key, value);
        else{                        //添加
            map.put(key, value);
            size ++;
            
            if(size >= upperTol * M && CapacityIndex+1 < capacity.length)    //即size除以M >=upperTol
                //resize(2 * M);
                CapacityIndex ++;
                resize(capacity[CapacityIndex]);
        }
    }

    public V remove(K key){
        V ret = null;
        TreeMap<K, V> map = hashtable[hash(key)];
        if(map.containsKey(key)){
            ret = map.remove(key);
            size --;
            
            if(size < lowerTol * M && CapacityIndex-1 >= 0)
                CapacityIndex --;
                //resize(M / 2);
                resize(capacity[CapacityIndex]);
        }
        return ret;
    }

    public void set(K key, V value){
        TreeMap<K, V> map = hashtable[hash(key)];
        if(!map.containsKey(key))
            throw new IllegalArgumentException(key + " doesn't exist!");

        map.put(key, value);
    }

    public boolean contains(K key){
        return hashtable[hash(key)].containsKey(key);
    }

    public V get(K key){
        return hashtable[hash(key)].get(key);
    }
    
    private void resize(int newM){
        TreeMap<K, V>[] newHashTable = new TreeMap[newM];
        for(int i = 0 ; i < newM ; i ++)
            newHashTable[i] = new TreeMap<>();
            
        //因爲在hash()方法中有對M進行操做,在往新哈希表中存數據時應該用newM計算hash相應的hash值
        int oldM = M;
        this.M = newM;
        
        for(int i = 0 ; i < oldM ; i ++){
            TreeMap<K, V> map = hashtable[i];
            for(K key: map.keySet())
                newHashTable[hash(key)].put(key, map.get(key));
        }

        this.hashtable = newHashTable;
    }
}

哈希表的均攤複雜度爲O(1),有這麼好的性能其中一個緣由是它犧牲了順序性spa

相關文章
相關標籤/搜索