Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

261
Vistas
Priority Queue <Double> vs Priority Queue <Node>

Ok so I'm trying to use a java priority queue to sort nodes by the double val they contain. I know how to do a priority queue by double but I need the rest of the node values for a huffman encoding tree construction. Is there a way to sort a priority queue of nodes by their val or would I be better off making the queue sort the double vals and then trying to match them to the nodes?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You have two options, and the choice depends on what the Double means in the Node:

  1. Have Node implement Comparable<Node>, and use PriorityQueue<Node>. The compareTo method will order by the Double. The queue order you want is the natural order of the elements.
  2. Use the PriorityQueue constructor that takes a Comparator argument. The Comparator you use for the queue should order according to the Double value.

The first one is better if the Double really is the natural order for the nodes. The second one is better if it is only important for this queue, and other fields are equally important for other purposes.

over 4 years ago · Santiago Trujillo Denunciar

0

You can use PriorityQueue<Node> implementation and manage the ordering of elements by overriding compare method of the Comparator interface during the queue construction time.

For example: To sort elements in ascending order of their Node values, use the below-mentioned declaration of the priority queue and then simply add nodes to the priority queue.

PriorityQueue<Node> pq = new PriorityQueue<Node>(new Comparator<Node>(){
   public int compare(Node node1, Node node2){
       if(node2.val>node1.val){
          return -1;
       }
       else{
          return 1;
       }
   }
});
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda