哈希查找两种实现

链表数组  和  并行数组

拉链法          线性检测法

要点:1、private int hash(key X){

 

  return (x。hashcode() & ox7fffffff) % M;}

  }

 

2、拉链核心:

public SeparateChainingHashST(int M){

  this。M =M;

st= new SqquentialSearchST【M】;

for(int i =0;i<M;i++)

st[i] = new new SqquentialSearchST();

}

 

public Value get(Key key){

  return st【hash(key)】。get(key);

}

 

3、线性检测核心:

public void put(Key key,Value val){

 

  if(N>=M/2)resize(2*M);//将M加倍,M线性检测表的大小

  int i ;

  for(i = hash(key);key[i]!=null; i =(i+1)%M)

    if(keys[i].equals(key))

  return vals[i];

  return null;

  resize(int cap)

{

LinearProbingHashST<key,Value>t;

t = new LLinearProbingHashST<key,Value>(cap);

for(int i = 0 ;i < M;i++)

  t.put(keys[i],vals[i]);

keys=t.keys;

val=t.vals;

M=t.M

}

 

}

posted @ 2015-03-09 22:07  NYK  阅读(161)  评论(0编辑  收藏  举报