data structure: static linked list / hash linking / an array based linked list

 

本來是對 CollisionCheckStack 數據結構的探究。

  • Push/pop operation.
  • Duplicate check. When an object that's already in the stack is pushed, this class will tell you so.

push/pop operation


使用一個數組,在數組尾端(tail)進行操作即可

 

 

Duplicate check

尋找一個元素,有如下幾種策略

  • loop all the element to find
  • sort the elements, using binaray search
    • B-tree
  • build index, use the index to find
    • use hashmap

CollisionCheckStack 使用hashmap來快速定位。我們知道hashmap最簡單的實現方式是chain , 而chain最簡單的實現方式是Linked list

 

Kohsuke Kawaguchi 採用的是array based linked list或者wiki中所說的hash linking

 

The link fields need not be physically part of the nodes. If the data records are stored in an array and referenced by their indices, the link field may be stored in a separate array with the same indices as the data records.

 

wiki 中在相關數據結構中提到

 

A hash table may use linked lists to store the chains of items that hash to the same position in the hash table.

A heap shares some of the ordering properties of a linked list, but is almost always implemented using an array. Instead of references from node to node, the next and previous data indexes are calculated using the current data's index.

 

使用數組來實現linked list, (A linked list could be implemented in an array )

 

 

 

上面的Linked list採用三元組表示爲 Data (index) (next node index)

 

                David (0) (4) -> Ioshua (4)  (7)->  Leah (7) (2) -> Miriam (2) (6) -> Robert (6) (-1)

 

其要點

  • 數組中的element有個index屬性,得到index就得到element
  • next[i] 即當前節點(第i個節點)的下一個元素在數組中的index

 

把array based linked list 運用到hash map中就是

 

 

一共使用了三個數組

  • Object[] data, 如前,用來實現stack
  • int[] initialHash, initialHash[i]用來存放hash值爲i的頭節點在data數組中的位置
  • int[] next, next[i]用來存放與data[i]有相同hash值的下一元素在data數組中的位置

Suppose Find(data) return the index of data in the data array

 

Find(d6)的過程如下

      hash(d6) = 1

      initHash(1) = 6

      data[6] == d6, return 6

 

Find(d4)的過程如下

     hash(d4) = 1

     initHash(1) = 6

     data[6] != d4

     next[6] = 4

     data[4] == d4, return 4

 

Find(d3)的過程如下

    hahs(d3) = 1

    initHash(1) = 6

    data[6] != d3

    next[6] = 4

    data[4] != d3

    next[4] = 3

    data[3] == d3, return 3

 

Find(e) 函數如下:

 

insert(e)函數如下:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章