1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| import java.util.*; public class HuffmanDemo { public static void main(String[] args) { int rr = 3; int count = 7; char[] cS = {'a', 'e', 'i', 's', 't', 'b', 'n'}; double[] dF = {0.1, 0.15, 0.12, 0.03, 0.04, 0.13, 0.01}; int[] dF2 = new int[count]; for (int i = 0; i < count; i++) { dF2[i] = (int) (dF[i] * 1000); } int[][] dPS = new int[3][count]; dPS[2][0] = 0; for (int i = 0; i < count; i++) { dPS[0][i] = dF2[i]; dPS[1][i] = i; insertHeap(dPS, i + 1); dPS[2][0] = dPS[2][0] + 1; } int[][] cHT = new int[4][count * 2]; for (int i = 0; i < count; i++) { cHT[0][i] = i; cHT[1][i] = -1; cHT[2][i] = -1; cHT[3][i] = 1; } int beishu = count * rr * 4; StdDraw.setXscale(0, beishu); StdDraw.setYscale(0, beishu); StdDraw.setPenRadius(0.005); int root = 0; int n = count; int count2 = count; while (count >= 1) { if (count == 1) { inOrderPaint(root, cS, cHT, count2 * 5, count2 * 4 * rr - 10, rr, count2 * 1.9); break; } int mF = dPS[1][0]; int f1 = dPS[0][0]; dPS[0][0] = dPS[0][dPS[2][0]-1]; dPS[1][0] = dPS[1][dPS[2][0]-1]; dPS[2][0] = dPS[2][0] - 1; deleteHeap(dPS, 1); int mS = dPS[1][0]; int f2 = dPS[0][0]; dPS[0][0] = dPS[0][dPS[2][0] - 1]; dPS[1][0] = dPS[1][dPS[2][0] - 1]; dPS[2][0] = dPS[2][0] - 1; deleteHeap(dPS, 1); cHT[0][n] = n; cHT[1][n] = mF; cHT[2][n] = mS; if (cHT[3][mF] > cHT[3][mS]) { cHT[3][n] = cHT[3][mF] + 1; } dPS[0][dPS[2][0]] = f1 + f2; dPS[1][dPS[2][0]] = n; dPS[2][0] = dPS[2][0] + 1; insertHeap(dPS, dPS[2][0]); root = n; n = n + 1; count = count - 1; } }
public static void insertHeap(int[][] dPS, int i) { int zj,j; if(i>1){ j=i/2; if(dPS[0][i-1]<dPS[0][j-1]){ zj=dPS[0][i-1]; dPS[0][i-1]=dPS[0][j-1]; dPS[0][j-1]=zj; zj=dPS[1][i-1]; dPS[1][i-1]=dPS[1][j-1]; dPS[1][j-1]=zj; insertHeap(dPS,j); } } }
private static void deleteHeap(int[][] dPS, int i) { int n; int left,right,zj; n=dPS[2][0];; int j = 0; if(2*i>n) return; else if(2*i<n){ left=2*i; right=2*i+1; if(dPS[0][left-1]>dPS[0][right-1]) { j = right; } else { j = left; } }else if(n==2*i){ j=2*i; } if(dPS[0][j-1]<dPS[0][i-1]){ zj=dPS[0][i-1]; dPS[0][i-1]=dPS[0][j-1]; dPS[0][j-1]=zj; zj=dPS[1][i-1]; dPS[1][i-1]=dPS[1][j-1]; dPS[1][j-1]=zj; deleteHeap(dPS,j); } } private static void inOrderPaint(int root, char[] cS, int[][] cHT, double x, double y, double r, double jg) { double xC,yC,xS,yS,rC,zj1,zj2; StdDraw.setPenColor(StdDraw.BLACK); StdDraw.circle(x,y,r); if(cHT[1][root]!=-1&&cHT[2][root]==-1){ xC=x-jg; yC=y-4*r; zj1=r*jg/(Math.sqrt(16*r*r+jg*jg)); zj2=4*r*r/(Math.sqrt(16*r*r+jg*jg)); StdDraw.setPenColor(StdDraw.RED); StdDraw.line(x-zj1,y-zj2,xC+zj1,yC+zj2); inOrderPaint(cHT[1][root],cS,cHT,xC,yC,r,jg/1.3); } if(cHT[2][root]!=-1&&cHT[1][root]==-1){ xC=x+jg; yC=y-4*r; zj1=r*jg/(Math.sqrt(16*r*r+jg*jg)); zj2=4*r*r/(Math.sqrt(16*r*r+jg*jg)); StdDraw.setPenColor(StdDraw.RED); StdDraw.line(x+zj1,y-zj2,xC-zj1,yC+zj2); inOrderPaint(cHT[2][root],cS,cHT,xC,yC,r,jg/1.3); } if(cHT[2][root]!=-1&&cHT[1][root]!=-1){ xC=x-jg; yC=y-4*r; zj1=r*jg/(Math.sqrt(16*r*r+jg*jg)); zj2=4*r*r/(Math.sqrt(16*r*r+jg*jg)); StdDraw.setPenColor(StdDraw.RED); StdDraw.line(x-zj1,y-zj2,xC+zj1,yC+zj2); inOrderPaint(cHT[1][root],cS,cHT,xC,yC,r,jg/1.3); xC=x+jg; yC=y-4*r; zj1=r*jg/(Math.sqrt(16*r*r+jg*jg)); zj2=4*r*r/(Math.sqrt(16*r*r+jg*jg)); StdDraw.setPenColor(StdDraw.RED); StdDraw.line(x+zj1,y-zj2,xC-zj1,yC+zj2); inOrderPaint(cHT[2][root],cS,cHT,xC,yC,r,jg/1.3); } if(cHT[2][root]==-1&&cHT[1][root]==-1){ xS=x-0.05*r; yS=y; String s=""+cS[cHT[0][root]]; StdDraw.setPenColor(StdDraw.RED); StdDraw.text(xS,yS,s); } } }
|