鏈地址法處理Hash衝突

   哈希表中的每個位置稱爲桶(bucket),當發生哈希衝突時就以鏈表形式存放多個元素。


鏈地址法處理Hash衝突,看看下面代碼,模擬了JDK中的HashSet:
Java代碼  收藏代碼
  1. class  Node{ //節點數據結構    
  2.     private  Object value; //節點的值    
  3.     private  Node next; //鏈表中指向下一結點的引用    
  4.   
  5.     /*提供了常見的操作*/    
  6.     public  Node(Object value){ this .value = value;};   
  7.     public  Object getValue() { return  value;}   
  8.     public  Node getNext() { return  next;}   
  9.     public   void  setNext(Node next){ this .next=next;}   
  10. }   
  11.   
  12. public   class  MyHashSet { //Hash數據結構    
  13.     private  Node[] array; //存儲數據鏈表的數組    
  14.     private   int  size =  0 ; //表示集合中存放的對象的數目    
  15.     public  MyHashSet( int  length){   
  16.         array = new  Node[length]; //創建數組    
  17.     }   
  18.   
  19.     public   int  size(){  
  20.      return  size;  
  21.     }  
  22.    
  23.     private   static   int  hash (Object o){     //根據對象的哈希碼得到一個優化的哈希碼,    
  24.                                         //算法參照java.util.HashMap的hash()方法    
  25.         int  h = o.hashCode();   
  26.         h += ~(h<<9 );   
  27.         h ^= (h>>>14 );   
  28.         h += (h<<4 );   
  29.         h ^= (h>>>10 );   
  30.         return  h;   
  31.     }  
  32.    
  33.     private   int  indexFor( int  hashCode){     //根據Hash碼得到其索引位置    
  34.                                         //算法參照java.util.HashMap的indexFor()方法    
  35.         return  hashCode & (array.length- 1 );   
  36.     }   
  37.   
  38.     public   void  add(Object value) { //把對象加入集合,不允許加入重複的元素    
  39.         int  index = indexFor(hash(value)); //先根據value得到index    
  40.         System.out.println("index:"  + index +  " value:"  + value);   
  41.         Node newNode = new  Node(value); //由value創建一個新節點newNode    
  42.         Node node = array[index];//由index得到一個節點node    
  43.   
  44.         if  (node ==  null ) { //若這個由index得到的節點是空,則將新節點放入其中    
  45.             array[index]=newNode;   
  46.             size++;   
  47.         } else  {  
  48. //若不爲空則遍歷這個點上的鏈表(下一個節點要等於空或者該節點不等於新節點的值--不允許重複)    
  49.         Node nextNode;   
  50.        while  (!node.getValue().equals(value) && (nextNode = node.getNext())!= null ) {   
  51.                 node = nextNode;   
  52.             }   
  53.             if  (!node.getValue().equals(value)) {  
  54.              //若值不相等則加入新節點    
  55.                 node.setNext(newNode);   
  56.                 size++;   
  57.             }   
  58.         }   
  59.     }   
  60.   
  61.   
  62.     public   boolean  contains(Object value){   
  63.         int  index = indexFor(hash(value));   
  64.         Node node = array[index];   
  65.         while  (node!= null  && !node.getValue().equals(value)) {   
  66.             node = node.getNext();   
  67.         }//橫向查找    
  68.         if  (node!= null  && node.getValue().equals(value)) {   
  69.             return   true ;   
  70.         } else  {   
  71.             return   false ;   
  72.         }   
  73.     }   
  74.   
  75.     public   boolean  remove(Object value) {   
  76.         int  index = indexFor(hash(value));   
  77.         Node node = array[index];   
  78.         if  (node!= null  && node.getValue().equals(value)) { //若是第一個節點就是要remove的    
  79.             array[index]=node.getNext();   
  80.             size--;   
  81.             return   true ;   
  82.         }   
  83.         Node lastNode = null ;   
  84.         while  (node!= null  && !node.getValue().equals(value)) { //若不是第一個節點就橫向查找    
  85.             lastNode = node;//記錄上一個節點    
  86.             node = node.getNext();   
  87.         }   
  88.         if  (node!= null  && node.getValue().equals(value)) {   
  89.             lastNode.setNext(node.getNext());   
  90.             size--;   
  91.             return   true ;   
  92.         }else  {   
  93.             return   false ;   
  94.         }   
  95.     }   
  96.   
  97.     public  Object[] getAll() {   
  98.         Object [] values = new  Object[size];   
  99.         int  index =  0 ;   
  100.         for  ( int  i =  0 ; i < array.length; i++) {   
  101.             Node node = array[i];   
  102.             while  (node!= null ) {   
  103.                 values[index++]=node.getValue();   
  104.                 node = node.getNext();   
  105.             }      
  106.         }   
  107.         return  values;      
  108.     }   
  109.     public   static   void  main(String[] args) {   
  110.         MyHashSet set = new  MyHashSet( 6 );   
  111.         Object [] values = {"Tom" , "Mike" , "Mike" , "Jack" , "Mary" , "Linda" , "Rose" , "Jone" };   
  112.         for  ( int  i =  0 ; i < values.length; i++) {   
  113.             set.add(values[i]);   
  114.         }   
  115.         set.remove("Mary" );   
  116.         System.out.println("size=" +set.size());   
  117.         values = set.getAll();   
  118.         for  ( int  i =  0 ; i < values.length; i++) {   
  119.             System.out.println(values[i]);   
  120.         }   
  121.         System.out.println(set.contains("Jack" ));   
  122.         System.out.println(set.contains("Linda" ));   
  123.         System.out.println(set.contains("Jane" ));   
  124.     }   
  125. }   



結果:
index:4 value:Tom
index:1 value:Mike
index:1 value:Mike
index:5 value:Jack
index:5 value:Mary
index:0 value:Linda
index:0 value:Rose
index:0 value:Jone
size=6
Linda
Rose
Jone
Mike
Tom
Jack
true
true
false
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章