File tree 3 files changed +71
-2
lines changed
3 files changed +71
-2
lines changed Original file line number Diff line number Diff line change
1
+ class HashTable {
2
+ constructor ( ) {
3
+ this . table = new Array ( 137 ) ;
4
+ this . values = new Array ( 137 ) ;
5
+ }
6
+
7
+ betterHash ( word ) {
8
+ let total = 0 ;
9
+ for ( let i = 0 ; i < word . length ; i ++ )
10
+ total = 37 * total + word . toLowerCase ( ) . charCodeAt ( i ) ;
11
+ return total % this . table . length ;
12
+ }
13
+
14
+ put ( key , value ) {
15
+ const pos = this . betterHash ( key ) ;
16
+ while ( this . table [ pos ] != undefined )
17
+ pos ++ ;
18
+ this . table [ pos ] = key ;
19
+ this . values [ pos ] = value ;
20
+ }
21
+
22
+ get ( key ) {
23
+ const hash = this . betterHash ( key ) ;
24
+ for ( let i = hash ; this . table [ hash ] != undefined ; i ++ )
25
+ if ( this . table [ hash ] == key )
26
+ break ;
27
+ return this . table [ hash ] ;
28
+ }
29
+
30
+ showAll ( ) {
31
+ let str = "" ;
32
+ for ( let i = 0 ; i < this . table . length ; i ++ )
33
+ if ( this . table [ i ] != undefined )
34
+ str += this . values [ i ] + " : " + this . table [ i ] + "\n" ;
35
+ return str ;
36
+ }
37
+ }
38
+
39
+ module . exports = HashTable ;
Original file line number Diff line number Diff line change @@ -34,8 +34,8 @@ class HashTable {
34
34
35
35
showAll ( ) {
36
36
let str = "" ;
37
- for ( let i = 0 ; i < this . table . length && this . table [ i ] != undefined ; i ++ )
38
- for ( let j = 0 ; j < this . table [ i ] . length && this . table [ i ] [ j ] != undefined ; j ++ )
37
+ for ( let i = 0 ; i < this . table . length ; i ++ )
38
+ for ( let j = 0 ; j < this . table [ i ] . length ; j ++ )
39
39
str += i + " : " + this . table [ i ] + "\n" ;
40
40
return str ;
41
41
}
Original file line number Diff line number Diff line change
1
+ const expect = require ( 'chai' ) . expect ;
2
+ const HashTable = require ( '../../examples/datastructures/hash-table-linear-probing' ) ;
3
+
4
+ describe ( '=> HASH TABLE USING LINEAR PROBING' , function ( ) {
5
+ let people ;
6
+ before ( function ( ) {
7
+ people = new HashTable ( ) ;
8
+ } ) ;
9
+
10
+ it ( 'should create an Empty Hash Table' , function ( done ) {
11
+ expect ( people . table . length ) . to . deep . equal ( 137 ) ;
12
+ done ( ) ;
13
+ } ) ;
14
+
15
+ it ( 'should put value into the Hash Table' , function ( ) {
16
+ people . put ( "Diana" , 13 ) ;
17
+ people . put ( "Clayton" , 14 ) ;
18
+ people . put ( "Raymond" , 47 ) ;
19
+ people . put ( "Nadia" , 28 ) ;
20
+ people . put ( "Afzar" , 51 ) ;
21
+ expect ( people . showAll ( ) ) . to . equal ( "13 : Diana\n51 : Afzar\n14 : Clayton\n47 : Raymond\n28 : Nadia\n" ) ;
22
+ } ) ;
23
+
24
+ it ( 'should get value from the Hash Table' , function ( ) {
25
+ expect ( people . get ( "Nadia" ) ) . to . equal ( "Nadia" ) ;
26
+ expect ( people . get ( "Diana" ) ) . to . equal ( "Diana" ) ;
27
+ expect ( people . get ( "Azhar" ) ) . to . equal ( undefined ) ;
28
+ } ) ;
29
+
30
+ } ) ;
You can’t perform that action at this time.
0 commit comments