Priority Huffman Tree Queues

I am trying to create a Huffman tree by reading in a file and counting the frequency of each character in letter space, etc. I use Priorityqueue to queue items from smallest to largest, but when I insert them into the queue, they dont queue correctly here is my code. huffman package;

import java.io.FileNotFoundException; import java.io.FileReader; import java.util.ArrayList; import java.util.PriorityQueue; import java.util.Scanner;

Huffman open class {

public ArrayList<Frequency> fileReader(String file)
{
    ArrayList<Frequency> al = new ArrayList<Frequency>();
    Scanner s;
    try {

        s = new Scanner(new FileReader(file)).useDelimiter("");
        while (s.hasNext())
        {
            boolean found = false;
            int i = 0;
            String temp = s.next();
            while(!found)
            {


                if(al.size() == i && !found)
                {
                    found = true;
                    al.add(new Frequency(temp, 1));
                }
                else if(temp.equals(al.get(i).getString()))
                {
                    int tempNum = al.get(i).getFreq() + 1;
                    al.get(i).setFreq(tempNum);
                    found = true;
                }
                i++;

            }



        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return al;
}
public void buildTree(ArrayList<Frequency> al)
{
    PriorityQueue<Frequency> pq = new PriorityQueue<Frequency>();
    for(int i = 0; i < al.size(); i++)
    {
        pq.add(al.get(i));          
    }
    while(pq.size() > 0)
    {
        System.out.println(pq.remove().getString());
    }
}
public void printFreq(ArrayList<Frequency> al)
{
    for(int i = 0; i < al.size(); i++)
    {
        System.out.println(al.get(i).getString() + "; " + al.get(i).getFreq());
    }
}

}

in the buildTree () method is where im has the problem. what I'm trying to do is turn. Frequency objects that contain a letter / space / character, and a frequency, like int, is a frequency class. Public class Frequency implements Comparative {private String s; private int n;

Frequency(String s, int n)
{
    this.s = s;
    this.n = n;
}
public String getString()
{
    return s;
}
public int getFreq()
{
    return n;
}
public void setFreq(int n)
{
    this.n = n;
}
@Override
public int compareTo(Object arg0) {
    // TODO Auto-generated method stub
    return 0;
}

}

, ?

+5
3

compareTo, .

compareTo, ,

, , , .

, - :

public int compareTo(Object arg0)
{
  Frequency other = (Frequency)arg0;

  return n < other.n ? -1 : (n == other.n ? 0 : 1);
}

, : Comparable<T>, arg0, Frequency :

class Frequency implements Comparable<Frequency> {   
  public int compareTo(Frequency f2) {
    // directly compare
  }
}
+3

, "Auto-generated method stub" "compareTo", - , , PriorityQueue . , , "n < arg0", Object.

+2

A , , - ​​, - ..

Java : Comparable, Comparator<E>, , E.

, " ", , compareTo(). :

. , , , .

Frequency.compareTo() 0 . , , Frequency Frequency. , .

+2

All Articles