Skip to content

Commit

Permalink
Merge pull request #6 from aliaks-ei/migrate_to_typescript
Browse files Browse the repository at this point in the history
Migrate repo to Typescript
  • Loading branch information
aliaks-ei authored Aug 21, 2023
2 parents d0bab71 + 9ca3bdc commit ac3d46d
Show file tree
Hide file tree
Showing 28 changed files with 3,248 additions and 3,253 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/.editorconfig-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
node-version: '16'
- run: npm ci
- run: npm run editorconfig:check
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
node-version: '16'
- run: npm ci
- run: npm run test:unit
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts?$': 'ts-jest',
},
transformIgnorePatterns: ['<rootDir>/node_modules/'],
};
5,957 changes: 2,949 additions & 3,008 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"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": {
"@types/jest": "^29.5.3",
"editorconfig-checker": "^4.0.2",
"jest": "^27.0.6"
"ts-jest": "^29.1.1",
"typescript": "^5.1.6"
}
}
20 changes: 10 additions & 10 deletions src/binary-search-tree/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Binary search tree

A [Binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) is a binary tree in which nodes which have lesser value are stored on the left while the nodes with higher value are stored at the right.
A [Binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) is a binary tree in which nodes with lesser value are stored on the left while the nodes with higher value are stored at the right.

A binary search tree (BST) implemented here has following methods:

- **_contains(value)_** - checks whether BST contains given ```value``` or not. Returns boolean.
- **_breadthFirstTraversal()_** - traverses through one entire level of children nodes first, then moving on to traverse through the grandchildren nodes and so on. Logs all nodes to the console.
- **_depthFirstTraversal(order)_** - traverses through all nodes according to the ```order``` parameter and logs them to the console. Possible values of the ```order``` are:
- _in-order_ - starts traversing the BST from the top and follows every branch down to its bottom before moves on to the next branch.
- _pre-order_ - starts traversing the BST from the top, then goes down the left branch and then goes down the right branch.
- _post-order_ - traverses through all of the left children, then all the right children and then the parent node.
- **_getMaxVal()_** - returns the maximum value of the BST.
- **_getMinVal()_** - returns the minimum value of the BST .
- **_insert(value)_** - inserts given ```value``` into the BST.
- `contains(value)` - checks whether BST contains given `value` or not. Returns boolean.
- `breadthFirstTraversal()` - traverses through one entire level of children nodes first, then moving on to traverse through the grandchildren nodes and so on. Logs all nodes to the console.
- `depthFirstTraversal(order)` - traverses through all nodes according to the `order` parameter and logs them to the console. Possible values of the `order` are:
- `in-order` - starts traversing the BST from the top and follows every branch down to its bottom before moves on to the next branch.
- `pre-order` - starts traversing the BST from the top, then goes down the left branch and then goes down the right branch.
- `post-order` - traverses through all of the left children, then all the right children and then the parent node.
- `getMaxVal()` - returns the maximum value of the BST.
- `getMinVal()` - returns the minimum value of the BST .
- `insert(value)` - inserts given `value` into the BST.
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
class BST {
constructor(value) {
type DepthFirstTraversalOrder = 'pre-order' | 'in-order' | 'post-order';

interface IBinarySearchTree {
contains(value?: number): boolean;
breadthFirstTraversal(): void;
depthFirstTraversal(order: DepthFirstTraversalOrder): void;
getMaxVal(): number;
getMinVal(): number;
insert(value: number): void;
}

class BinarySearchTree implements IBinarySearchTree {
private value: number;
private left: BinarySearchTree | null;
private right: BinarySearchTree | null;

constructor(value: number) {
this.value = value;

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

contains(value) {
public contains(value?: number): boolean {
if (!value) {
return false;
}

if (this.value === value) {
return true;
}
Expand All @@ -27,25 +46,25 @@ class BST {
}
}

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

while (queue.length) {
const treeNode = queue.shift();

console.log(treeNode.value);
console.log(treeNode?.value);

if (treeNode.left) {
if (treeNode?.left) {
queue.push(treeNode.left);
}

if (treeNode.right) {
if (treeNode?.right) {
queue.push(treeNode.right);
}
}
}

depthFirstTraversal(order) {
public depthFirstTraversal(order: DepthFirstTraversalOrder) {
if (order === 'pre-order') {
console.log(this.value);
}
Expand All @@ -67,34 +86,34 @@ class BST {
}
}

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

return this.value;
}

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

return this.value;
}

insert(value) {
public insert(value: number) {
if (value <= this.value) {
if (!this.left) {
this.left = new BST(value);
this.left = new BinarySearchTree(value);
}
else {
this.left.insert(value);
}
}
else {
if (!this.right) {
this.right = new BST(value);
this.right = new BinarySearchTree(value);
}
else {
this.right.insert(value);
Expand All @@ -103,4 +122,4 @@ class BST {
}
}

module.exports = BST;
export default BinarySearchTree;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const BST = require('../binary-search-tree');
import BST from '..';

/** Test suites on contains() method */
describe('Binary search tree: contains() method', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const BST = require('../binary-search-tree');
import BST from '..';

/** Test suites on getMaxVal() method */
describe('Binary search tree: getMaxVal() method', () => {
Expand All @@ -14,15 +14,15 @@ describe('Binary search tree: getMaxVal() method', () => {
expect(bst.getMaxVal()).toBe(99);
});

test('should return null as max value', () => {
test('should return 0 as max value', () => {
const bst = new BST(-3);

bst.insert(-6);
bst.insert(-900);
bst.insert(null);
bst.insert(0);
bst.insert(-1e8);

expect(bst.getMaxVal()).toBe(null);
expect(bst.getMaxVal()).toBe(0);
});

test('should return 1 as max value (BST contains only one value)', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const BST = require('../binary-search-tree');
import BST from '..';

/** Test suites on getMinVal() method */
describe('Binary search tree: getMinVal() method', () => {
Expand All @@ -14,15 +14,15 @@ describe('Binary search tree: getMinVal() method', () => {
expect(bst.getMinVal()).toBe(2);
});

test('should return null as min value', () => {
test('should return 0 as min value', () => {
const bst = new BST(3);

bst.insert(6);
bst.insert(900);
bst.insert(null);
bst.insert(0);
bst.insert(1e8);

expect(bst.getMinVal()).toBe(null);
expect(bst.getMinVal()).toBe(0);
});

test('should return 1 as min value (BST contains only one 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;
Loading

0 comments on commit ac3d46d

Please sign in to comment.