-
Notifications
You must be signed in to change notification settings - Fork 268
Heaps #155
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
Heaps #155
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const MaxHeap = require('.'); | ||
|
||
describe('MaxHeap', () => { | ||
it('Should be a class', () => { | ||
expect(typeof MaxHeap.prototype.constructor).toEqual('function'); | ||
}); | ||
|
||
const mh = new MaxHeap(); | ||
|
||
beforeEach(() => { | ||
mh.destroy(); | ||
}); | ||
|
||
it('Should create an instance of MaxHeap', () => { | ||
expect(mh instanceof MaxHeap).toEqual(true); | ||
}); | ||
|
||
it('Should add an element to the MaxHeap', () => { | ||
mh.add(10); | ||
expect(mh.getMax()).toEqual(10); | ||
}); | ||
|
||
it('Should keep the largest element at the root', () => { | ||
[12, 5, 34].forEach(el => mh.add(el)); | ||
expect(mh.getMax()).toEqual(34); | ||
}); | ||
|
||
it('Should retain Heap properties after removal of an element', () => { | ||
[12, 45, 1, 34].forEach(el => mh.add(el)); | ||
expect(mh.getMax()).toEqual(45); | ||
mh.remove(); | ||
expect(mh.getMax()).toEqual(34); | ||
}); | ||
|
||
it('Should return `null` when heap is empty', () => { | ||
[1, 34].forEach(el => mh.add(el)); | ||
expect(mh.getMax()).toEqual(34); | ||
mh.remove(); | ||
mh.remove(); | ||
expect(mh.getMax()).toEqual(null); | ||
}); | ||
|
||
it('Should return the elelment value on `remove()`', () => { | ||
[1, 34].forEach(el => mh.add(el)); | ||
expect(mh.getMax()).toEqual(34); | ||
expect(mh.remove()).toEqual(34); | ||
expect(mh.remove()).toEqual(1); | ||
expect(mh.getMax()).toEqual(null); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
class MaxHeap { | ||
constructor() { | ||
this.heap = []; | ||
} | ||
|
||
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); | ||
} | ||
|
||
getMax() { | ||
return this.heap[0] || null; | ||
} | ||
|
||
remove() { | ||
// return the element at the root | ||
const max = this.heap[0] || null; | ||
if (this.heap.length > 1) { | ||
// move the leaf to the root | ||
this.heap[0] = this.heap[this.heap.length - 1]; | ||
this.heap.splice(this.heap.length - 1, 1); | ||
// restore the heapify property | ||
// eslint-disable-next-line no-underscore-dangle | ||
this.__heapify(0); | ||
return max; | ||
} | ||
|
||
if (this.heap.length === 1) { | ||
this.heap.splice(this.heap.length - 1, 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. splice will work here in O(1) because heap size is 1 but you can use pop. |
||
return max; | ||
} | ||
|
||
return max; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove this line because it will never execute and it will increase code coverage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
} | ||
|
||
__heapify(index) { | ||
const left = index * 2; | ||
const right = index * 2 + 1; | ||
let largest = index; | ||
|
||
if (this.heap.length > left && this.heap[largest] < this.heap[left]) { | ||
largest = left; | ||
} | ||
|
||
if (this.heap.length > right && this.heap[largest] < this.heap[right]) { | ||
largest = right; | ||
} | ||
|
||
if (largest !== index) { | ||
const temp = this.heap[largest]; | ||
this.heap[largest] = this.heap[index]; | ||
this.heap[index] = temp; | ||
// eslint-disable-next-line no-underscore-dangle | ||
this.__heapify(largest); | ||
} | ||
} | ||
|
||
__traverseUpAndSwap(index) { | ||
if (index <= 0) return; | ||
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); | ||
} | ||
} | ||
|
||
destroy() { | ||
this.heap = []; | ||
} | ||
} | ||
|
||
module.exports = MaxHeap; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Splice worst case should be O(n) . Instead of splice , pop will work in O(1).