{"id":20745326,"url":"https://github.com/klaudiosinani/mheap","last_synced_at":"2025-10-26T10:39:03.471Z","repository":{"id":57296246,"uuid":"182825880","full_name":"klaudiosinani/mheap","owner":"klaudiosinani","description":"Binary min \u0026 max heaps for ES6","archived":false,"fork":false,"pushed_at":"2025-09-24T09:56:09.000Z","size":143,"stargazers_count":12,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-01T14:54:04.537Z","etag":null,"topics":["binary","es6","heap","max","min","typescript"],"latest_commit_sha":null,"homepage":"http://klaudiosinani.com/mheap","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/klaudiosinani.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":"contributing.md","funding":null,"license":"license.md","code_of_conduct":"code-of-conduct.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-22T16:27:45.000Z","updated_at":"2025-09-26T17:25:48.000Z","dependencies_parsed_at":"2022-09-04T10:03:50.949Z","dependency_job_id":null,"html_url":"https://github.com/klaudiosinani/mheap","commit_stats":null,"previous_names":["klaussinani/mheap"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/klaudiosinani/mheap","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fmheap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fmheap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fmheap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fmheap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/klaudiosinani","download_url":"https://codeload.github.com/klaudiosinani/mheap/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fmheap/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281092780,"owners_count":26442440,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-26T02:00:06.575Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binary","es6","heap","max","min","typescript"],"created_at":"2024-11-17T07:19:43.666Z","updated_at":"2025-10-26T10:39:03.441Z","avatar_url":"https://github.com/klaudiosinani.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n  Mheap\n\u003c/h1\u003e\n\n\u003ch4 align=\"center\"\u003e\n  Binary min \u0026 max heaps for ES6\n\u003c/h4\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://travis-ci.com/klaussinani/mheap\"\u003e\n    \u003cimg alt=\"Build Status\" src=\"https://travis-ci.com/klaussinani/mheap.svg?branch=master\"\u003e\n  \u003c/a\u003e\n  \u003ca href='https://coveralls.io/github/klaussinani/mheap?branch=master'\u003e\n    \u003cimg alt=\"Coverage Status\" src=\"https://coveralls.io/repos/github/klaussinani/mheap/badge.svg?branch=master\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n## Description\n\nES6 implementation of the binary min \u0026 max heap data structures with TypeScript support.\n\nVisit the [contributing guidelines](https://github.com/klaussinani/mheap/blob/master/contributing.md#translating-documentation) to learn more on how to translate this document into more languages.\n\n## Contents\n\n- [Description](#description)\n- [Install](#install)\n- [In Depth](#in-depth)\n- [Usage](#usage)\n- [API](#api)\n- [Development](#development)\n- [Related](#related)\n- [Team](#team)\n- [License](#license)\n\n## Install\n\n### Yarn\n\n```bash\nyarn add mheap\n```\n\n### NPM\n\n```bash\nnpm install mheap\n```\n\n## In Depth\n\nA binary heap is a heap data structure that takes the form of a binary tree, defined with two additional constraints:\n\n- **Shape property**: A binary heap is a complete binary tree, that is all levels, except possibly the last one / deepest are fully filled, and if the last level of the tree is not complete, the nodes of that level are filled from left to right.\n\n- **Heap property**: The key stored in each node is either greater than or equal to or less than or equal to the keys in the node's children, according to the maximum \u0026 minimum total orders, respectively.\n\nHeaps, where the parent key is greater than or equal to the child keys are called max-heaps, and those where it is less than or equal to are called min-heaps.\n\nMheap binary min \u0026 max heaps are internally implemented with an array, where nodes are stored by the level order traversal of the heap and the root node is always placed at index 0. This is due to the fact that any binary tree can be stored in an array, but because a binary heap is always a complete binary tree, it can be compactly \u0026 uniquely represented by storing its level order traversal in an array. As a result, no space is required for pointers, instead, the parent and children of each node are found by arithmetic calculations on array indices.\n\n## Usage\n\nMheap exposes a chainable API, that can be utilized through a simple and minimal syntax, allowing you to combine methods effectively.\n\nUsage examples can be also found at the [`test`](https://github.com/klaussinani/mheap/tree/master/test) directory.\n\n```js\n'use strict';\nconst {MaxHeap, MinHeap, Node} = require('mheap');\n\nconst maxHeap = new MaxHeap();\n//=\u003e MaxHeap { data: [] }\n\nmaxHeap.insert(15, 'A');\n//=\u003e MaxHeap { data: [Node { key: 15, value: 'A' }] }\n\nmaxHeap.root;\n//=\u003e Node { key: 15, value: 'A' }\n\nconst node = new Node(15, 'A');\n\nmaxHeap.root.toPair();\n//=\u003e [15, 'A']\n\nmaxHeap.root.key === node.key;\n//=\u003e true\n\nmaxHeap.root.value === node.value;\n//=\u003e true\n\nmaxHeap.insert(10, 'B').insert(5, 'C');\n//=\u003e MaxHeap { data: [\n// Node { key: 15, value: 'A' },\n// Node { key: 10, value: 'B' },\n// Node { key: 5, value: 'C' } ] }\n\nmaxHeap.left(0);\n//=\u003e Node { key: 10, value: 'B' }\n\nmaxHeap.right(0);\n//=\u003e Node { key: 5, value: 'C' }\n\nmaxHeap.insert(7, 'D').insert(8, 'E').insert(2, 'F');\n//=\u003e MaxHeap { data: [\n// Node { key: 15, value: 'A' },\n// Node { key: 10, value: 'B' },\n// Node { key: 5, value: 'C' },}\n// Node { key: 7, value: 'D' },\n// Node { key: 8, value: 'E' },\n// Node { key: 2, value: 'F' } ] }\n\nmaxHeap.search(8);\n//=\u003e Node { key: 8, value: 'E' }\n\nmaxHeap.includes(2);\n//=\u003e true\n\nmaxHeap.includes(100);\n//=\u003e false\n\nmaxHeap.height();\n//=\u003e 2\n\nmaxHeap.indexOf(7);\n//=\u003e 3\n\nmaxHeap.remove(1);\n//=\u003e MaxHeap { data: [\n// Node { key: 15, value: 'A' },\n// Node { key: 8, value: 'E' },\n// Node { key: 5, value: 'C' },\n// Node { key: 7, value: 'D' },\n// Node { key: 2, value: 'F' } ] }\n\nmaxHeap.children(0);\n//=\u003e { left: Node { key: 8, value: 'E' },\n// right: Node { key: 5, value: 'C' } }\n\nmaxHeap.extractMax();\n//=\u003e Node { key: 15, value: 'A' }\n\nmaxHeap.toPairs();\n//=\u003e [ [ 8, 'E' ], [ 7, 'D' ], [ 5, 'C' ], [ 2, 'F' ] ]\n```\n\n## API\n\nThe following documentation holds for both binary max \u0026 min heaps. The below described `heap` instance is used to depict the same methods that are available to both a min and a max heap, without overlooking their above described differences and unique qualities. For dedicated methods to min or max binary heaps, the `min` \u0026 `max` instances are used respectively.\n\n#### heap.`root`\n\n- Return Type: `Node | undefined`\n\nReturns the root node of the heap.\nIf the heap is empty `undefined` is returned.\n\n```js\nheap.insert(10, 'A');\nheap.root;\n// =\u003e Node { key: 10, value: 'A' }\n```\n\n#### heap.`size`\n\n- Return Type: `Number`\n\nReturns the total number of nodes residing in the heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C');\nheap.size;\n// =\u003e 3\n```\n\n#### heap.`childIndices(index)`\n\n- Return Type: `{ left?: Number, right?: Number }`\n\nReturns an object containing the child indices of the parent node corresponding to the given index. Both the given parent index and the returned child indices are relative to the unique level order array representation of the heap. If the parent node is either a full, a partial or leaf node then the returned object will respectively contain both, only one or none of the child indices.\n\n##### **`index`**\n\n- Type: `Number`\n\nNode index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C');\nheap.childIndices(0);\n// =\u003e { left: 1, right: 2 }\nheap.childIndices(1);\n// =\u003e { }\nheap.childIndices(2);\n// =\u003e { }\n```\n\n#### heap.`children(index)`\n\n- Return Type: `{ left?: Node, right?: Node }`\n\nReturns an object containing the children of the parent node corresponding to the given index. If the parent node is either a full, a partial or leaf node then the returned object will respectively contain both, only one or none of the child nodes.\n\n##### **`index`**\n\n- Type: `Number`\n\nNode index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C');\nheap.children(0);\n// =\u003e { left: Node { key:10, value 'B' }, right: Node { key: 5, value 'C' } }\nheap.children(1);\n// =\u003e { }\nheap.children(2);\n// =\u003e { }\n```\n\n#### heap.`clear()`\n\n- Return Type: `Heap`\n\nMutates the heap by removing all residing nodes and returns it empty.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C');\n//=\u003e Heap { data: [\n// Node { key: 15, value: 'A' },\n// Node { key: 10, value: 'B' },\n// Node { key: 5, value: 'C' } ] }\nheap.size;\n//=\u003e 3\nheap.clear();\n//=\u003e Heap { data: [] } }\nheap.size;\n//=\u003e 0\n```\n\n#### heap.`degree(index)`\n\n- Return Type: `Number`\n\nReturns the number of sub-heaps that the node, corresponding to the give index, points to.\n\n##### **`index`**\n\n- Type: `Number`\n\nNode index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C');\nheap.degree(0);\n//=\u003e 2\nheap.degree(1);\n//=\u003e 0\n```\n\n#### heap.`extract(index)`\n\n- Return Type: `Node | undefined`\n\nMutates the binary heap by removing the node, corresponding to the given index, and properly readjusts the heap in order for it to fulfill the two **shape** \u0026 **heap** **properties**. Returns the removed node, if the node is found, or `undefined` if it is not.\n\n##### **`index`**\n\n- Type: `Number`\n\nNode index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D').insert(7, 'E').insert(1, 'F');\n//=\u003e Heap { data: [ \n// Node { key: 15, value: 'A' },\n// Node { key: 10, value: 'B' },\n// Node { key: 5, value: 'C' },\n// Node { key: 8, value: 'D' },\n// Node { key: 7, value: 'E' },\n// Node { key: 1, value: 'F' } ] }\nheap.extract(1);\n//=\u003e Node { key: 10, value: 'B' }\nheap;\n//=\u003e Heap { data: [ \n// Node { key: 15, value: 'A' },\n// Node { key: 8, value: 'D' },\n// Node { key: 5, value: 'C' },\n// Node { key: 1, value: 'F' },\n// Node { key: 7, value: 'E' } ] }\n```\n\n#### maxHeap.`extractMax()`\n\n- Return Type: `Node | undefined`\n\nMutates the binary max heap by removing the node with the greatest key, known as maximum node / root node, and properly readjusts the max heap in order for it to fulfill the two **shape** \u0026 **heap** **properties**. Returns the maximum node, if the heap is not empty, or `undefined` if it is.\n\n```js\nmaxHeap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D').insert(7, 'E').insert(1, 'F');\n//=\u003e MaxHeap { data: [ \n// Node { key: 15, value: 'A' },\n// Node { key: 10, value: 'B' },\n// Node { key: 5, value: 'C' },\n// Node { key: 8, value: 'D' },\n// Node { key: 7, value: 'E' },\n// Node { key: 1, value: 'F' } ] }\nmaxHeap.extractMax();\n//=\u003e Node { key: 15, value: 'A' }\nheap;\n//=\u003e MaxHeap { data: [ \n// Node { key: 10, value: 'B' },\n// Node { key: 8, value: 'D' },\n// Node { key: 5, value: 'C' },\n// Node { key: 1, value: 'F' },\n// Node { key: 7, value: 'E' } ] }\n```\n\n#### minHeap.`extractMin()`\n\n- Return Type: `Node | undefined`\n\nMutates the binary min heap by removing the node with the smallest key, known as minimum node / root node, and properly readjusts the min heap in order for it to fulfill the two **shape** \u0026 **heap** **properties**. Returns the minimum node, if the heap is not empty, or `undefined` if it is.\n\n```js\nminHeap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D').insert(7, 'E').insert(1, 'F');\n//=\u003e MinHeap { data: [ \n// Node { key: 1, value: 'F' },\n// Node { key: 7, value: 'E' },\n// Node { key: 5, value: 'C' },\n// Node { key: 15, value: 'A' },\n// Node { key: 8, value: 'D' },\n// Node { key: 10, value: 'B' } ] }\nminHeap.extractMin();\n//=\u003e Node { key: 1, value: 'F' }\nheap;\n//=\u003e MinHeap { data: [ \n// Node { key: 5, value: 'C' },\n// Node { key: 7, value: 'E' },\n// Node { key: 10, value: 'B' },\n// Node { key: 15, value: 'A' },\n// Node { key: 8, value: 'D' } ] }\n```\n\n#### heap.`fullNodes()`\n\n- Return Type: `Array\u003cNode\u003e`\n\nApplies level order traversal to the heap and stores each traversed full node (node with two non-null children) in an array.\nThe array is returned at the end of the traversal.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C');\nheap.fullNodes();\n//=\u003e [ \n//  Node { key: 15, value: 'A' }\n// ]\n```\n\n#### heap.`height()`\n\n- Return Type: `Number`\n\nReturns the maximum distance of any leaf node from the root. \nIf the heap is empty `-1` is returned.\n\n```js\nheap.insert(15, 'A');\nheap.height();\n// =\u003e 0\nheap.insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.height();\n//=\u003e 2\n```\n\n#### heap.`includes(key)`\n\n- Return Type: `Boolean`\n\nDetermines whether the heap includes a node with a certain `key`, returning `true` or `false` as appropriate.\n\n##### **`key`**\n\n- Type: `Number`\n\nNode `key` to search for.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C');\nheap.includes(10);\n// =\u003e true\nheap.includes(25);\n// =\u003e false\nheap.includes(5);\n// =\u003e true\n```\n\n#### heap.`indexOf(key)`\n\n- Return Type: `Number`\n\nReturns the first index at which the node with the given `key` can be found in the unique level order array representation of the heap. If the node is not present then `-1` is returned.\n\n##### **`key`**\n\n- Type: `Number`\n\nNode `key` to search for.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C');\nheap.indexOf(10);\n// =\u003e 1\nheap.indexOf(25);\n// =\u003e -1\nheap.indexOf(5);\n// =\u003e 2\n```\n\n#### heap.`insert(key, value)`\n\n- Return Type: `Heap`\n\nMutates the heap by inserting a new node at the appropriate location and return the heap itself.\n\n##### **`key`**\n\n- Type: `Number`\n\nCan be any number that will correspond to the `key` of the created node. \n\n##### **`value`**\n\n- Type: `Any`\n\nCan be any value that will stored in the new node.\n\n```js\nheap.insert(15, 'A');\n//=\u003e Heap { data: [ Node { key: 15, value: 'A' } ] }\nheap.insert(10, 'B').insert(5, 'C');\n//=\u003e Heap { data: [ \n// Node { key: 15, value: 'A' },\n// Node { key: 10, value: 'B' },\n// Node { key: 5, value: 'C' } ] }\n```\n\n#### heap.`internalNodes()`\n\n- Return Type: `Array\u003cNode\u003e`\n\nApplies level order traversal to the heap and stores each traversed internal node (non-leaf nodes) in an array.\nThe array is returned at the end of the traversal.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.fullNodes();\n//=\u003e [ \n//  Node { key: 15, value: 'A' },\n//  Node { key: 10, value: 'B' }\n// ]\n```\n\n#### heap.`isEmpty()`\n\n- Return Type: `Boolean`\n\nDetermines whether the heap is empty, returning `true` or `false` as appropriate.\n\n```js\nheap.insert(10, 'A');\nheap.isEmpty();\n//=\u003e false\nheap.clear().isEmpty();\n//=\u003e true\n```\n\n#### heap.`isFullNode(index)`\n\n- Return Type: `Boolean`\n\nDetermines whether the node, corresponding to the given index, is a full node (has two non-null children), returning `true` or `false` as appropriate.\n\n##### **`index`**\n\n- Type: `Number`\n\nNode index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A');\nheap.isFullNode(0)\n//=\u003e false\nheap.insert(10, 'B').insert(5, 'C').isFullNode(0);\n//=\u003e true\n```\n\n#### heap.`isInternalNode(index)`\n\n- Return Type: `Boolean`\n\nDetermines whether the node, corresponding to the given index, is an internal node (has at least one non-null child), returning `true` or `false` as appropriate.\n\n##### **`index`**\n\n- Type: `Number`\n\nNode index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(10, 'A').isInternalNode(0);\n//=\u003e false\nheap.insert(5, 'B').isInternalNode(0);\n//=\u003e true\n```\n\n#### heap.`isLeafNode(index)`\n\n- Return Type: `Boolean`\n\nDetermines whether the node, corresponding to the given index, is a leaf node (has no children), returning `true` or `false` as appropriate.\n\n##### **`index`**\n\n- Type: `Number`\n\nNode index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').isLeafNode(0);\n//=\u003e true\nheap.insert(10, 'B').isLeafNode(0);\n//=\u003e false\n```\n\n#### heap.`isPartialNode(index)`\n\n- Return Type: `Boolean`\n\nDetermines whether the node, corresponding to the given index, is a partial node (has ony one non-null child), returning `true` or `false` as appropriate.\n\n##### **`index`**\n\n- Type: `Number`\n\nNode index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').isPartialNode(0);\n//=\u003e false\nheap.insert(10, 'B').isPartialNode(0);\n//=\u003e true\nheap.insert(5, 'C').isPartialNode(0);\n//=\u003e false\n```\n\n#### heap.`keys()`\n\n- Return Type: `Array\u003cNumber\u003e`\n\nApplies level order traversal to the heap and stores the `key` of each traversed node in an array.\nThe array is returned at the end of the traversal.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\n//=\u003e [ 15, 10, 5, 8 ]\n```\n\n#### heap.`leafNodes()`\n\n- Return Type: `Array\u003cNode\u003e`\n\nApplies level order traversal to the heap and stores each traversed leaf node (node without children) in an array.\nThe array is returned at the end of the traversal.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.leafNodes();\n//=\u003e [ \n//  Node { key: 5, value: 'C' },\n//  Node { key: 8, value: 'D' }\n// ]\n```\n\n#### heap.`left(index)`\n\n- Return Type: `Node | undefined`\n\nReturns the left child of the parent node corresponding to the given index. \nIf the left child does not exist then `undefined` is returned.\n\n##### **`index`**\n\n- Type: `Number`\n\nParent node index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.left(0);\n//=\u003e Node { key: 5, value: 'C' }\nheap.left(1);\n//=\u003e Node { key: 8, value: 'D' }\nheap.left(2);\n//=\u003e undefined\n```\n\n#### heap.`leftIndex(index)`\n\n- Return Type: `Number`\n\nReturns the index of left child, which is equal to `2 * index + 1`, of the parent node corresponding to the given index. Both the given parent index and the returned left child index are relative to the unique level order array representation of the heap.\n\n##### **`index`**\n\n- Type: `Number`\n\nParent node index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.leftIndex(0);\n//=\u003e 1\nheap.leftIndex(1);\n//=\u003e 3\nheap.leftIndex(2);\n//=\u003e 5\n```\n\n#### heap.`levelOrder(fn)`\n\n- Return Type: `Heap`\n\nApplies level-order traversal (breadth-first traversal) to the heap and executes the provided `fn` function on each traversed node without mutating the heap. Returns the heap itself at the end of the traversal.\n\n##### **`fn`**\n\n- Type: `Function`\n\nUnary function to execute on each node.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.levelOrder(node =\u003e console.log(node.key));\n//=\u003e 15\n//=\u003e 10\n//=\u003e 5\n//=\u003e 8\n```\n\n#### heap.`maxChild(index)`\n\n- Return Type: `Node | undefined`\n\nReturns the child, with the greatest `key` value, of the parent node corresponding to the given index. If the parent node is either a partial or a leaf node then the method respectively returns the only child node or `undefined`.\n\n##### **`index`**\n\n- Type: `Number`\n\nParent node index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.maxChild(0);\n//=\u003e Node { key: 10, value: 'B' }\nheap.maxChild(1);\n//=\u003e Node { key: 8, value: 'D' }\nheap.maxChild(2);\n//=\u003e undefined\n```\n\n#### heap.`maxChildIndex(index)`\n\n- Return Type: `Number`\n\nReturns the index of the child, with the greatest `key` value, of the parent node corresponding to the given index.  Both the given parent index and the returned max child index are relative to the unique level order array representation of the heap. If the parent node is either a partial or leaf node then the method respectively returns the index of the only child node or `-1`.\n\n##### **`index`**\n\n- Type: `Number`\n\nParent node index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.maxChildIndex(0);\n//=\u003e 1\nheap.maxChildIndex(1);\n//=\u003e 3\nheap.maxChildIndex(2);\n//=\u003e -1\n```\n\n#### heap.`minChild(index)`\n\n- Return Type: `Node | undefined`\n\nReturns the child, with the smallest `key` value, of the parent node corresponding to the given index. If the parent node is either a partial or a leaf node then the method respectively returns the only child node or `undefined`.\n\n##### **`index`**\n\n- Type: `Number`\n\nParent node index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.minChild(0);\n//=\u003e Node { key: 5, value: 'C' }\nheap.minChild(1);\n//=\u003e Node { key: 8, value: 'D' }\nheap.minChild(2);\n//=\u003e undefined\n```\n\n#### heap.`minChildIndex(index)`\n\n- Return Type: `Number`\n\nReturns the index of the child, with the smallest `key` value, of the parent node corresponding to the given index.  Both the given parent index and the returned min child index are relative to the unique level order array representation of the heap. If the parent node is either a partial or leaf node then the method respectively returns the index of the only child node or `-1`.\n\n##### **`index`**\n\n- Type: `Number`\n\nParent node index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.maxChildIndex(0);\n//=\u003e 2\nheap.maxChildIndex(1);\n//=\u003e 3\nheap.maxChildIndex(2);\n//=\u003e -1\n```\n\n#### heap.`node(index)`\n\n- Return Type: `Node | undefined`\n\nReturns the the node corresponding to the give index. If the node does not exist then `undefined` is returned.\n\n##### **`index`**\n\n- Type: `Number`\n\nNode index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.node(0);\n//=\u003e Node { key: 15, value: 'A' }\nheap.node(2);\n//=\u003e Node { key: 5, value: 'C' }\nheap.node(15);\n//=\u003e undefined\n```\n\n#### heap.`parent(index)`\n\n- Return Type: `Node | undefined`\n\nReturns the parent node of the node corresponding to the given index. \nIf the parent node does not exist then `undefined` is returned.\n\n##### **`index`**\n\n- Type: `Number`\n\nNode index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.parent(0);\n//=\u003e undefined\nheap.parent(1);\n//=\u003e Node { key: 15, value: 'A' }\nheap.parent(3);\n//=\u003e Node { key: 10, value: 'B' }\n```\n\n#### heap.`parentIndex(index)`\n\n- Return Type: `Number`\n\nReturns the index of the parent node, which is equal to `floor((index - 1) / 2)`, of the node corresponding to the given index. Both the given node index and the returned parent index are relative to the unique level order array representation of the heap.\n\n##### **`index`**\n\n- Type: `Number`\n\nNode index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.parentIndex(0);\n//=\u003e -1\nheap.parentIndex(1);\n//=\u003e 0\nheap.parentIndex(3);\n//=\u003e 1\n```\n\n#### heap.`partialNodes()`\n\n- Return Type: `Array\u003cNode\u003e`\n\nApplies level order traversal to the heap and stores each traversed partial node (node only one non-null child) in an array.\nThe array is returned at the end of the traversal.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.partialNodes();\n//=\u003e [ \n//  Node { key: 10, value: 'B' }\n// ]\n```\n\n#### heap.`remove(index)`\n\n- Return Type: `Heap`\n\nMutates the binary heap by removing the node, corresponding to the given index, and properly readjusts the heap in order for it to fulfill the two **shape** \u0026 **heap** **properties**. Returns the heap itself.\n\n##### **`index`**\n\n- Type: `Number`\n\nNode index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D').insert(7, 'E').insert(1, 'F');\n//=\u003e Heap { data: [ \n// Node { key: 15, value: 'A' },\n// Node { key: 10, value: 'B' },\n// Node { key: 5, value: 'C' },\n// Node { key: 8, value: 'D' },\n// Node { key: 7, value: 'E' },\n// Node { key: 1, value: 'F' } ] }\nheap.remove(0);\n//=\u003e Heap { data: [ \n// Node { key: 10,value: 'B' },\n// Node { key: 8, value: 'D' },\n// Node { key: 5, value: 'C' },\n// Node { key: 1, value: 'F' },\n// Node { key: 7, value: 'E' } ] }\n```\n\n#### heap.`right(index)`\n\n- Return Type: `Node | undefined`\n\nReturns the right child node of the parent node corresponding to the given index. \nIf the right child node does not exist then `undefined` is returned.\n\n##### **`index`**\n\n- Type: `Number`\n\nParent node index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.right(0);\n//=\u003e Node { key: 5, value: 'C' }\nheap.right(1);\n//=\u003e undefined\n```\n\n#### heap.`rightIndex(index)`\n\n- Return Type: `rightIndex`\n\nReturns the index of the right child node, which is equal to `2 * index + 2;`, of the parent node corresponding to the given index. Both the given parent node index and the returned right child index are relative to the unique level order array representation of the heap.\n\n##### **`index`**\n\n- Type: `Number`\n\nParent node index relative to the unique level order array representation of the binary heap.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.rightIndex(0);\n//=\u003e 2\nheap.rightIndex(1);\n//=\u003e 4\nheap.rightIndex(2);\n//=\u003e 6\n```\n\n#### heap.`search(key)`\n\n- Return Type: `Node | undefined`\n\nDetermines whether the heap includes a node with a certain `key`, returning the targeted node or `undefined` as appropriate.\n\n##### **`key`**\n\n- Type: `Number`\n\nNode `key` to search for.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.search(10);\n// =\u003e Node { key: 10, value: 'B' }\nheap.search(5);\n// =\u003e Node { key: 5, value: 'C' }\nheap.search(25);\n// =\u003e undefined\n```\n\n#### heap.`toArray()`\n\n- Return Type: `Array\u003cNode\u003e`\n\nApplies level order traversal to the heap and stores each traversed node in an array.\nThe array is returned at the end of the traversal.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.toArray();\n//=\u003e [ \n//  Node { key: 15, value: 'A' },\n//  Node { key: 10, value: 'B' },\n//  Node { key: 5, value: 'C' },\n//  Node { key: 8, value: 'D' }\n// ]\n```\n\n#### heap.`toPairs()`\n\n- Return Type: `Array\u003c[Number, Any]\u003e`\n\nApplies level order traversal to the heap and for each traversed node stores in an array an ordered-pair/2-tuple, where the first element is a `number` corresponding to the `key` of the traversed node, and the last one is a value of type `any`, corresponding to the `value` stored in the traversed node.\nThe array is returned at the end of the traversal.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\nheap.toPairs();\n//=\u003e [ [ 15, 'A' ], [ 10, 'B' ], [ 5, 'C' ], [ 8, 'D' ] ]\n```\n\n#### heap.`update(key, value)`\n\n- Return Type: `Heap`\n\nMutates the heap by inserting a new `value` at the new node corresponding to the given `key`.\nReturns the heap itself.\n\n##### **`key`**\n\n- Type: `Number`\n\nThe number corresponding to the `key` of the existing node. \n\n##### **`value`**\n\n- Type: `Any`\n\nThe new `value` that will be stored in the existing node.\n\n```js\nheap.insert(15, 'A').insert(10, 'B')\n//=\u003e Heap { data: [ \n// Node { key: 15, value: 'A' },\n// Node { key: 10, value: 'A' } ] }\nheap.update(10, 'a').insert(10, 'b');\n//=\u003e Heap { data: [ \n// Node { key: 15, value: 'a' },\n// Node { key: 10, value: 'b' } ] }\n```\n\n#### heap.`values()`\n\n- Return Type: `Array\u003cAny\u003e`\n\nApplies level order traversal to the heap and stores the `value` of each traversed node in an array.\nThe array is returned at the end of the traversal.\n\n```js\nheap.insert(15, 'A').insert(10, 'B').insert(5, 'C').insert(8, 'D');\n//=\u003e [ 'A', 'B', 'C', 'D' ]\n```\n\nAlso available, along with the `MaxHeap` \u0026 `MinHeap` exposed classes, is the `Node` class, mainly useful for testing purposes, since it can be utilized to compare heap nodes. The class has a binary constructor method, with a `key` and a `value` parameter, corresponding to the key and the value stored in the created instance, respectively.\n\n#### node.`key`\n\n- Return Type: `Number`\n\nThe `key` corresponding to the node instance.\n\n```js\nconst {Node} = require('mheap');\n\nconst node = new Node(10, 'A');\n// =\u003e Node { key:10, value: 'A' }\nnode.key;\n//=\u003e 10\n```\n\n#### node.`value`\n\n- Return Type: `Any`\n\nThe value that the node contains.\n\n```js\nconst {Node} = require('mheap');\n\nconst node = new Node(10, 'A');\n// =\u003e Node { key: 10, value: 'A' }\nnode.value;\n//=\u003e 'A'\nnode.value = 'B'\n// =\u003e Node { key: 10, value: 'B' }\n```\n\n#### node.`toPair()`\n\n- Return Type: `[Number, Any]`\n\nReturns an ordered-pair/2-tuple, where the first element is a number corresponding to the `key` of the node, and the last one is a value, that can be of any type, corresponding to the `value` stored in the node.\n\n```js\nconst {Node} = require('mheap');\n\n\nconst node = new Node(5, 'B');\n\nnode.toPair();\n//=\u003e [ 5, 'B' ]\n```\n\n## Development\n\nFor more info on how to contribute to the project, please read the [contributing guidelines](https://github.com/klaussinani/mheap/blob/master/contributing.md).\n\n- Fork the repository and clone it to your machine\n- Navigate to your local fork: `cd mheap`\n- Install the project dependencies: `npm install` or `yarn install`\n- Lint the code and run the tests: `npm test` or `yarn test`\n\n## Related\n\n- [avlbinstree](https://github.com/klaussinani/avlbinstree) - AVL self-balancing binary search trees for ES6\n- [binoheap](https://github.com/klaussinani/binoheap) - Binomial heaps for ES6\n- [binstree](https://github.com/klaussinani/binstree) - Binary search trees for ES6\n- [doublie](https://github.com/klaussinani/doublie) - Doubly circular \u0026 linear linked lists for ES6\n- [dsforest](https://github.com/klaussinani/dsforest) - Disjoint-set forests for ES6\n- [kiu](https://github.com/klaussinani/kiu) - FIFO Queues for ES6\n- [prioqueue](https://github.com/klaussinani/prioqueue) - Priority queues for ES6\n- [shtack](https://github.com/klaussinani/shtack) - LIFO Stacks for ES6\n- [singlie](https://github.com/klaussinani/singlie) - Singly circular \u0026 linear linked lists for ES6\n\n## Team\n\n- Klaus Sinani [(@klaussinani)](https://github.com/klaussinani)\n\n## License\n\n[MIT](https://github.com/klaussinani/mheap/blob/master/license.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklaudiosinani%2Fmheap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fklaudiosinani%2Fmheap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklaudiosinani%2Fmheap/lists"}