Comments (4)
They have a writing error, the correct one is "if" and not 'it'.
from javascript-algorithms.
You need to show me the poll method in priorityQueue? I think issue is occured due to that thing.
from javascript-algorithms.
Is this issue is still open ?
from javascript-algorithms.
The priority queue extends the heap Class.
The poll() method, inherited by the priority queue class, is part of the Heap Class. I have pasted a portion of the code from the method below:
// Move the last element from the end to the head.
this.heapContainer[0] = this.heapContainer.pop();
this.heapifyDown();
As you can see, the poll method pushes the last element in the queue to the front. In your case, after the first element is 'polled', the last element, which is 4, will be moved to the front of the queue. The 'heapifyDown' method, then, sorts the queue based on a comparator function. The priority queue class overrides the compactor function of the heap Class to ensure the queue is sorted based on priority of the item and not its value.
this.compare = new Comparator(this.comparePriority.bind(this));
comparePriority(a, b) {
if (this.priorities.get(a) === this.priorities.get(b)) {
return 0;
}
return this.priorities.get(a) < this.priorities.get(b) ? -1 : 1;
}
Since the priorities of all the items in the queue are the same, the 'heapifyDown' method does not sort the queue after 4 is moved to the front.
So if you were to log the output of the toString method:
pq.poll()
console.log(pq.toString())
Output:
'4,3,2'
instead of
'2,3,4'
If you are not going to assign priorities to the items in the queue, try using the minHeap data structure. When you use that data structure, the 'heapifydown' method will sort the queue based on the value of the item. You would get the output you were expecting.
from javascript-algorithms.
Related Issues (20)
- Binary Search file, comment typo HOT 3
- JS Data structure And Algorithms HOT 1
- Javadcript
- Typo in Portuguese README
- Data structures and algorithms by JavaScript
- Would be great to see `discussions` tab brought to this repository
- JavaScript HOT 3
- Traverse in Reverse is unnecessary complex HOT 4
- Unhandled Edge Case in Binary Search Implementation HOT 1
- `replaceChild` method in BinaryTreeNode is not correct HOT 1
- Pendekatan "Brute Force" dan "Divide and Conquer" HOT 1
- Why this pointing to global context is giving undefined, even when global variable is defined ? HOT 2
- is there support for support for `BigInt` HOT 1
- My dsa roadmap HOT 2
- Ne data project
- Learn HOT 1
- Ok
- Adding Circular Linked List In Data Structure. HOT 1
- Good HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javascript-algorithms.