-
Notifications
You must be signed in to change notification settings - Fork 269
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
Heaps #156
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
15e262f
update: entry in TOC.md
ashokdey c2f5934
fix: used pop() instead of splice()
ashokdey 5338710
update: added MinHeap, support for collection in constructor
ashokdey 16006e0
fix: order of length check in remove()
ashokdey 7b958e9
fix & update: handled case of min/max element 0, fixes in tests
ashokdey 6ced3ff
update: entry in TOC.md
ashokdey 042670a
update: added k smallest elements problem
ashokdey f9f2cfc
update: added K largest array elements
ashokdey a03369c
fix: repo name change
ashokdey 875f6ca
fix: repo placement
ashokdey 4f5f695
cleanup
ashokdey 1e2a548
update: entry in TOC.md
ashokdey c2d8c12
fix: remove will return and remove the element from the heap
ashokdey c3e44df
update: README makeover
ashokdey 2daa87d
fixes: README changes
ashokdey 2c1011a
update: Added links in README
ashokdey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file added
.assets/dsa.jpeg
Binary file added
.assets/logo.png
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
src/_DataStructures_/Heaps/MinHeap/MinHeap.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const MinHeap = require('.'); | ||
|
||
describe('MinHeap', () => { | ||
it('Should be a class', () => { | ||
expect(typeof MinHeap.prototype.constructor).toEqual('function'); | ||
}); | ||
|
||
const mh = new MinHeap(); | ||
|
||
beforeEach(() => { | ||
mh.destroy(); | ||
}); | ||
|
||
it('Should create an instance of MinHeap', () => { | ||
expect(mh instanceof MinHeap).toEqual(true); | ||
}); | ||
|
||
it('Should create a MinHeap using collection', () => { | ||
const mHBulk = new MinHeap([112, 3, 21, 9, 10, 0]); | ||
expect(mHBulk.getMin()).toEqual(0); | ||
}); | ||
|
||
it('Should add an element to the MinHeap', () => { | ||
mh.add(10); | ||
expect(mh.getMin()).toEqual(10); | ||
}); | ||
|
||
it('Should keep the smallest element at the root', () => { | ||
[12, 5, 34].forEach(el => mh.add(el)); | ||
expect(mh.getMin()).toEqual(5); | ||
}); | ||
|
||
it('Should retain Heap properties after removal of an element', () => { | ||
[12, 45, 1, 34].forEach(el => mh.add(el)); | ||
expect(mh.getMin()).toEqual(1); | ||
mh.remove(); | ||
expect(mh.getMin()).toEqual(12); | ||
}); | ||
|
||
it('Should return `null` when heap is empty', () => { | ||
[1, 34].forEach(el => mh.add(el)); | ||
expect(mh.getMin()).toEqual(1); | ||
mh.remove(); | ||
mh.remove(); | ||
expect(mh.getMin()).toEqual(null); | ||
}); | ||
|
||
it('Should return the elelment value on `remove()`', () => { | ||
[1, 34].forEach(el => mh.add(el)); | ||
expect(mh.getMin()).toEqual(1); | ||
expect(mh.remove()).toEqual(1); | ||
expect(mh.remove()).toEqual(34); | ||
expect(mh.getMin()).toEqual(null); | ||
}); | ||
|
||
it('Should return `null` on `remove() called on empty heap`', () => { | ||
expect(mh.getMin()).toEqual(null); | ||
}); | ||
}); |
78 changes: 78 additions & 0 deletions
src/_DataStructures_/Heaps/MinHeap/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
class MinHeap { | ||
constructor(collection) { | ||
this.heap = []; | ||
|
||
if (collection) { | ||
collection.forEach((element) => { | ||
this.add(element); | ||
}); | ||
} | ||
} | ||
|
||
add(element) { | ||
this.heap.push(element); | ||
// check for the parent element & swap if required | ||
// eslint-disable-next-line no-underscore-dangle | ||
this.__traverseUpAndSwap(this.heap.length - 1); | ||
} | ||
|
||
getMin() { | ||
return this.heap[0] !== undefined ? this.heap[0] : null; | ||
} | ||
|
||
remove() { | ||
const min = this.heap[0] !== undefined ? this.heap[0] : null; | ||
if (this.heap.length === 1) { | ||
this.heap.pop(); | ||
} | ||
if (this.heap.length > 1) { | ||
this.heap[0] = this.heap[this.heap.length - 1]; | ||
this.heap.pop(); | ||
// eslint-disable-next-line no-underscore-dangle | ||
this.__heapify(0); | ||
} | ||
return min; | ||
} | ||
|
||
destroy() { | ||
this.heap = []; | ||
} | ||
|
||
// eslint-disable-next-line consistent-return | ||
__traverseUpAndSwap(index) { | ||
if (index <= 0) return null; | ||
|
||
const parent = Math.floor(index / 2); | ||
|
||
if (this.heap[parent] > this.heap[index]) { | ||
const temp = this.heap[parent]; | ||
this.heap[parent] = this.heap[index]; | ||
this.heap[index] = temp; | ||
// eslint-disable-next-line no-underscore-dangle | ||
this.__traverseUpAndSwap(parent); | ||
} | ||
} | ||
|
||
__heapify(index) { | ||
const left = index * 2; | ||
const right = index * 2 + 1; | ||
|
||
let smallest = index; | ||
|
||
if (this.heap.length > left && this.heap[smallest] > this.heap[left]) { | ||
smallest = left; | ||
} | ||
if (this.heap.length > right && this.heap[smallest] > this.heap[right]) { | ||
smallest = right; | ||
} | ||
if (smallest !== index) { | ||
const tmp = this.heap[smallest]; | ||
this.heap[smallest] = this.heap[index]; | ||
this.heap[index] = tmp; | ||
// eslint-disable-next-line no-underscore-dangle | ||
this.__heapify(smallest); | ||
} | ||
} | ||
} | ||
|
||
module.exports = MinHeap; |
22 changes: 22 additions & 0 deletions
src/_DataStructures_/Heaps/k-largest-in-array/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.remove()); | ||
} | ||
return result; | ||
} | ||
|
||
module.exports = findKLargest; |
21 changes: 21 additions & 0 deletions
src/_DataStructures_/Heaps/k-smallest-in-array/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.remove()); | ||
} | ||
return result; | ||
} | ||
|
||
module.exports = findKSmallest; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.