{"id":25490181,"url":"https://github.com/fix2015/structure_heap","last_synced_at":"2025-08-11T05:08:31.977Z","repository":{"id":273082597,"uuid":"918657919","full_name":"fix2015/structure_heap","owner":"fix2015","description":"A simple implementation of the **Heap** data structure in JavaScript. This repository demonstrates how to create a heap class with essential methods and explains its functionality with practical examples.  ","archived":false,"fork":false,"pushed_at":"2025-01-18T14:19:28.000Z","size":6,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-02T13:14:39.508Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fix2015.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-18T14:17:05.000Z","updated_at":"2025-01-18T14:41:18.000Z","dependencies_parsed_at":"2025-01-18T15:38:32.126Z","dependency_job_id":null,"html_url":"https://github.com/fix2015/structure_heap","commit_stats":null,"previous_names":["fix2015/structure_heap"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/fix2015/structure_heap","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fix2015%2Fstructure_heap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fix2015%2Fstructure_heap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fix2015%2Fstructure_heap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fix2015%2Fstructure_heap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fix2015","download_url":"https://codeload.github.com/fix2015/structure_heap/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fix2015%2Fstructure_heap/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269832882,"owners_count":24482330,"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-08-11T02:00:10.019Z","response_time":75,"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":[],"created_at":"2025-02-18T21:27:27.054Z","updated_at":"2025-08-11T05:08:31.949Z","avatar_url":"https://github.com/fix2015.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Heap Data Structure in JavaScript 🚀  \n\nA simple implementation of the **Heap** data structure in JavaScript. This repository demonstrates how to create a heap class with essential methods and explains its functionality with practical examples.  \n\n---\n\n## What is a Heap?  \nA **Heap** is a special tree-based data structure that satisfies the **heap property**. In a **Max Heap**, the value of each parent node is greater than or equal to the values of its children, while in a **Min Heap**, the value of each parent node is less than or equal to the values of its children. Heaps are commonly used to implement priority queues.  \n\n---\n\n## Features  \n- **Insert**: Add a new element to the heap.  \n- **Extract**: Remove and return the root element (max or min).  \n- **Peek**: View the root element without removing it.  \n- **Size**: Get the number of elements in the heap.  \n- **Heapify**: Maintain the heap property after insertions or deletions.  \n\n---\n\n## Code Implementation  \n\nHere’s the JavaScript implementation of the heap:  \n\n```javascript\nclass Heap {\n    constructor(type = \"max\") {\n        this.type = type; // \"max\" for Max Heap, \"min\" for Min Heap\n        this.heap = [];\n    }\n\n    // Insert an element into the heap\n    insert(value) {\n        this.heap.push(value);\n        this.heapifyUp();\n    }\n\n    // Helper function to maintain the heap property after insertion\n    heapifyUp() {\n        let index = this.heap.length - 1;\n        while (index \u003e 0) {\n            const parentIndex = Math.floor((index - 1) / 2);\n            if (this.compare(this.heap[index], this.heap[parentIndex])) {\n                [this.heap[index], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[index]];\n                index = parentIndex;\n            } else {\n                break;\n            }\n        }\n    }\n\n    // Extract the root element (max or min)\n    extract() {\n        if (this.size() === 0) return null;\n        const root = this.heap[0];\n        const last = this.heap.pop();\n        if (this.size() \u003e 0) {\n            this.heap[0] = last;\n            this.heapifyDown();\n        }\n        return root;\n    }\n\n    // Helper function to maintain the heap property after extraction\n    heapifyDown() {\n        let index = 0;\n        const length = this.heap.length;\n        const leftChildIndex = (index * 2) + 1;\n        const rightChildIndex = (index * 2) + 2;\n        let swap = null;\n\n        if (leftChildIndex \u003c length) {\n            if (this.compare(this.heap[leftChildIndex], this.heap[index])) {\n                swap = leftChildIndex;\n            }\n        }\n        if (rightChildIndex \u003c length) {\n            if (this.compare(this.heap[rightChildIndex], this.heap[swap || index])) {\n                swap = rightChildIndex;\n            }\n        }\n        if (swap === null) return;\n        [this.heap[index], this.heap[swap]] = [this.heap[swap], this.heap[index]];\n        this.heapifyDown();\n    }\n\n    // Compare two values based on the heap type\n    compare(child, parent) {\n        if (this.type === \"max\") {\n            return child \u003e parent;\n        }\n        return child \u003c parent;\n    }\n\n    // Peek at the root element without removing it\n    peek() {\n        return this.heap[0];\n    }\n\n    // Get the size of the heap\n    size() {\n        return this.heap.length;\n    }\n}\n```\n\n---\n\n## Example Usage  \n\n```javascript\n// Initialize a Max Heap\nconst maxHeap = new Heap(\"max\");\n\n// Insert elements into the heap\nmaxHeap.insert(10);\nmaxHeap.insert(20);\nmaxHeap.insert(5);\n\n// Peek at the root element\nconsole.log(maxHeap.peek()); // Output: 20\n\n// Extract the root element (max)\nconsole.log(maxHeap.extract()); // Output: 20\nconsole.log(maxHeap.peek()); // Output: 10\n\n// Get the size of the heap\nconsole.log(maxHeap.size()); // Output: 2\n\n// Initialize a Min Heap\nconst minHeap = new Heap(\"min\");\n\n// Insert elements into the min heap\nminHeap.insert(10);\nminHeap.insert(20);\nminHeap.insert(5);\n\n// Peek at the root element\nconsole.log(minHeap.peek()); // Output: 5\n\n// Extract the root element (min)\nconsole.log(minHeap.extract()); // Output: 5\nconsole.log(minHeap.peek()); // Output: 10\n```\n\n---\n\n## Real-World Applications  \n1. **Priority Queues**: For scheduling tasks based on priority.  \n2. **Heap Sort**: Sorting an array in ascending or descending order.  \n3. **Dijkstra’s Algorithm**: For finding the shortest path in a graph.  \n4. **Dynamic Median Finding**: Finding the median in a dynamic data set.  \n\n---\n\n## TikTok Tutorial 🎥  \nWant to see a quick tutorial on how to build this? Check out this TikTok video:  \n[]()  \n\n---\n\n## How to Run the Code  \n1. Clone the repository:  \n   ```bash\n   git clone https://github.com/fix2015/structure_heap\n   cd structure_heap\n   ```\n2. Open the file `index.js` in your favorite code editor.  \n3. Run the file using Node.js:  \n   ```bash\n   node index.js\n   ```\n\n---\n\n## Contributing  \nContributions are welcome! If you have suggestions or want to add new features, feel free to create a pull request.  \n\n---\n\n## License  \nThis project is licensed under the MIT License.  \n\n---\n\n## Connect with Me:\n- [LinkedIn - Vitalii Semianchuk](https://www.linkedin.com/in/vitalii-semianchuk-9812a786/)\n- [Telegram - @jsmentorfree](https://t.me/jsmentorfree) - We do a lot of free teaching on this channel! Join us to learn and grow in web development.\n- [Tiktok - @jsmentoring](https://www.tiktok.com/@jsmentoring) Everyday new videos\n- [Youtube - @jsmentor-uk](https://www.youtube.com/@jsmentor-uk) Mentor live streams\n- [Dev.to - fix2015](https://dev.to/fix2015) Javascript featured, live, experience but about Heap\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffix2015%2Fstructure_heap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffix2015%2Fstructure_heap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffix2015%2Fstructure_heap/lists"}