Skip to content

Commit

Permalink
Added tests to hash table
Browse files Browse the repository at this point in the history
  • Loading branch information
aliaks-ei committed Aug 21, 2023
1 parent 2696dbd commit 9ca3bdc
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 101 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "data-structures",
"version": "1.0.0",
"version": "1.1.0",
"description": "A collection of the most famous data structures implemented in Javascript",
"author": "Alexey Mozheyko",
"author": "Aliaksei Mazheika",
"scripts": {
"test:unit": "jest --silent",
"test:unit": "jest",
"editorconfig:check": "editorconfig-checker --exclude '.git|node_modules|.DS_store'"
},
"devDependencies": {
Expand Down
14 changes: 7 additions & 7 deletions src/binary-search-tree/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class BinarySearchTree implements IBinarySearchTree {
constructor(value: number) {
this.value = value;

this.left = null;
this.left = null;
this.right = null;
}

contains(value?: number): boolean {
public contains(value?: number): boolean {
if (!value) {
return false;
}
Expand All @@ -46,7 +46,7 @@ class BinarySearchTree implements IBinarySearchTree {
}
}

breadthFirstTraversal() {
public breadthFirstTraversal() {
const queue = [this] as BinarySearchTree[];

while (queue.length) {
Expand All @@ -64,7 +64,7 @@ class BinarySearchTree implements IBinarySearchTree {
}
}

depthFirstTraversal(order: DepthFirstTraversalOrder) {
public depthFirstTraversal(order: DepthFirstTraversalOrder) {
if (order === 'pre-order') {
console.log(this.value);
}
Expand All @@ -86,23 +86,23 @@ class BinarySearchTree implements IBinarySearchTree {
}
}

getMaxVal(): number {
public getMaxVal(): number {
if (this.right) {
return this.right.getMaxVal();
}

return this.value;
}

getMinVal(): number {
public getMinVal(): number {
if (this.left) {
return this.left.getMinVal();
}

return this.value;
}

insert(value: number) {
public insert(value: number) {
if (value <= this.value) {
if (!this.left) {
this.left = new BinarySearchTree(value);
Expand Down
11 changes: 11 additions & 0 deletions src/hash-table/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Hash table

A [hash table](https://en.wikipedia.org/wiki/Hash_table) s a data structure that implements an associative array or dictionary. It is an abstract data type that maps keys to values.
A hash table uses a hash function to compute an index, also called a hash code, into an array of buckets or slots, from which the desired value can be found.

A hash table implemented here has following methods:

- `get(key)` - returns value by provided `key`. Returns `null` if no `key` found.
- `set(key, value)` - adds new key/value pair into hash table.
- `remove(key)` - removes key/value pair from hash table by provided `key` and returns `true`. Return `false` if no `key` found.
- `_hash(key)` - private method that transforms `key` into an index.
89 changes: 0 additions & 89 deletions src/hash-table/hash-table.js

This file was deleted.

53 changes: 53 additions & 0 deletions src/hash-table/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
interface IHashTable {
get(key: string): unknown;
set(key: string, value: unknown): void;
remove(key: string): boolean;
}

class HashTable implements IHashTable {
private table: (Array<unknown> | undefined)[];
public size: number;

constructor(size: number) {
this.table = new Array(size);
this.size = 0;
}

private hash(key: string): number {
let total = 0;

for (let i = 0; i < key.length; i++) {
total += key.charCodeAt(i);
}

return total % this.size;
}

public get(key: string): unknown {
const index = this.hash(key);

return this.table[index]?.[1] ?? null;
}

public set(key: string, value: unknown) {
const index = this.hash(key);

this.table[index] = [key, value];
this.size++;
}

public remove(key: string) {
const index = this.hash(key);

if (this.table[index]?.length) {
this.table[index] = undefined;
this.size--;

return true;
}

return false;
}
}

export default HashTable;
34 changes: 34 additions & 0 deletions src/hash-table/tests/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import HashTable from '..';

/** Test cases on get() and set() method */
describe('HashTable:', () => {
describe('get() and set() methods', () => {
test('should get correct values', () => {
const hashTable = new HashTable(3);

hashTable.set("Canada", 300);
hashTable.set("France", 100);
hashTable.set("Spain", 110);

expect(hashTable.get("Canada")).toBe(300);
expect(hashTable.get("France")).toBe(100);
expect(hashTable.get("Spain")).toBe(110);
expect(hashTable.get("Germany")).toBe(null);
});
});

describe('remove() methods', () => {
test('should remove correct values', () => {
const hashTable = new HashTable(3);

hashTable.set("Andrew", 300);
hashTable.set("Florence", 100);
hashTable.set("Paul", 110);

expect(hashTable.remove("Canada")).toBeTruthy();
expect(hashTable.remove("Florence")).toBeTruthy();
expect(hashTable.remove("Paul")).toBeTruthy();
expect(hashTable.remove("Kate")).toBeFalsy();
});
});
});
4 changes: 2 additions & 2 deletions src/linked-list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class ListNode {

constructor(value: unknown, next: ListNode | null, prev: ListNode | null) {
this.value = value;
this.next = next;
this.prev = prev;
this.next = next;
this.prev = prev;
}
}

Expand Down

0 comments on commit 9ca3bdc

Please sign in to comment.