mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
Make it possible to change priority of PriorityQueue.
This commit is contained in:
@@ -33,4 +33,58 @@ describe('PriorityQueue', () => {
|
||||
expect(priorityQueue.poll()).toBe(10);
|
||||
expect(priorityQueue.poll()).toBe(5);
|
||||
});
|
||||
|
||||
it('should be possible to change priority of internal nodes', () => {
|
||||
const priorityQueue = new PriorityQueue();
|
||||
|
||||
priorityQueue.add(10, 1);
|
||||
priorityQueue.add(5, 2);
|
||||
priorityQueue.add(100, 0);
|
||||
priorityQueue.add(200, 0);
|
||||
|
||||
priorityQueue.changePriority(100, 10);
|
||||
priorityQueue.changePriority(10, 20);
|
||||
|
||||
expect(priorityQueue.poll()).toBe(200);
|
||||
expect(priorityQueue.poll()).toBe(5);
|
||||
expect(priorityQueue.poll()).toBe(100);
|
||||
expect(priorityQueue.poll()).toBe(10);
|
||||
});
|
||||
|
||||
it('should be possible to change priority of head node', () => {
|
||||
const priorityQueue = new PriorityQueue();
|
||||
|
||||
priorityQueue.add(10, 1);
|
||||
priorityQueue.add(5, 2);
|
||||
priorityQueue.add(100, 0);
|
||||
priorityQueue.add(200, 0);
|
||||
|
||||
priorityQueue.changePriority(200, 10);
|
||||
priorityQueue.changePriority(10, 20);
|
||||
|
||||
expect(priorityQueue.poll()).toBe(100);
|
||||
expect(priorityQueue.poll()).toBe(5);
|
||||
expect(priorityQueue.poll()).toBe(200);
|
||||
expect(priorityQueue.poll()).toBe(10);
|
||||
});
|
||||
|
||||
it('should be possible to change priority along with node addition', () => {
|
||||
const priorityQueue = new PriorityQueue();
|
||||
|
||||
priorityQueue.add(10, 1);
|
||||
priorityQueue.add(5, 2);
|
||||
priorityQueue.add(100, 0);
|
||||
priorityQueue.add(200, 0);
|
||||
|
||||
priorityQueue.changePriority(200, 10);
|
||||
priorityQueue.changePriority(10, 20);
|
||||
|
||||
priorityQueue.add(15, 15);
|
||||
|
||||
expect(priorityQueue.poll()).toBe(100);
|
||||
expect(priorityQueue.poll()).toBe(5);
|
||||
expect(priorityQueue.poll()).toBe(200);
|
||||
expect(priorityQueue.poll()).toBe(15);
|
||||
expect(priorityQueue.poll()).toBe(10);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user