Course: "Programming and Data Structures"
Class: 1823
Name: Hua Luo Han
Student ID: 20182308
experiments Teacher: Johnny
experiment date :( after-school practice)
Required / Elective: Compulsory
1. Experimental content
With character set: S = {a, b, c, d, e, f, g, h, i, j, k, l, m, nopq, r, s, t, u, v, w, x, y, z}.
Given a 26 letters of the file containing the statistical probability of occurrence of each character, based on the calculated probability construct a Huffman tree.
English documents and complete the encoding and decoding.
Claim:
- Prepare a 26 letters containing documents in English (or may not contain punctuation, etc.), the probability of each character's statistics
- Huffman tree structure
- File after the English files are encoded, a coding output
- Decoding the encoded file, outputting a decoded file
- Write a blog about experimental design and implementation process, and the source code cloud spread
- The results screenshots uploaded to the cloud class lesson
2. Experimental ideas, process and results
1, found to meet the requirements of a segment on Baidu.
2, the configuration of the Huffman tree, the first Huffman tree is a tree, the nodes must be manufactured, which is part of the code inside the methods Node.
public HuffmanNode(T name, double length)
{
this.name = name;
this.length = length;
code = "";
}
public T getName()
{
return name;
}
public void setName(T name)
{
this.name = name;
}
public double getLength()
{
return length;
}
public void setLength(double length)
{
this.length = length;
}
public HuffmanNode<T> getLeft()
{
return left;
}
public void setLeft(HuffmanNode<T> left)
{
this.left = left;
}
public HuffmanNode<T> getRight()
{
return right;
}
public void setRight(HuffmanNode<T> right)
{
this.right = right;
}
public String getCode()
{
return code;
}
public void setCode(String str)
{
code = str;
}
@Override
public String toString()
{
return "name:"+this.name+";length:"+this.length+";编码为: "+this.code;
}
@Override
public int compareTo(HuffmanNode<T> other)
{
if(other.getLength() > this.getLength())
return 1;
if(other.getLength() < this.getLength())
return -1;
return 0;
}
These are Huffman methods based node in the tree, the tree structure of a Huffman body in some other way the tree is constructed, generally similar to the previous playing tree.
Here contribution of code portions include:
public HuffmanNode<T> createTree(List<HuffmanNode<T>> nodes)
{
while (nodes.size() > 1)
{
Collections.sort(nodes);
HuffmanNode<T> left = nodes.get(nodes.size() - 2);
left.setCode(0 + "");
HuffmanNode<T> right = nodes.get(nodes.size() - 1);
right.setCode(1 + "");
HuffmanNode<T> parent = new HuffmanNode<T>(null, left.getLength() + right.getLength());
parent.setLeft(left);
parent.setRight(right);
nodes.remove(left);
nodes.remove(right);
nodes.add(parent);
}
return nodes.get(0);
}
public List<HuffmanNode> breadth(HuffmanNode root)
{
List<HuffmanNode> list = new ArrayList<HuffmanNode>();
Queue<HuffmanNode> queue = new ArrayDeque<HuffmanNode>();
if (root != null)
{
queue.offer(root);
root.getLeft().setCode(root.getCode() + "0");
root.getRight().setCode(root.getCode() + "1");
}
while (!queue.isEmpty())
{
list.add(queue.peek());
HuffmanNode node = queue.poll();
if (node.getLeft() != null)
node.getLeft().setCode(node.getCode() + "0");
if (node.getRight() != null)
node.getRight().setCode(node.getCode() + "1");
if (node.getLeft() != null)
{
queue.offer(node.getLeft());
}
if (node.getRight() != null)
{
queue.offer(node.getRight());
}
}
return list;
}
3, the English files are encoded, you first need to create a file and read into, here follows the I / O-related content. This part of the code to skip it, convert this document into English String variable format, 01 were encrypted. Create two files and then a target variable res (encoded), and String2 (decoded) write. At the same time again for String operate, calculate the probability variables like appearance.
int temp = 0;
for(int e = 0;e<inlist.size();e++)
{
if(inlist.get(e).getName() != null){
System.out.println(inlist.get(e).getName()+"的编码为"+ inlist.get(e).getCode()+" ");
name[temp] = (String) inlist.get(e).getName();
code[temp] = inlist.get(e).getCode();
temp++;
}
}
String res = "";
for(int f = 0; f < sum; f++)
{
for(int j = 0;j<name.length;j++)
{
if(message.charAt(f) == name[j].charAt(0))
res += code[j];
}
}
System.out.println("编码后:"+ res);
List<String> putlist = new ArrayList<String>();
for(int i = 0;i < res.length();i++)
putlist.add(res.charAt(i)+"");
String string1 = "";
String string2 = "";
for(int h = putlist.size(); h > 0; h--){
string1 = string1 + putlist.get(0);
putlist.remove(0);
for(int i=0;i<code.length;i++){
if (string1.equals(code[i])) {
string2 = string2+""+ name[i];
string1 = "";
}
}
}
System.out.println("解码后:" + string2);
4, after the read error on the part thrown part, this is not the code is put out.
Operating results as shown below:
3. Experimental problems encountered in the process and settlement process
- Question 1: Error thrown problems.
- Problem 1 Solution: See classmates code, think throw the wrong question. Thus the addition of the following method code (for simultaneously reading two operations into a new file is here):
private static int getFileLineCount(File file) {
int cnt = 0;
InputStream is = null;
try
{
is = new BufferedInputStream(new FileInputStream(file));
byte[] c = new byte[1024];
int readChars = 0;
while ((readChars = is.read(c)) != -1)
{
for (int i = 0; i < readChars; ++i)
{
if (c[i] == '\n')
++cnt;
}
}
} catch (Exception ex)
{
cnt = -1;
ex.printStackTrace();
}
finally
{
try
{
is.close();
}
catch (Exception ex)
{ex.printStackTrace();}
}
return cnt;
}