File tree 4 files changed +120
-0
lines changed
algorithms/searching/hash-table
4 files changed +120
-0
lines changed Original file line number Diff line number Diff line change
1
+ const Search = require ( "../Search" )
2
+
3
+ class LinearProbingHash extends Search {
4
+ #keyValuePairsCount
5
+ #linearProbingTableSize
6
+ #keys
7
+ #values
8
+
9
+ constructor ( ) {
10
+ super ( )
11
+
12
+ this . #keyValuePairsCount = 0
13
+ this . #linearProbingTableSize = 16
14
+ this . #keys = [ ]
15
+ this . #values = [ ]
16
+ }
17
+
18
+ put ( key , value ) {
19
+ let i
20
+
21
+ for ( i = this . generateHash ( key ) ; ! ! this . #keys[ i ] ; i ++ ) {
22
+ const currentKey = this . #keys[ i ]
23
+
24
+ const areKeysEqual = this . compareValues ( key , currentKey ) === 0
25
+
26
+ if ( areKeysEqual ) {
27
+ this . #values[ i ] = value
28
+ return
29
+ }
30
+ }
31
+
32
+ this . #keys[ i ] = key
33
+ this . #values[ i ] = value
34
+
35
+ this . #keyValuePairsCount++
36
+ }
37
+
38
+ get ( key ) {
39
+ for ( let i = this . generateHash ( key ) ; ! ! this . #keys[ i ] ; i ++ ) {
40
+ const currentKey = this . #keys[ i ]
41
+
42
+ const areKeysEqual = this . compareValues ( key , currentKey ) === 0
43
+
44
+ if ( areKeysEqual ) {
45
+ return this . #values[ i ]
46
+ }
47
+ }
48
+
49
+ return null
50
+ }
51
+ }
52
+
53
+ module . exports = LinearProbingHash
Original file line number Diff line number Diff line change
1
+ const assert = require ( "assert" )
2
+ const LinearProbingHash = require ( "./LinearProbingHash.js" )
3
+
4
+ const linearProbingHash = new LinearProbingHash ( )
5
+
6
+ linearProbingHash . put ( "Guilherme" , 123 )
7
+ linearProbingHash . put ( "Mota" , 431 )
8
+ linearProbingHash . put ( "teste" , 0 )
9
+
10
+ assert . deepEqual ( linearProbingHash . get ( "Guilherme" ) , 123 )
11
+ assert . deepEqual ( linearProbingHash . get ( "NotExistentKey" ) , null )
12
+
13
+
Original file line number Diff line number Diff line change
1
+ const Search = require ( "../Search" )
2
+ const SequentialSearch = require ( "../symbol-table/SequentialSearch" )
3
+
4
+ class SeparateChainingHash extends Search {
5
+ #keyValuePairsCount
6
+ #hashTableSize
7
+ #sequentialSearchTables
8
+
9
+ constructor ( ) {
10
+ super ( )
11
+
12
+ this . #keyValuePairsCount = 0
13
+ this . #hashTableSize = 0
14
+ this . #sequentialSearchTables = [ ]
15
+ }
16
+
17
+ get ( key ) {
18
+ const hash = this . generateHash ( key )
19
+
20
+ const sequentialSearchTable = this . #sequentialSearchTables[ hash ]
21
+
22
+ if ( ! sequentialSearchTable ) {
23
+ return null
24
+ }
25
+
26
+ const value = sequentialSearchTable . get ( key )
27
+
28
+ return value
29
+ }
30
+
31
+ put ( key , value ) {
32
+ const hash = this . generateHash ( key )
33
+
34
+ if ( ! this . #sequentialSearchTables[ hash ] ) {
35
+ this . #sequentialSearchTables[ hash ] = new SequentialSearch ( )
36
+ }
37
+
38
+ this . #sequentialSearchTables[ hash ] . put ( key , value )
39
+ }
40
+ }
41
+
42
+ module . exports = SeparateChainingHash
Original file line number Diff line number Diff line change
1
+ const assert = require ( "assert" )
2
+ const SeparateChainingHash = require ( "./SeparateChainingHash.js" )
3
+
4
+ const separateChainingHash = new SeparateChainingHash ( )
5
+
6
+ separateChainingHash . put ( "Guilherme" , 123 )
7
+ separateChainingHash . put ( "Mota" , 431 )
8
+ separateChainingHash . put ( "teste" , 0 )
9
+
10
+ assert . deepEqual ( separateChainingHash . get ( "Guilherme" ) , 123 )
11
+ assert . deepEqual ( separateChainingHash . get ( "NotExistentKey" ) , null )
12
+
You can’t perform that action at this time.
0 commit comments