Achieve haffman tree

Achieve haffman tree

Overall analysis:

  • First of all, we have to analyze the whole idea, we divided into three parts, the first part is the contribution, the second part is supposed to do after getting a string to turn it into a binary 01, then a 01 into a binary character string, the third part is the establishment of documentation to read and store.
  • Then consider the first part, we can be divided into steps, the first step, we count the number of characters in the string out of the second step, to establish a certain node, a third step, the connected nodes into a tree.
  • Followed by the second part, in fact, this may not be haffman class was done, can be done in the main method call in. They are also divided into three steps, the first step is to establish the basis of this tree correspondence between letters and 01, the second step is based on correspondence between the encryption, decryption is the third step.
  • The final step is to build a file, this is very simple.

    Part I: achievements

    The first step: count the number of characters

      We can use a simple and crude way, the direct use arrays to store, we may note that we use in the general process, we can use these methods to make an array of letters and correspondence, of course, we can also pass a.charAt(i)-'a'the - to determine the location of each character in the array, and then every once met this letter to let that corresponds to the number of the self-energizing just once. I have here it is the first method.

Here Insert Picture Description
  After that we can get an array record number of each letter.
  We then add half a step in the middle, we create a memory array to which letters are present, which are not so easy letters present in the back stand when the contribution to reduce waste.

Here Insert Picture Description

The second step: the establishment of a node

  With this array b, we will have to establish node according to, we can use these to create nodes.
Here Insert Picture Description

Step 3: Connect nodes

  In fact, this step is where the bulk, mainly easily stuck in this step. We can see that we have so many nodes, it is imperative that we put these nodes are connected together (nonsense) According to the principle it into a tree, we can think of is that every time we have taken a minimum of two digital node, and then put them together to form a new node into them, in fact, the first thing I thought is that this is not priority queue you want to direct use, but does not seem to find a java in the priority queue, you can only use the first queue instead. Each time a pop number, compared with the existing number of two, leaving the smaller of two numbers, to push back large numbers, so that it can get after a selected minimum number of two, then the operation again and to put back until only a.

Here Insert Picture Description

Part II: encryption, decryption,

The first step: the establishment of the correspondence between

  This is actually a traversal thing to note here is that we try to use the recursive method, so do not back and forth additions and deletions to the last element.

Here Insert Picture Description

Step two: Encryption

  This step we have to do is put a string scan it again, while scanning each character is encrypted.

Here Insert Picture Description

The third step: decrypting

  The more interesting, we have the tree directly by the string operations, determined by the 01 sub-tree is left or right, and then has gone through 01 removed.
Here Insert Picture Description

Part III: build file

Here Insert Picture Description

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/ydfy/p/11899416.html