public void putinPQ(int newVert, int newDist) { int queueIndex = thePQ.find(newVert); if (queueIndex != -1) { Edge tempEdge = thePQ.peekN(queueIndex); int oldDist = tempEdge.distance; if (oldDist > newDist) { thePQ.removeN(queueIndex); Edge edge = new Edge(currVert, newVert, newDist); thePQ.insert(edge); } } else { Edge edge = new Edge(currVert, newVert, newDist); thePQ.insert(edge); } }
public void queueTree() { // Builds the queue along with all the subtrees that exist within. for (int i = 0; i < freqTable.length; i++) { if (freqTable[i] > 0) { Tree t = new Tree(); t.insert(freqTable[i], (char) (i + 65)); theQ.insert(t); } } }
public void makeHuffmanTree() { // Goes through the process of combining the two lowest priority subtrees // and then replacing them within the queue until a single tree is left while (!theQ.isEmpty()) { Tree t = new Tree(); Tree left = theQ.remove(); Tree right = theQ.remove(); t.insert(left.root.iData + right.root.iData, '+'); t.root.leftChild = left.root; t.root.rightChild = right.root; if (theQ.isEmpty()) { huffTree = t; } else { theQ.insert(t); } } }