-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from aliaks-ei/migrate_to_typescript
Migrate repo to Typescript
- Loading branch information
Showing
28 changed files
with
3,248 additions
and
3,253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/'], | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...binary-search-tree/tests/contains.spec.js → ...binary-search-tree/tests/contains.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.