Code Monkey home page Code Monkey logo

Comments (4)

stiven104 avatar stiven104 commented on June 11, 2024 6

They have a writing error, the correct one is "if" and not 'it'.

from javascript-algorithms.

PranjalPatel14 avatar PranjalPatel14 commented on June 11, 2024

You need to show me the poll method in priorityQueue? I think issue is occured due to that thing.

from javascript-algorithms.

v-a14 avatar v-a14 commented on June 11, 2024

Is this issue is still open ?

from javascript-algorithms.

fahad-ejaz avatar fahad-ejaz commented on June 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.