Respuesta :

To store the tree, create a priority queue. Next, merge the two least frequent nodes. Lastly, move the tree's root to the front of the queue.

CODE :

public HuffmanTree(Scanner input) {

   // read the number of items in the tree

   int size = input.nextInt();

   // create a priority queue to store the tree

   PriorityQueue queue = new PriorityQueue(size, new HuffmanNodeComparator());

   // loop through the scanner and create the nodes

   for (int i = 0; i < size; i++) {

       int frequency = input.nextInt();

       char letter = input.next().charAt(0);

       HuffmanNode node = new HuffmanNode(frequency, letter);

       queue.add(node);

   }

   // while there is more than one node in the queue, combine the two least frequent nodes

   while (queue.size() > 1) {

       HuffmanNode node1 = queue.poll();

       HuffmanNode node2 = queue.poll();

       int frequency = node1.frequency + node2.frequency;

       HuffmanNode parent = new HuffmanNode(frequency, '*');

       parent.leftChild = node1;

       parent.rightChild = node2;

       queue.add(parent);

   }

   // set the root of the tree to the top of the queue

   root = queue.poll();

}

To know more about Code
https://brainly.com/question/13261820
#SPJ4

ACCESS MORE