Skip to content

Commit

Permalink
📝 (docs) added MaxHeap docs (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
trinhthinh388 authored Jun 17, 2024
1 parent 32def7f commit 6f6f032
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 5 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.6

### Patch Changes

- Updated dependencies
- @ultify/datastructure@0.3.3

## 0.0.5

### 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.5",
"version": "0.0.6",
"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.2",
"@ultify/datastructure": "0.3.3",
"classnames": "^2.5.1",
"next": "^14.0.4",
"next-mdx-remote": "^5.0.0",
Expand Down
84 changes: 84 additions & 0 deletions docs/src/pages/docs/datastructures/heap/max-heap.mdx
Original file line number Diff line number Diff line change
@@ -1 +1,85 @@
import { StackIcon, QueueIcon } from '@components/icons';
import { Callout } from 'nextra/components';
import { Playground } from '@components/playground';

import { files as constructorFiles } from '@snippets/datastructures/max-heap/constructor';
import { files as fromArray } from '@snippets/datastructures/max-heap/fromArray';
import { files as toArrayFiles } from '@snippets/datastructures/max-heap/toArray';
import { files as insertFiles } from '@snippets/datastructures/max-heap/insert';
import { files as extractMaxFiles } from '@snippets/datastructures/max-heap/extractMax';
import { files as getMaxFiles } from '@snippets/datastructures/max-heap/getMax';
import { files as isValidFiles } from '@snippets/datastructures/max-heap/isValid';

# Max Heap

A **MaxHeap** is a complete binary tree in which the value in each internal node is greater than or equal to the values in the children of that node.
Mapping the elements of a heap into an array is trivial: if a node is stored an index k, then its left child is stored at index 2k + 1 and its right child at index 2k + 2.

<Callout type="info">
Ultify's **MaxHeap** is a wrapper around Javascript array with a standard heap
interface.
</Callout>

## Operations

- **insert**: Adds elements to the heap.
- **extractMax**: Removes and returns the max element from the heap.
- `property` **max**: Returns the max element of the heap without removing it.
- **isValid**: Check ifs the heap is valid.
- **IsEmpty**: Checks if the queue is empty.
- **toArray**: Convert a heap to an array.
- `static` **getLeftChildIndex**: Returns the left child's index of a given index.
- `static` **getRightChildIndex**: Returns the right child's index of a given index.
- `static` **getParentIndex**: Returns the parent's index of a given index.

## APIs

### Constructor

<Callout type="info">
Ultify's **heap** time complexity to create a Heap from an array is O(${n}$).
</Callout>

#### `compareFn`

- **Type**: `<T>(a: T, b: T) => number`
- **Description**: Function used to determine the order of the elements. It is expected to return a negative value if the first argument is less than the second argument, zero if they're equal, and a positive value otherwise.

<Playground files={constructorFiles} />

### fromArray `(static)`

<Callout type="info">
Ultify's **heap** time complexity to create a Heap from an array is O(${n}$).
</Callout>

<Playground files={fromArray} />

### toArray

<Playground files={toArrayFiles} />

### Insert

<Callout type="info">
Ultify's **heap** time complexity to insert a single value to a Heap is
O($\log{n}$).
</Callout>
<Callout type="info">
Ultify's **heap** time complexity to insert multiple values to a Heap is O($
{n}$).
</Callout>

<Playground files={insertFiles} />

### Extract Max

<Playground files={extractMaxFiles} />

### Max

<Playground files={getMaxFiles} />

### Is Valid

<Playground files={isValidFiles} />
16 changes: 16 additions & 0 deletions docs/src/snippets/datastructures/max-heap/constructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const Index = `import { MaxHeap } from '@ultify/datastructure'
// Create an empty heap
const foo = new MaxHeap()
console.log("Empty MaxHeap:", foo.toArray())
// Create a heap from an array
const bar = new MaxHeap([3, 7, 5, 9])
console.log("Max MaxHeap:", bar.toArray())
`;

export const files = {
'/index.js': {
code: Index,
},
};
17 changes: 17 additions & 0 deletions docs/src/snippets/datastructures/max-heap/extractMax.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Index = `import { MaxHeap } from '@ultify/datastructure'
// Create an empty Heap
const foo = new MaxHeap()
foo.insert(1)
foo.insert(2)
foo.insert(67, -7)
console.log(foo.extractMax())
console.log(foo.toArray())
`;

