{"name":"heap-typed","version":"2.2.8","keywords":["heap","Binary Heap","binary-heap","heap data structure","min heap","max heap","complete binary tree","heapify","heap sort","heapify up","heapify down","Priority Queue","priority queue","priorityqueue","priority-queue","priority q","priorityQ","min","max","efficient","priority","complete binary","extract min","extract max","min priority queue","max priority queue","efficient priority","ordering property","dynamic resizing","priority-based","priority-based processing","sorting","element priority","queue priority","insertion","deletion","javascript","java script","JavaScript","js","typescript","type script","TypeScript","ts","sorted","sort","data","structure","structures","data structure","datastructure","data-structure","data structures","datastructures","data-structures","in data structures","in data structure","DataStructure","DataStructures","parent","child","root","decrease key","increase key","traversal","recursive","iterative","Node.js","CommonJS","ES6","UMD","esmodule","java.util","c++ stl","c++ std","Python collections","System.Collections.Generic","STL","stl","util","collection","Collection","collections","Collections","searching","performance","OOP","documentation","visualization"],"author":{"name":"Pablo Zeng zrwusa@gmail.com"},"license":"MIT","_id":"heap-typed@2.2.8","maintainers":[{"name":"zrwusa.org","email":"zrwusa@gmail.com"}],"homepage":"https://data-structure-typed-docs.vercel.app","bugs":{"url":"https://github.com/zrwusa/data-structure-typed/issues"},"dist":{"shasum":"022549e6dda0e6331a1f535110123bbfbeee3944","tarball":"http://registry-npm.ywwl.com/heap-typed/download/heap-typed-2.2.8.tgz","fileCount":276,"integrity":"sha512-zuYAxK4fP9qUjz40XIOgIatQhY0SpXx3SmRMxrJXrSwv5YvqjZvVna/zwcpE4wfso7iDTu7ONyRdE2H6RqWZww==","signatures":[{"sig":"MEUCICE5z+4y7SpVmHmEKxJT3fzkXk16vDKNq1+ht7FNMDTlAiEAiDmjbezWMtK0447YXhbUK3UVi0kWYCY2POkP/u7mGbg=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":2179434,"size":365891,"noattachment":false,"key":"/heap-typed/-/heap-typed-2.2.8.tgz"},"main":"dist/cjs/index.cjs","types":"dist/types/index.d.ts","module":"dist/esm/index.mjs","browser":"dist/umd/heap-typed.min.js","engines":{"npm":">=6.14.0","node":">=12.20.0"},"exports":{".":{"node":{"import":"./dist/esm/index.mjs","require":"./dist/cjs/index.cjs"},"types":"./dist/types/index.d.ts","import":"./dist/esm-legacy/index.mjs","browser":{"import":"./dist/esm-legacy/index.mjs","require":"./dist/cjs-legacy/index.cjs"},"require":"./dist/cjs-legacy/index.cjs"},"./legacy":{"types":"./dist/types/index.d.ts","import":"./dist/esm-legacy/index.mjs","require":"./dist/cjs-legacy/index.cjs"},"./modern":{"types":"./dist/types/index.d.ts","import":"./dist/esm/index.mjs","require":"./dist/cjs/index.cjs"}},"gitHead":"0ee2ad72fc510f1d81b171e43cf5a9b2f9279563","scripts":{"lint":"eslint --fix \"src/**/*.{js,ts}\"","test":"jest","build":"npm run build:ecu","format":"prettier --write \"src/**/*.{js,ts}\"","build:ecu":"npm run build:node && npm run build:types && npm run build:umd","build:umd":"tsup","build:docs":"typedoc --out docs ./src","build:node":"tsup --config tsup.node.config.js","deps:check":"dependency-cruiser src","build:types":"rm -rf dist/types && tsc -p tsconfig.types.json","build:publish":"npm run build && npm publish"},"_npmUser":{"name":"zrwusa.org","email":"zrwusa@gmail.com"},"umd:main":"dist/umd/heap-typed.min.js","repository":{"url":"git+https://github.com/zrwusa/data-structure-typed.git","type":"git"},"_npmVersion":"10.9.3","description":"Heap","directories":{},"sideEffects":false,"_nodeVersion":"22.19.0","dependencies":{"data-structure-typed":"^2.2.8"},"_hasShrinkwrap":false,"devDependencies":{"jest":"^29.6.2","tsup":"^8.5.1","eslint":"^7.32.0","ts-jest":"^29.1.1","typedoc":"^0.25.1","prettier":"^3.0.3","typescript":"^4.9.5","@types/jest":"^29.5.3","@types/node":"^20.4.9","eslint-plugin-import":"^2.25.4","eslint-config-prettier":"^8.3.0","@typescript-eslint/parser":"^5.11.0","eslint-import-resolver-alias":"^1.1.2","@typescript-eslint/eslint-plugin":"^5.6.0","eslint-import-resolver-typescript":"^2.5.0"},"_npmOperationalInternal":{"tmp":"tmp/heap-typed_2.2.8_1769635949962_0.44823323157759165","host":"s3://npm-registry-packages-npm-production"},"readme":"![NPM](https://img.shields.io/npm/l/heap-typed)\n![GitHub top language](https://img.shields.io/github/languages/top/zrwusa/data-structure-typed)\n![npm](https://img.shields.io/npm/dw/heap-typed)\n![eslint](https://aleen42.github.io/badges/src/eslint.svg)\n![npm bundle size](https://img.shields.io/bundlephobia/minzip/heap-typed)\n![npm bundle size](https://img.shields.io/bundlephobia/min/heap-typed)\n![npm](https://img.shields.io/npm/v/heap-typed)\n\n# What\n\n## Brief\n\nThis is a standalone Heap data structure from the data-structure-typed collection. If you wish to access more data\nstructures or advanced features, you can transition to directly installing the\ncomplete [data-structure-typed](https://www.npmjs.com/package/data-structure-typed) package\n\n# How\n\n## install\n\n### npm\n\n```bash\nnpm i heap-typed --save\n```\n\n### yarn\n\n```bash\nyarn add heap-typed\n```\n\n### snippet\n\n[//]: # (No deletion!!! Start of Example Replace Section)\n\n### Use Heap to solve top k problems\n```typescript\n function topKElements(arr: number[], k: number): number[] {\n      const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap\n      arr.forEach(num => {\n        heap.add(num);\n        if (heap.size > k) heap.poll(); // Keep the heap size at K\n      });\n      return heap.toArray();\n    }\n\n    const numbers = [10, 30, 20, 5, 15, 25];\n    console.log(topKElements(numbers, 3)); // [15, 10, 5];\n```\n\n### Use Heap to dynamically maintain the median\n```typescript\n class MedianFinder {\n      private low: MaxHeap<number>; // Max heap, stores the smaller half\n      private high: MinHeap<number>; // Min heap, stores the larger half\n\n      constructor() {\n        this.low = new MaxHeap<number>([]);\n        this.high = new MinHeap<number>([]);\n      }\n\n      addNum(num: number): void {\n        if (this.low.isEmpty() || num <= this.low.peek()!) this.low.add(num);\n        else this.high.add(num);\n\n        // Balance heaps\n        if (this.low.size > this.high.size + 1) this.high.add(this.low.poll()!);\n        else if (this.high.size > this.low.size) this.low.add(this.high.poll()!);\n      }\n\n      findMedian(): number {\n        if (this.low.size === this.high.size) return (this.low.peek()! + this.high.peek()!) / 2;\n        return this.low.peek()!;\n      }\n    }\n\n    const medianFinder = new MedianFinder();\n    medianFinder.addNum(10);\n    console.log(medianFinder.findMedian()); // 10;\n    medianFinder.addNum(20);\n    console.log(medianFinder.findMedian()); // 15;\n    medianFinder.addNum(30);\n    console.log(medianFinder.findMedian()); // 20;\n    medianFinder.addNum(40);\n    console.log(medianFinder.findMedian()); // 25;\n    medianFinder.addNum(50);\n    console.log(medianFinder.findMedian()); // 30;\n```\n\n### Use Heap for load balancing\n```typescript\n function loadBalance(requests: number[], servers: number): number[] {\n      const serverHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // min heap\n      const serverLoads = new Array(servers).fill(0);\n\n      for (let i = 0; i < servers; i++) {\n        serverHeap.add({ id: i, load: 0 });\n      }\n\n      requests.forEach(req => {\n        const server = serverHeap.poll()!;\n        serverLoads[server.id] += req;\n        server.load += req;\n        serverHeap.add(server); // The server after updating the load is re-entered into the heap\n      });\n\n      return serverLoads;\n    }\n\n    const requests = [5, 2, 8, 3, 7];\n    console.log(loadBalance(requests, 3)); // [12, 8, 5];\n```\n\n### Use Heap to schedule tasks\n```typescript\n type Task = [string, number];\n\n    function scheduleTasks(tasks: Task[], machines: number): Map<number, Task[]> {\n      const machineHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // Min heap\n      const allocation = new Map<number, Task[]>();\n\n      // Initialize the load on each machine\n      for (let i = 0; i < machines; i++) {\n        machineHeap.add({ id: i, load: 0 });\n        allocation.set(i, []);\n      }\n\n      // Assign tasks\n      tasks.forEach(([task, load]) => {\n        const machine = machineHeap.poll()!;\n        allocation.get(machine.id)!.push([task, load]);\n        machine.load += load;\n        machineHeap.add(machine); // The machine after updating the load is re-entered into the heap\n      });\n\n      return allocation;\n    }\n\n    const tasks: Task[] = [\n      ['Task1', 3],\n      ['Task2', 1],\n      ['Task3', 2],\n      ['Task4', 5],\n      ['Task5', 4]\n    ];\n    const expectedMap = new Map<number, Task[]>();\n    expectedMap.set(0, [\n      ['Task1', 3],\n      ['Task4', 5]\n    ]);\n    expectedMap.set(1, [\n      ['Task2', 1],\n      ['Task3', 2],\n      ['Task5', 4]\n    ]);\n    console.log(scheduleTasks(tasks, 2)); // expectedMap;\n```\n\n### Get all elements as array\n```typescript\n const heap = new Heap<number>([5, 1, 3, 2, 4]);\n    const arr = heap.toArray();\n    console.log(arr.length); // 5;\n    console.log(arr.sort()); // [1, 2, 3, 4, 5];\n```\n\n[//]: # (No deletion!!! End of Example Replace Section)\n\n\n\n## API docs & Examples\n\n[API Docs](https://data-structure-typed-docs.vercel.app)\n\n[Live Examples](https://vivid-algorithm.vercel.app)\n\n<a href=\"https://github.com/zrwusa/vivid-algorithm\" target=\"_blank\">Examples Repository</a>\n\n## Data Structures\n\n<table>\n<thead>\n<tr>\n<th>Data Structure</th>\n<th>Unit Test</th>\n<th>Performance Test</th>\n<th>API Docs</th>\n</tr>\n</thead>\n<tbody>\n\n<tr>\n<td>Heap</td>\n<td><img src=\"https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg\" alt=\"\"></td>\n<td><img src=\"https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg\" alt=\"\"></td>\n<td><a href=\"https://data-structure-typed-docs.vercel.app/classes/Heap.html\"><span>Heap</span></a></td>\n</tr>\n\n</tbody>\n</table>\n\n## Standard library data structure comparison\n\n<table>\n  <thead>\n  <tr>\n    <th>Data Structure Typed</th>\n    <th>C++ STL</th>\n    <th>java.util</th>\n    <th>Python collections</th>\n  </tr>\n  </thead>\n  <tbody>\n  <tr>\n    <td>Heap&lt;E&gt;</td>\n    <td>priority_queue&lt;T&gt;</td>\n    <td>PriorityQueue&lt;E&gt;</td>\n    <td>heapq</td>\n  </tr>\n  \n  </tbody>\n</table>\n\n## Benchmark\n\n[//]: # (No deletion!!! Start of Replace Section)\n<div class=\"json-to-html-collapse clearfix 0\">\n      <div class='collapsible level0' ><span class='json-to-html-label'>heap</span></div>\n      <div class=\"content\"><table style=\"display: table; width:100%; table-layout: fixed;\"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add & pop</td><td>5.80</td><td>172.35</td><td>8.78e-5</td></tr><tr><td>10,000 fib add & pop</td><td>357.92</td><td>2.79</td><td>0.00</td></tr></table></div>\n    </div>\n\n[//]: # (No deletion!!! End of Replace Section)\n\n## Built-in classic algorithms\n\n<table>\n  <thead>\n  <tr>\n    <th>Algorithm</th>\n    <th>Function Description</th>\n    <th>Iteration Type</th>\n  </tr>\n  </thead>\n  <tbody>\n\n  </tbody>\n</table>\n\n## Software Engineering Design Standards\n<table>\n    <tr>\n        <th>Principle</th>\n        <th>Description</th>\n    </tr>\n    <tr>\n        <td>Practicality</td>\n        <td>Follows ES6 and ESNext standards, offering unified and considerate optional parameters, and simplifies method names.</td>\n    </tr>\n    <tr>\n        <td>Extensibility</td>\n        <td>Adheres to OOP (Object-Oriented Programming) principles, allowing inheritance for all data structures.</td>\n    </tr>\n    <tr>\n        <td>Modularization</td>\n        <td>Includes data structure modularization and independent NPM packages.</td>\n    </tr>\n    <tr>\n        <td>Efficiency</td>\n        <td>All methods provide time and space complexity, comparable to native JS performance.</td>\n    </tr>\n    <tr>\n        <td>Maintainability</td>\n        <td>Follows open-source community development standards, complete documentation, continuous integration, and adheres to TDD (Test-Driven Development) patterns.</td>\n    </tr>\n    <tr>\n        <td>Testability</td>\n        <td>Automated and customized unit testing, performance testing, and integration testing.</td>\n    </tr>\n    <tr>\n        <td>Portability</td>\n        <td>Plans for porting to Java, Python, and C++, currently achieved to 80%.</td>\n    </tr>\n    <tr>\n        <td>Reusability</td>\n        <td>Fully decoupled, minimized side effects, and adheres to OOP.</td>\n    </tr>\n    <tr>\n        <td>Security</td>\n        <td>Carefully designed security for member variables and methods. Read-write separation. Data structure software does not need to consider other security aspects.</td>\n    </tr>\n    <tr>\n        <td>Scalability</td>\n        <td>Data structure software does not involve load issues.</td>\n    </tr>\n</table>\n\n\n\n","publish_time":1769635950167,"_cnpm_publish_time":1769635950167,"dist-tags":{"latest":"2.6.0"}}