Skip to content

Commit

Permalink
✨ (datastructure) added Trie (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
trinhthinh388 authored Jun 18, 2024
1 parent 0df5f50 commit 4bb25de
Show file tree
Hide file tree
Showing 13 changed files with 324 additions and 10 deletions.
7 changes: 7 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# docs

## 0.0.9

### Patch Changes

- Updated dependencies
- @ultify/datastructure@0.4.0

## 0.0.8

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "docs",
"private": true,
"version": "0.0.8",
"version": "0.0.9",
"scripts": {
"build": "next build",
"start": "next start ",
Expand All @@ -12,7 +12,7 @@
"dependencies": {
"@codesandbox/sandpack-react": "^2.14.2",
"@hugeicons/react-pro": "^0.3.2",
"@ultify/datastructure": "0.3.5",
"@ultify/datastructure": "0.4.0",
"classnames": "^2.5.1",
"next": "^14.0.4",
"next-mdx-remote": "^5.0.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/datastructure/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @ultify/datastructure

## 0.4.0

### Minor Changes

- Added Trie

## 0.3.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/datastructure/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ultify/datastructure",
"version": "0.3.5",
"version": "0.4.0",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
8 changes: 6 additions & 2 deletions packages/datastructure/src/heap/heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,13 @@ export class Heap<T> {
* @param values New elements to add to the Heap.
*/
insert(...values: T[]) {
if (values.length === 0) return;
if (values.length === 1) return this.#insertNode(values[0]); // O(logN)
if (values.length === 0) return this;
if (values.length === 1) {
this.#insertNode(values[0]); // O(logN)
return this;
}
this.#insertNodes(values);
return this;
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/datastructure/src/heap/min-heap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ describe('Heap', () => {

const el = heap.extractMin();
expect(el).toBe(6);
console.log(heap.toArray());
expect(heap.toArray()).toEqual([8, 16, 26, 56, 47, 42, 94, 81, 79, 66]);
expect(heap.isValid()).toBe(true);
});
Expand Down
1 change: 1 addition & 0 deletions packages/datastructure/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './stack';
export * from './queue';
export * from './heap';
export * from './trie';
3 changes: 2 additions & 1 deletion packages/datastructure/src/queue/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ export class Queue<T> {
* Returns the top element without removing it
*/
peek() {
return this.#array[0]
return this.#array[0];
}

/**
* Clear queue
*/
clear() {
this.#array = [];
return this;
}
}
3 changes: 2 additions & 1 deletion packages/datastructure/src/stack/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ export class Stack<T> {
* Returns the top element without removing it
*/
peek() {
return this.#array.at(-1)
return this.#array.at(-1);
}

/**
* Clear stack
*/
clear() {
this.#array = [];
return this;
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/datastructure/src/trie/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './trie';
137 changes: 137 additions & 0 deletions packages/datastructure/src/trie/trie.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { Trie } from './trie';

describe('Trie', () => {
describe('constructor', () => {
it('Should construct a trie', () => {
const trie = new Trie();
trie
.insert('abc')
.insert('xyz')
.insert('foo')
.insert('bar')
.insert('atmosphere');
expect(trie.nodeCount).toBe(22);
expect(trie.wordCount).toBe(5);
});

it('Should construct a trie', () => {
const trie = new Trie('abc', 'xyz', 'foo', 'bar');
expect(trie.nodeCount).toBe(13);
expect(trie.wordCount).toBe(4);
});

it('Should construct a trie', () => {
const trie = Trie.fromArray(['abc', 'xyz', 'foo', 'bar']);
expect(trie.nodeCount).toBe(13);
expect(trie.wordCount).toBe(4);
});
});

describe('Search', () => {
it('Should work correctly', () => {
const trie = new Trie();
trie
.insert('abc')
.insert('xyz')
.insert('foo')
.insert('bar')
.insert('atmosphere');
expect(trie.has('abc')).toBe(true);
expect(trie.has('bar')).toBe(true);
expect(trie.has('atmosphere')).toBe(true);
expect(trie.has('gy')).toBe(false);
expect(trie.has('atmospher')).toBe(false);
});
});

describe('Starts with', () => {
it('Should work correctly', () => {
const trie = new Trie();
trie
.insert('abc')
.insert('xyz')
.insert('foo')
.insert('bar')
.insert('atmosphere');
expect(trie.startsWith('abc')).toBe(true);
expect(trie.startsWith('bar')).toBe(true);
expect(trie.startsWith('gy')).toBe(false);
expect(trie.startsWith('atmospher')).toBe(true);
});
});

describe('Iteration', () => {
it('Should be iterable', () => {
const trie = new Trie();
trie
.insert('abc')
.insert('xyz')
.insert('foo')
.insert('bar')
.insert('atmosphere');
expect([...trie]).toContain('abc');
expect([...trie]).toContain('xyz');
expect([...trie]).toContain('foo');
expect([...trie]).toContain('bar');
expect([...trie]).toContain('atmosphere');
});

it('toArray should return new array', () => {
const trie = new Trie();
trie
.insert('abc')
.insert('xyz')
.insert('foo')
.insert('bar')
.insert('atmosphere');
expect(Array.isArray(trie.toArray())).toBe(true);
});
});

describe('Remove', () => {
it('Should work correctly', () => {
const trie = new Trie();
trie
.insert('abc')
.insert('xyz')
.insert('foo')
.insert('bar')
.insert('atmosphere');
trie.remove('atmosphere');
expect([...trie]).toContain('abc');
expect([...trie]).toContain('xyz');
expect([...trie]).toContain('foo');
expect([...trie]).toContain('bar');
expect([...trie]).not.toContain('atmosphere');
expect(trie.nodeCount).toBe(13);
expect(trie.wordCount).toBe(4);

trie.remove('b');
expect([...trie]).toContain('abc');
expect([...trie]).toContain('xyz');
expect([...trie]).toContain('foo');
expect([...trie]).toContain('bar');
expect(trie.nodeCount).toBe(13);
expect(trie.wordCount).toBe(4);

trie.remove('k');
expect([...trie]).toContain('abc');
expect([...trie]).toContain('xyz');
expect([...trie]).toContain('foo');
expect([...trie]).toContain('bar');
expect(trie.nodeCount).toBe(13);
expect(trie.wordCount).toBe(4);
});
});

describe('Clear', () => {
it('Should clear all nodes', () => {
const trie = new Trie('abc', 'xyz', 'foo', 'bar');
expect(trie.nodeCount).toBe(13);
expect(trie.wordCount).toBe(4);
trie.clear();
expect(trie.nodeCount).toBe(1);
expect(trie.wordCount).toBe(0);
});
});
});
Loading

0 comments on commit 4bb25de

Please sign in to comment.