{"id":16157967,"url":"https://github.com/can-dy-jack/heap","last_synced_at":"2025-07-25T16:33:55.101Z","repository":{"id":65253667,"uuid":"587587271","full_name":"can-dy-jack/heap","owner":"can-dy-jack","description":"MinHeap, MaxHeap, minHeapSort and maxHeapSort implementation in JavaScript.","archived":false,"fork":false,"pushed_at":"2023-01-14T07:06:48.000Z","size":65,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-09T04:13:33.547Z","etag":null,"topics":["data-structures","heap","heap-sort","javascript","maxheap","minheap"],"latest_commit_sha":null,"homepage":"","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/can-dy-jack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2023-01-11T05:08:56.000Z","updated_at":"2023-03-07T03:47:14.000Z","dependencies_parsed_at":"2023-01-15T23:00:41.974Z","dependency_job_id":null,"html_url":"https://github.com/can-dy-jack/heap","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/can-dy-jack/heap","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/can-dy-jack%2Fheap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/can-dy-jack%2Fheap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/can-dy-jack%2Fheap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/can-dy-jack%2Fheap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/can-dy-jack","download_url":"https://codeload.github.com/can-dy-jack/heap/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/can-dy-jack%2Fheap/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267030593,"owners_count":24024230,"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-07-25T02:00:09.625Z","response_time":70,"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":["data-structures","heap","heap-sort","javascript","maxheap","minheap"],"created_at":"2024-10-10T01:51:41.428Z","updated_at":"2025-07-25T16:33:55.044Z","avatar_url":"https://github.com/can-dy-jack.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# heap\n\n\u003e `堆` 在大部分编程语言中，都已经有内置方法实现它，但似乎JS并没有。\n\u003e\n\u003e 最大堆和最小堆：用于高效快速地取得当前数据集中最大或者最小的元素\n\n\u003e The default initial size of heap is 0.  \n\n\u003c!-- \n插入：找到第一个空子节点，插入，然后与父节点不断替换，直到符合特点\n删除：找到最后一个子节点，放到根上，然后与子节点不断替换，直到符合特点\n\n0 处为个数\n父节点：n/2\n左节点：n*2\n右节点：n*1+1\n叶子节点个数：索引大于 n/2 的都是 \n --\u003e\n\n## install\n```sh\nnpm i @kartjim/heap\n```\n## require\n```js\nconst {\n    MaxHeap,\n    MinHeap,\n    minHeapSort,\n    maxHeapSort\n} = require('@kartjim/heap');\n```\n\n## import \n```js\nimport {\n    MaxHeap,\n    MinHeap,\n    minHeapSort,\n    maxHeapSort\n} from '@kartjim/heap';\n```\n\n## HeapSort\n### maxHeapSort\n\u003e sort the array using MaxHeap (from maximum to minimum).\n\n时间复杂度： $O(log N)$ \n\n```js\nconst arr = [12, 668, 1, 0, 4, 67];\nmaxHeapSort(arr) // [668, 67, 12, 4, 1, 0]\n```\n### minHeapSort\n\u003e sort the array using MaxHeap (from minimum to maximum).\n\n时间复杂度： $O(log N)$ \n\n```js\nconst arr = [12, 668, 1, 0, 4, 67];\nminHeapSort(arr) // [0, 1, 4, 12, 67, 668]\n```\n\n## MaxHeap API\n### use\nconstructor \n\n时间复杂度： $O(N)$    \n空间复杂度： $O(N)$  \n```js\nconst heap = new MaxHeap(4);\n```\n\n### push\n\u003e add a new element to the MaxHeap.  \n\n时间复杂度： $O(log N)$  \n空间复杂度： $O(1)$\n```js\nheap.push(1);\nheap.push(2);\nheap.push(3);\n```\n\n### peek\n\u003e return the max element in the MaxHeap.  \n\n时间复杂度： $O(1)$。  \n空间复杂度： $O(1)$。\n```js\nheap.peek() // 3\n```\n\n### pop\n\u003e remove the max element in the MaxHeap.  \n\n时间复杂度： $O(log N)$      \n空间复杂度： $O(1)$\n```js\nheap.pop() // 3\n```\n\n### getSize\n\u003e return the size of the MaxHeap.\n\n```js\nheap.getSize() // 2\n```\n### isEmpty\n\u003e check if the MaxHeap is empty\n\n```js\nheap.isEmpty() // false\n```\n### isFull\n\u003e check if the MaxHeap is full\n\n```js\nheap.isFull() // true\n```\n### MaxHeap.heapify\n\u003e create a MaxHeap from a Array.\n\n```js\nconst t = MaxHeap.heapify([1, 2, 3, 4]);\nt.peek() // 4\n```\n\n## MinHeap API\n### use\nconstructor \n\n时间复杂度： $O(N)$    \n空间复杂度： $O(N)$  \n```js\nconst heap = new MinHeap(4);\n```\n\n### push\n\u003e add a new element to the MinHeap.  \n\n时间复杂度： $O(log N)$  \n空间复杂度： $O(1)$\n```js\nheap.push(1);\nheap.push(2);\nheap.push(3);\n```\n\n### peek\n\u003e return the max element in the MinHeap.  \n\n时间复杂度： $O(1)$。  \n空间复杂度： $O(1)$。\n```js\nheap.peek() // 1\n```\n\n### pop\n\u003e remove the max element in the MinHeap.  \n\n时间复杂度： $O(log N)$      \n空间复杂度： $O(1)$\n```js\nheap.pop() // 1\n```\n\n### getSize\n\u003e return the size of the MinHeap.\n\n```js\nheap.getSize() // 2\n```\n### isEmpty\n\u003e check if the MinHeap is empty\n\n```js\nheap.isEmpty() // false\n```\n### isFull\n\u003e check if the MinHeap is full\n\n```js\nheap.isFull() // true\n```\n### MaxHeap.heapify\n\u003e create a MinHeap from a Array.\n\n```js\nconst t = MaxHeap.heapify([1, 2, 3, 4]);\nt.peek() // 1\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcan-dy-jack%2Fheap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcan-dy-jack%2Fheap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcan-dy-jack%2Fheap/lists"}