Hash table (novice article, do not enter) data structures

Basic understanding

This is a one-to-school is different from before, it does not have much logic, indexed by keywords

Hash function

Encryption can be used to obtain data from a small place large volumes of data

Separation Law Links

Linked lists, hash to the same value of the elements remain in the same linked list

/**
 * 
 */
package little_class;

import java.util.LinkedList;
import java.util.List;

/**     1
 * @author 梅肺 
 */
public class SeperateChiningHashTable<E> {
    public SeperateChiningHashTable() {
        // TODO Auto-generated constructor stub
        this(DEFAULT_TABLE_SIZE);
    }
    public SeperateChiningHashTable(int size){
        theLists = new LinkedList[nextPrime(size)]; //???在是什么构造方法   
        for (int i = 0; i < theLists.length; i++) {
            theLists[i] = new LinkedList<>();       
        }
    }
    public void insert(E x) {
        List<E> whichList = theLists[myhash(x)];
        if(!whichList.contains(x)) {//如果不存在就插入,存在就算了

            whichList.add(x);
            
            if (++currentSize > theLists.length) {
                rehash();//哈希表不够大的时候,扩建

            }
        }
    }
    
    public void remove(E x) {
        List<E> whichList = theLists[myhash(x)];
        if(!whichList.contains(x)) {
            whichList.remove(x);
        currentSize--;//不要忘记当前大小减小
        }
        
    }
    
    public boolean contains(E x) {
        List<E> whichList = theLists[myhash(x)];//数组中寻找列表
        return whichList.contains(x);//列表中寻找元素
        
    }
    
    public void makeEmpty() {
        for (int i = 0; i < theLists.length; i++) {
            theLists[i].clear();
        }
        currentSize =0;
    }
    
    private static final int DEFAULT_TABLE_SIZE = 101;
    
    private List<E> [] theLists;//由许多列组成的数组
    private int currentSize;
    
    private void rehash() {
        
    }
    
    private int myhash(E x) {
        int hashval  = x.hashCode();//每一个数据类型都有哈希值
        hashval %= theLists.length;
        if (hashval <0) {
            hashval +=theLists.length;          
        }
        return hashval;
    }
    
}

Linear detection method

Such methods, and the maximum difference separation method wherein the link - the former load factor (ratio of number of elements in the table size) reaches 1, each adding a new element need to assign a new address. The latter is only 0.5 load factor, it can be to find a new cell for the new element.

One drawback: even if the load factor is relatively small table, a gathering will take place, this will lead to any keyword hash table requires several tests to resolve conflict

Square detection method

To remedy the drawbacks mentioned above, but there are some harsh conditions.

  1. Table filled half smaller than a certain position, or equal to half. Even one more are likely to fail
  2. The size of the table must be a prime number, otherwise it will waste a lot of space

Guess you like

Origin www.cnblogs.com/xiaolongdejia/p/11367754.html