export const files = {
'/index.js': {
code: Index,
},
};
12 changes: 12 additions & 0 deletions docs/src/snippets/datastructures/max-heap/fromArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const Index = `import { MaxHeap } from '@ultify/datastructure'
// Create an empty MaxHeap
const foo = MaxHeap.fromArray([1, 2, 3])
console.log(foo.toArray())
`;

export const files = {
'/index.js': {
code: Index,
},
};
17 changes: 17 additions & 0 deletions docs/src/snippets/datastructures/max-heap/getMax.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Index = `import { MaxHeap } from '@ultify/datastructure'
// Create an empty Heap
const foo = new MaxHeap()
foo.insert(1)
foo.insert(2)
foo.insert(67, -7)
console.log(foo.max)
console.log(foo.toArray())
`;

export const files = {
'/index.js': {
code: Index,
},
};
16 changes: 16 additions & 0 deletions docs/src/snippets/datastructures/max-heap/insert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const Index = `import { MaxHeap } from '@ultify/datastructure'
// Create an empty MaxHeap
const foo = new MaxHeap()
foo.insert(1)
foo.insert(2)
foo.insert(67, -7)
console.log(foo.toArray())
`;

export const files = {
'/index.js': {
code: Index,
},
};
17 changes: 17 additions & 0 deletions docs/src/snippets/datastructures/max-heap/isValid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Index = `import { MaxHeap } from '@ultify/datastructure'
// Create an empty MaxHeap
const foo = new MaxHeap()
foo.insert(1)
foo.insert(2)
foo.insert(67, -7)
console.log(foo.toArray())
console.log(foo.isValid())
`;

export const files = {
'/index.js': {
code: Index,
},
};
13 changes: 13 additions & 0 deletions docs/src/snippets/datastructures/max-heap/toArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Index = `import { MaxHeap } from '@ultify/datastructure'
// Create a MaxHeap
const foo = new MaxHeap([1, 2, 3])
console.log(foo.toArray())
`;

export const files = {
'/index.js': {
code: Index,
},
};
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.3.3

### Patch Changes

- added MaxHeap static methods

## 0.3.2

### 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.2",
"version": "0.3.3",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/datastructure/src/heap/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './heap';
export * from './max-heap';
8 changes: 8 additions & 0 deletions packages/datastructure/src/heap/max-heap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,12 @@ describe('Heap', () => {
expect(heap.toArray()).toEqual([81, 79, 42, 56, 66, 16, 26, 6, 8, 47]);
});
});

describe('Static', () => {
it('Get index', () => {
expect(MaxHeap.getLeftChildIndex(1)).toBe(3);
expect(MaxHeap.getRightChildIndex(1)).toBe(4);
expect(MaxHeap.getParentIndex(1)).toBe(0);
});
});
});
21 changes: 21 additions & 0 deletions packages/datastructure/src/heap/max-heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ export class MaxHeap<T> {
) {
return new MaxHeap(values, compareFn);
}
/**
* Get left child's index of the provided index.
* @param i index of the node
*/
static getLeftChildIndex(i: number) {
return Heap.getLeftChildIndex(i);
}
/**
* Get right child's index of the provided index.
* @param i index of the node
*/
static getRightChildIndex(i: number) {
return Heap.getRightChildIndex(i);
}
/**
* Get parent's index of the provided index.
* @param i index of the node
*/
static getParentIndex(i: number) {
return Heap.getParentIndex(i);
}

/**
* Inserts new elements to the Heap.
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2370,7 +2370,7 @@ __metadata:
languageName: node
linkType: hard

"@ultify/datastructure@npm:0.3.2, @ultify/datastructure@workspace:packages/datastructure":
"@ultify/datastructure@npm:0.3.3, @ultify/datastructure@workspace:packages/datastructure":
version: 0.0.0-use.local
resolution: "@ultify/datastructure@workspace:packages/datastructure"
dependencies:
Expand Down Expand Up @@ -4541,7 +4541,7 @@ __metadata:
"@hugeicons/react-pro": "npm:^0.3.2"
"@types/react": "npm:^18.2.46"
"@types/react-dom": "npm:^18.2.18"
"@ultify/datastructure": "npm:0.3.2"
"@ultify/datastructure": "npm:0.3.3"
"@ultify/eslint-config": "npm:*"
"@ultify/typescript-config": "npm:*"
autoprefixer: "npm:^10.4.15"
Expand Down

0 comments on commit 6f6f032

Please sign in to comment.