-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[heap, priority queue] Redesigned tested pass
- Loading branch information
Showing
5 changed files
with
135 additions
and
167 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,44 +1,52 @@ | ||
import {HeapItem, MaxHeap} from '../../../../src'; | ||
|
||
describe('MaxHeap Operation Test', () => { | ||
it('should object Max Heap operations be proper', function () { | ||
const maxHeap = new MaxHeap<{keyA: string}>(); | ||
const myObj1 = {keyA: 'a1'}, | ||
myObj6 = {keyA: 'a6'}, | ||
myObj5 = {keyA: 'a5'}, | ||
myObj2 = {keyA: 'a2'}, | ||
myObj0 = {keyA: 'a0'}, | ||
myObj9 = {keyA: 'a9'}; | ||
maxHeap.add(1, myObj1); | ||
expect(maxHeap.has(myObj1)).toBe(true); | ||
expect(maxHeap.has(myObj9)).toBe(false); | ||
maxHeap.add(6, myObj6); | ||
expect(maxHeap.has(myObj6)).toBe(true); | ||
maxHeap.add(5, myObj5); | ||
expect(maxHeap.has(myObj5)).toBe(true); | ||
maxHeap.add(2, myObj2); | ||
expect(maxHeap.has(myObj2)).toBe(true); | ||
expect(maxHeap.has(myObj6)).toBe(true); | ||
maxHeap.add(0, myObj0); | ||
expect(maxHeap.has(myObj0)).toBe(true); | ||
expect(maxHeap.has(myObj9)).toBe(false); | ||
maxHeap.add(9, myObj9); | ||
expect(maxHeap.has(myObj9)).toBe(true); | ||
|
||
const peek9 = maxHeap.peek(true); | ||
peek9 && peek9.val && expect(peek9.val.keyA).toBe('a9'); | ||
|
||
const heapToArr = maxHeap.toArray(true); | ||
expect(heapToArr.map(item => item?.val?.keyA)).toEqual(['a9', 'a2', 'a6', 'a1', 'a0', 'a5']); | ||
|
||
const values = ['a9', 'a6', 'a5', 'a2', 'a1', 'a0']; | ||
let i = 0; | ||
while (maxHeap.size > 0) { | ||
const polled = maxHeap.poll(true); | ||
expect(polled).toBeInstanceOf(HeapItem); | ||
polled && expect(polled.val).toHaveProperty('keyA'); | ||
polled && polled.val && expect(polled.val.keyA).toBe(values[i]); | ||
i++; | ||
} | ||
import {CompareFunction, MaxHeap} from '../../../../src'; | ||
|
||
describe('MaxHeap', () => { | ||
const numberComparator: CompareFunction<number> = (a, b) => b - a; | ||
let maxHeap: MaxHeap<number>; | ||
|
||
beforeEach(() => { | ||
maxHeap = new MaxHeap(numberComparator); | ||
}); | ||
|
||
test('add and poll elements in descending order', () => { | ||
maxHeap.add(3); | ||
maxHeap.add(1); | ||
maxHeap.add(4); | ||
maxHeap.add(2); | ||
|
||
expect(maxHeap.poll()).toBe(4); | ||
expect(maxHeap.poll()).toBe(3); | ||
expect(maxHeap.poll()).toBe(2); | ||
expect(maxHeap.poll()).toBe(1); | ||
}); | ||
|
||
test('peek at the top element without removing it', () => { | ||
maxHeap.add(3); | ||
maxHeap.add(1); | ||
maxHeap.add(4); | ||
maxHeap.add(2); | ||
|
||
expect(maxHeap.peek()).toBe(4); | ||
expect(maxHeap.size).toBe(4); | ||
}); | ||
|
||
test('sort elements in descending order', () => { | ||
maxHeap.add(3); | ||
maxHeap.add(1); | ||
maxHeap.add(4); | ||
maxHeap.add(2); | ||
|
||
const sortedArray = maxHeap.sort(); | ||
expect(sortedArray).toEqual([4, 3, 2, 1]); | ||
}); | ||
|
||
test('check if the heap is empty', () => { | ||
expect(maxHeap.isEmpty()).toBe(true); | ||
|
||
maxHeap.add(5); | ||
expect(maxHeap.isEmpty()).toBe(false); | ||
|
||
maxHeap.poll(); | ||
expect(maxHeap.isEmpty()).toBe(true); | ||
}); | ||
}); |
This file contains 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 |
---|---|---|
@@ -1,82 +1,52 @@ | ||
import {HeapItem, MinHeap} from '../../../../src'; | ||
import {CompareFunction, MinHeap} from '../../../../src'; | ||
|
||
describe('MinHeap Operation Test', () => { | ||
it('should numeric Min Heap operations be proper', function () { | ||
const minNumHeap = new MinHeap<number>(); | ||
expect(minNumHeap).toBeInstanceOf(MinHeap); | ||
describe('MinHeap', () => { | ||
const numberComparator: CompareFunction<number> = (a, b) => a - b; | ||
let minHeap: MinHeap<number>; | ||
|
||
minNumHeap.add(1); | ||
expect(minNumHeap.has(1)).toBe(true); | ||
minNumHeap.add(6); | ||
expect(minNumHeap.has(2)).toBe(false); | ||
expect(minNumHeap.has(6)).toBe(true); | ||
minNumHeap.add(2); | ||
expect(minNumHeap.has(2)).toBe(true); | ||
minNumHeap.add(0); | ||
expect(minNumHeap.has(0)).toBe(true); | ||
minNumHeap.add(5); | ||
expect(minNumHeap.has(5)).toBe(true); | ||
minNumHeap.add(9); | ||
expect(minNumHeap.has(9)).toBe(true); | ||
expect(minNumHeap.size).toBe(6); | ||
|
||
const poll1 = minNumHeap.poll(true); | ||
expect(poll1).toBeInstanceOf(HeapItem); | ||
poll1 instanceof HeapItem && expect(poll1.val).toBe(0); | ||
|
||
const poll2 = minNumHeap.poll(true); | ||
expect(poll2).toBeInstanceOf(HeapItem); | ||
poll2 instanceof HeapItem && expect(poll2.val).toBe(1); | ||
beforeEach(() => { | ||
minHeap = new MinHeap(numberComparator); | ||
}); | ||
|
||
const peek1 = minNumHeap.peek(true); | ||
expect(peek1).toBeInstanceOf(HeapItem); | ||
peek1 instanceof HeapItem && expect(peek1.val).toBe(2); | ||
test('add and poll elements in ascending order', () => { | ||
minHeap.add(3); | ||
minHeap.add(1); | ||
minHeap.add(4); | ||
minHeap.add(2); | ||
|
||
const heapArray = minNumHeap.toArray(true); | ||
expect(heapArray).toBeInstanceOf(Array); | ||
expect(heapArray.map(item => item?.priority)).toEqual([2, 5, 9, 6]); | ||
expect(minNumHeap.size).toBe(4); | ||
expect(minHeap.poll()).toBe(1); | ||
expect(minHeap.poll()).toBe(2); | ||
expect(minHeap.poll()).toBe(3); | ||
expect(minHeap.poll()).toBe(4); | ||
}); | ||
|
||
it('should object Min Heap operations be proper', function () { | ||
class MyObject { | ||
keyA: string; | ||
test('peek at the top element without removing it', () => { | ||
minHeap.add(3); | ||
minHeap.add(1); | ||
minHeap.add(4); | ||
minHeap.add(2); | ||
|
||
constructor(keyA: string) { | ||
this.keyA = keyA; | ||
} | ||
} | ||
expect(minHeap.peek()).toBe(1); | ||
expect(minHeap.size).toBe(4); | ||
}); | ||
|
||
const minObjHeap = new MinHeap<MyObject>(); | ||
test('sort elements in ascending order', () => { | ||
minHeap.add(3); | ||
minHeap.add(1); | ||
minHeap.add(4); | ||
minHeap.add(2); | ||
|
||
const obj1 = new MyObject('a1'), | ||
obj6 = new MyObject('a6'), | ||
obj2 = new MyObject('a2'), | ||
obj0 = new MyObject('a0'); | ||
minObjHeap.add(1, obj1); | ||
expect(minObjHeap.has(obj1)).toBe(true); | ||
expect(minObjHeap.has(obj6)).toBe(false); | ||
minObjHeap.add(6, obj6); | ||
expect(minObjHeap.has(obj6)).toBe(true); | ||
minObjHeap.add(2, obj2); | ||
expect(minObjHeap.has(obj2)).toBe(true); | ||
minObjHeap.add(0, obj0); | ||
expect(minObjHeap.has(obj0)).toBe(true); | ||
const sortedArray = minHeap.sort(); | ||
expect(sortedArray).toEqual([1, 2, 3, 4]); | ||
}); | ||
|
||
const peek = minObjHeap.peek(true); | ||
peek && peek.val && expect(peek.val.keyA).toBe('a0'); | ||
test('check if the heap is empty', () => { | ||
expect(minHeap.isEmpty()).toBe(true); | ||
|
||
const heapToArr = minObjHeap.toArray(true); | ||
expect(heapToArr.map(item => item?.val?.keyA)).toEqual(['a0', 'a1', 'a2', 'a6']); | ||
minHeap.add(5); | ||
expect(minHeap.isEmpty()).toBe(false); | ||
|
||
const values = ['a0', 'a1', 'a2', 'a6']; | ||
let i = 0; | ||
while (minObjHeap.size > 0) { | ||
const polled = minObjHeap.poll(true); | ||
expect(polled).toBeInstanceOf(HeapItem); | ||
polled && expect(polled.val).toBeInstanceOf(MyObject); | ||
polled && polled.val && expect(polled.val.keyA).toBe(values[i]); | ||
i++; | ||
} | ||
minHeap.poll(); | ||
expect(minHeap.isEmpty()).toBe(true); | ||
}); | ||
}); |
This file contains 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 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