{"_id":"@caelus-dts/heap","name":"@caelus-dts/heap","dist-tags":{"latest":"0.1.0"},"versions":{"0.1.0":{"name":"@caelus-dts/heap","version":"0.1.0","description":"A Typescript implementation of a tree-based structure, ensuring highest priority element at the root.","publishConfig":{"registry":"https://registry.npmjs.org/"},"main":"dist/cjs/index.js","module":"dist/es/index.js","types":"dist/types/index.d.ts","scripts":{"build":"vite build","test":"jest --detectOpenHandles"},"keywords":["heap","data structure","tree-based"],"author":{"name":"Caelus"},"license":"MIT","exports":{".":{"types":"./dist/types/index.d.ts","require":"./dist/cjs/index.js","import":"./dist/es/index.js","default":"./dist/es/index.js"}},"dependencies":{"@caelus-dts/iterable":"^1.0.0"},"devDependencies":{"@types/jest":"^29.5.14","jest":"^29.7.0","node-notifier":"^10.0.1","ts-jest":"^29.2.5","vite":"^6.0.5","vite-plugin-dts":"^4.4.0"},"repository":{"url":"git+https://github.com/caelus-lab/caelus-dts-heap.git"},"bugs":{"url":"https://github.com/caelus-lab/caelus-dts-heap/issues"},"homepage":"https://github.com/caelus-lab/caelus-dts-heap#readme","_id":"@caelus-dts/heap@0.1.0","gitHead":"2c552934d6487f9149216291e107fde1fa44cbbb","_nodeVersion":"22.12.0","_npmVersion":"10.9.0","dist":{"integrity":"sha512-u/7BhMOjEHKz9GQLn+y6a1p8WNvdJs21tWuq54XLQKa7zemGc1O+T7DfcezLiqs8/ZbTgzWw+Ki5T3xb8OSb0A==","shasum":"8303f165fbd5b46c5e446f552fb733ff95d24aab","tarball":"https://registry.npmjs.org/@caelus-dts/heap/-/heap-0.1.0.tgz","fileCount":6,"unpackedSize":13263,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDloWiBuss+bDYqPeB19LozdnnWs81zjWOdIF4nipHFCAIgeRv5zVFWQeoPtEqL2HbysGGeMdP3sdWj3pKpWW//9rI="}]},"_npmUser":{"name":"caelus-lib","email":"caelus.team@hotmail.com"},"directories":{},"maintainers":[{"name":"caelus-lib","email":"caelus.team@hotmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/heap_0.1.0_1736118280585_0.4063446861462141"},"_hasShrinkwrap":false}},"time":{"created":"2025-01-05T23:04:40.501Z","0.1.0":"2025-01-05T23:04:40.743Z","modified":"2025-01-05T23:04:41.002Z"},"maintainers":[{"name":"caelus-lib","email":"caelus.team@hotmail.com"}],"description":"A Typescript implementation of a tree-based structure, ensuring highest priority element at the root.","homepage":"https://github.com/caelus-lab/caelus-dts-heap#readme","keywords":["heap","data structure","tree-based"],"repository":{"url":"git+https://github.com/caelus-lab/caelus-dts-heap.git"},"author":{"name":"Caelus"},"bugs":{"url":"https://github.com/caelus-lab/caelus-dts-heap/issues"},"license":"MIT","readme":"# @caelus-dts/queue\r\n\r\n## Overview\r\nA TypeScript implementation of a heap data structure, a specialized tree-based structure that satisfies the heap property.\r\n\r\n* **Heap Property:**  The element with the highest priority (determined by a comparison function) is always at the root of the heap.\r\n\r\n## Installation\r\n- using `npm`\r\n```shell\r\nnpm install @caelus-dts/heap\r\n```\r\n- using `yarn`\r\n```shell\r\nyarn add @caelus-dts/heap\r\n```\r\n- using `pnpm`\r\n```shell\r\npnpm add @caelus-dts/heap\r\n```\r\n\r\n## Usage\r\n\r\n```ts\r\nimport Heap from '@caelus-dts/heap';\r\n// Comparison function for numbers (min-heap)\r\nconst compareNumbers = (a, b) => a - b;\r\n\r\n// Create a new min-heap of numbers\r\nconst minHeap = new Heap(compareNumbers);\r\n\r\n// Add some numbers\r\nminHeap.add(5);\r\nminHeap.add(2);\r\nminHeap.add(8);\r\nminHeap.add(1);\r\n\r\n// Peek at the smallest element (should be 1)\r\nconsole.log(minHeap.peek()); // Output: 1\r\n\r\n// Remove the smallest element\r\nconsole.log(minHeap.remove()); // Output: 1\r\n\r\n// Convert to array\r\nconsole.log(minHeap.toArray()); // Output: [2, 5, 8]\r\n\r\n// Example with string values and different compare function\r\nconst compareLength = (a, b) => a.length - b.length;\r\n\r\nconst heapStrings = new Heap<string>(compareLength)\r\n\r\nheapStrings.add('short')\r\nheapStrings.add('longer')\r\nheapStrings.add('a');\r\n\r\nconsole.log(heapStrings.peek()); // 'a'\r\n```\r\n\r\n## API Documentation\r\n\r\n### Constructor\r\n`new Heap<T>(compareFunc: HeapCompareFunc<T>)`\r\nCreates a new Heap instance.\r\n- **`compareFunc`**: The function used to compare elements in the heap, determining their priority.\r\n\r\n### Properties\r\n- **`isEmpty`**: Returns `true` if the heap is empty, `false` otherwise.\r\n- **`size`**: Returns the number of elements in the heap.\r\n\r\n### Methods\r\n- **`add(item: T)`**: Adds a new element to the heap.\r\n- **`clear()`**: Removes all elements from the heap.\r\n- **`peek()`**: Returns the root element of the heap (element with highest priority) without removing it. Returns `undefined` if the heap is empty.\r\n- **`remove()`**: Removes and returns the root element of the heap. Returns `undefined` if the heap is empty.\r\n- **`toArray()`**: Returns a shallow copy of the heap's elements as an array.\r\n\r\n## Contributing\r\nContributions are welcome! Please fork the repository and submit a pull request.\r\n\r\n## License\r\n[MIT License](LICENSE)","readmeFilename":"README.md"}