Skip to content

Heaps #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Nov 5, 2019
Merged

Heaps #156

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: repo placement
  • Loading branch information
ashokdey committed Nov 4, 2019
commit 875f6cab95e5aae978836153d268b99735bd4706
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const MaxHeap = require('../../_DataStructures_/Heaps/MaxHeap');
const MaxHeap = require('../MaxHeap');

/**
* Find the 4 largest elements from an array
Expand Down
21 changes: 21 additions & 0 deletions src/_DataStructures_/Heaps/k-smallest-in-array/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Find 4 smallest elements in an array
*/

const MinHeap = require('../MinHeap');

function findKSmallest(collection, k) {
if (!collection || !Array.isArray(collection)) {
throw new Error('Invalid / missing collection');
}

// create a MinHeap using the collection
const mh = new MinHeap(collection);
const result = [];
for (let i = 0; i < k; i += 1) {
result.push(mh.getMin());
}
return result;
}

module.exports = findKSmallest;
22 changes: 22 additions & 0 deletions src/_DataStructures_/k-largest-in-array/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const MaxHeap = require('../MaxHeap');

/**
* Find the 4 largest elements from an array
*/

function findKLargest(collection, k) {
if (!collection || !Array.isArray(collection)) {
throw new Error('Invalid / missing collection');
}

// create a MaxHeap using the collection
const mh = new MaxHeap(collection);
const result = [];

for (let i = 0; i < k; i += 1) {
result.push(mh.getMax());
}
return result;
}

module.exports = findKLargest;
2 changes: 1 addition & 1 deletion src/_Problems_/k-smallest-in-array/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Find 4 smallest elements in an array
*/

const MinHeap = require('../../_DataStructures_/Heaps/MinHeap');
const MinHeap = require('../MinHeap');

function findKSmallest(collection, k) {
if (!collection || !Array.isArray(collection)) {
Expand Down