Data compression method and device and data decompression method and device based on Huffman coding

By converting the Hoffman tree to a trident tree structure and generating unique encoding using recursive formulas, the inefficiency problem caused by traversing and obtaining encoding in the Hoffman encoding method is solved, and more efficient data compression and decompression are achieved.

CN119135188APending Publication Date: 2024-12-13XIAMEN UNIV
View PDF 0 Cites 3 Cited by

Patent Information

Application Number
CN202411292217.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-14
Publication Date
2024-12-13

AI Technical Summary

Technical Problem

The Hoffman encoding method needs to traverse to obtain character encoding after building the Hoffman tree, resulting in low data compression efficiency.

Method used

By converting the Hoffman tree to a trine tree structure, adjusting the node depth and number of child nodes, determining the leaf node number using a recursive formula, and generating a unique encoding for data compression and decompression.

Benefits of technology

It significantly improves the efficiency of data compression and decompression, avoids traversal operations, and enhances the uniqueness and compression effect of encoding.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119135188A_ABST
    Figure CN119135188A_ABST
Patent Text Reader

Abstract

The invention discloses a data compression method and device and a data decompression method and device.In the compression method, data to be compressed are processed according to a Huffman coding mode to obtain a first data table, then the first data table is standardized to obtain a second data table, and the second data table is compressed according to the Huffman coding mode. Obtaining a hierarchical table according to the second data table, obtaining serial numbers of all leaf nodes through a formula on the basis of the hierarchical table, outputting codes corresponding to the characters according to the serial numbers and depth of the leaf nodes, and compressing the data according to the codes; in the decompression method, decompression can be carried out only by acquiring the coding table or the weight table generated in the compression process of the data to be decompressed; and the Hoffman tree does not need to be traversed in the compression and decompression processes, so that the compression and decompression efficiency of the data can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of data compression, and in particular to a data compression method and device based on Huffman coding, and a data decompression method and device. Background Art

[0002] As an entropy coding technology, Huffman coding is widely used in the field of lossless data compression. Huffman coding constructs a binary tree "bottom-up" based on the count results of the number of occurrences of various characters in the encoded data, and then assigns a code with the shortest average length to each character. It is a coding method based on character statistical frequency and binary tree data structure. However, in the Huffman coding method, after the Huffman tree is constructed, it is necessary to traverse the Huffman tree to obtain the code corresponding to each character, which undoubtedly reduces the compression efficiency of the data. Summary of the invention

[0003] The purpose of the present invention is to overcome the above-mentioned defects or problems existing in the background technology and to provide a data compression method and device and a data decompression method and device based on Huffman coding, which can improve the compression efficiency of data.

[0004] To achieve the above object, the present invention adopts the following technical solutions:

[0005] Technical Solution 1: A data compression method based on Huffman coding, which is used to compress data including multiple characters, and is characterized in that the data to be compressed is processed according to the Huffman coding method, and a first data table with a ternary tree structure is output; the information recorded in the first data table includes the weight of each node, the parent node of the child node, and the depth of each node; the first data table is standardized and a second data table is output; the standardized processing refers to adjusting the number of child nodes of the parent nodes of other layers above the second-to-last parent node in the first data table to 3, and adjusting the depth of the nodes whose tree relationship has changed; a hierarchical table is output based on the second data table, and the hierarchy records the number of leaf nodes in each layer, and each leaf node corresponds to a character; the hierarchical table is divided into two layers with M={m i , i=1,2,3……k} means, where m i is the number of leaves in each layer in the second data table, i represents the corresponding layer in the second data table, and k represents the total number of layers in the second data table; according to the recursive formula, the numbers of all leaf nodes are determined; the recursive formula is: N i =R i -m i , R i+1 =3N i +2, P i =N i+1, where R2 = 2, P i The number of the first leaf node in the i-th layer when all leaf nodes in the second data table are sorted in ascending order according to their weights, and the number of subsequent leaf nodes in the i-th layer is the number of its previous leaf node plus one; according to the numbers of all leaf nodes and the corresponding depths in the second data table, the code corresponding to each character is output; and the data to be compressed is compressed according to the code.

[0006] Technical solution 2 based on technical solution 1: the code corresponding to each character is a ternary representation of the number of the leaf node corresponding to the character.

[0007] Technical solution three based on technical solution two: the length of the code corresponding to each character is consistent with the depth of the leaf node corresponding to the character in the second data table.

[0008] Technical solution 4 based on technical solution 3: the hierarchical table corresponds to the third data table, and the third data table is obtained by normalization processing based on the second data table; the normalization processing refers to adjusting the non-leaf nodes of each layer in the second data table to before the leaf nodes, and sorting the leaf nodes of the layer in ascending order with reference to the weight of each node; in the recursive formula, R i Indicates the node number at the last position in the i-th layer of the third data table, N i Indicates the number of the last non-leaf node in the third data table; when node N i When only two child nodes are included, R in the recursive formula i+1 =3N i +2 adjusted to R i+1 =3N i +1.

[0009] Technical Solution 5 based on Technical Solution 4: The processing of the data to be compressed according to the Huffman coding method means that the weights of the characters in the compressed data are calculated according to the frequency, starting with one character corresponding to one node, and cyclically adding the three nodes with the lowest weights, and at each addition, the depth of the nodes and their child nodes involved in the addition operation is increased by one.

[0010] Technical Solution 6 based on Technical Solution 5: The standardized processing of the first data table means that when there is a parent node with only two or fewer child nodes, if the depth of its child node is not the maximum value among all depths, then the node with the largest weight is selected from the next layer of its child node as its new child node, and the weight and depth of the child node are modified until the number of child nodes of the parent nodes of other layers above the penultimate layer are all 3.

[0011] Technical Solution 7: A data compression device based on Huffman coding, which is used to compress data including multiple characters, and is characterized in that it includes: a first processing module, which is used to process the data to be compressed according to the Huffman coding method, and output a first data table with a ternary tree structure; the information recorded in the first data table includes the weight of each node, the parent node of the child node, and the depth of each node; a second processing module, which is used to standardize the first data table and output a second data table; the standardization process refers to adjusting the number of child nodes of the parent nodes of other layers above the parent node of the second-to-last layer in the first data table to 3, and adjusting the depth of the nodes whose tree relationship has changed; a third processing module, which is used to output a hierarchical table based on the second data table, the hierarchy records the number of leaf nodes in each layer, and each leaf node corresponds to one of the characters. ; The hierarchical table is represented by M={m_i, i=1,2,3...k}, wherein m_i is the number of leaves in each layer in the second data table, i represents the corresponding layer in the second data table, and k represents the total number of layers in the second data table; according to the recursive formula, the numbering of all leaf nodes is determined; the recursive formula is: N_i=R_i-m_i, R_(i+1)=3N_i+2, P_i=N_i+1, wherein R_2=2, P_i is the number of the first leaf node in the i-th layer when all leaf nodes in the second data table are sorted in ascending order according to their weights, and the numbering of subsequent leaf nodes in the i-th layer is the numbering of the previous leaf node plus one; an encoding module, which is used to output the code corresponding to each character according to the numbering of all leaf nodes and the corresponding depth in the second data table; a compression module, which is used to compress the data to be compressed according to the code.

[0012] Technical Solution 8: A data decompression method based on Huffman coding, which is used to decompress data obtained after compression using the data compression method based on Huffman coding as described in claim 6, characterized in that, according to the weight table corresponding to the data to be decompressed, the depth corresponding to each character is obtained, and the hierarchical table is output; the weight table includes the corresponding relationship between each character and its weight; according to the hierarchical table, the number of each leaf node after all leaf nodes are sorted in ascending order according to their weights is determined by the recursive formula, and a fourth data table is output; or the fourth data table is output according to the encoding table corresponding to the data to be decompressed, the encoding table includes the corresponding relationship between each character and the encoding; the depth of the leaf node with the smallest depth is taken as the minimum unit, the data to be decompressed is read, and compared with the number and depth of each leaf node in the fourth data table, if the match is successful, it is converted into the original character, if the match fails, the number of read bits is increased by one, and re-matched in the fourth data table until the data to be decompressed is completely decompressed.

[0013] Technical solution nine based on technical solution eight: when outputting the hierarchical table according to the weight table corresponding to the data to be decompressed, the weight table is processed in turn according to the Huffman coding method to output a first data table with a ternary tree structure, the first data table is standardized to output a second data table, and the hierarchical table is output based on the second data table.

[0014] Technical Solution 10: A data decompression device based on Huffman coding, which is used to decompress data obtained after compression using the data compression method based on Huffman coding as described in claim 6, characterized in that it includes: a first decoding module, which is used to obtain the depth corresponding to each character according to the weight table corresponding to the data to be decompressed, and output the hierarchical table; the weight table includes the corresponding relationship between each character and its weight; a second decoding module, which is used to determine the number of each leaf node after all leaf nodes are sorted in ascending order according to their weights according to the hierarchical table through the recursive formula, and output a fourth data table; or output the fourth data table according to the encoding table corresponding to the data to be decompressed, the encoding table includes the corresponding relationship between each character and the encoding; a decompression module, which is used to read the compressed data with the depth of the leaf node with the smallest depth as the minimum unit, and compare it with the number and depth of each leaf node in the fourth data table. If the match is successful, it is converted into the original character. If the match fails, the number of read bits is increased by one, and re-matched in the fourth data table until the data to be decompressed is completely decompressed.

[0015] It can be seen from the above description of the present invention that, compared with the prior art, the present invention has the following beneficial effects:

[0016] The data compression method and data decompression method based on Huffman coding provided by the present invention can compress data including multiple characters, and decompress the data compressed by the method to obtain initial data in which the multiple characters are arranged in a specific manner, and compared with the conventional Huffman coding method, the method has higher compression efficiency and decompression efficiency. The data to be compressed can be in a variety of formats, such as text, pictures, audio, video, etc., and the character refers to the smallest unit corresponding to different formats for storing information. For example, for a string of text, each character is a character, and for a picture, the value of each pixel is a character.

[0017] In the data compression method, the data to be compressed is first processed into a first data table with a ternary tree structure according to the Huffman coding method, and the first data table records the weight of each node, the parent node of the child node and the depth of each node. The first data table corresponds to an initial Huffman tree, wherein the depth refers to the layer where each node is located. For example, a depth of 1 indicates that the node is in the first layer, that is, the layer of the root node, and a depth of 3 indicates that the node is in the third layer and is one of the child nodes of one of the child nodes of the root node; processing according to the Huffman coding method refers to referring to the conventional Huffman coding method, and each calculation adds several nodes with the lowest weight to form a new parent node. In this method, the number of nodes added each time is 3, and compared with the conventional binary Huffman tree, it has a higher information density at the same number of layers;

[0018] Then, the first data table is standardized to obtain a second data table, which corresponds to a standard Huffman tree obtained by standardizing the initial Huffman tree. The standardization process is to adjust the parent nodes with less than 3 child nodes in the initial Huffman tree except the parent node of the second-to-last layer to parent nodes with 3 child nodes, so that the weighted paths of all nodes in the standard Huffman tree are the shortest, and at the same time, adjust the depth of the nodes whose tree relationships have changed. At this time, the depth of each leaf node is determined, that is, it is possible to know how many leaf nodes are included in each layer; obviously, one leaf node corresponds to one character of the data to be compressed;

[0019] A hierarchical table can be formed according to the number of leaf nodes in each layer, and then the numbering of all leaf nodes can be obtained according to the hierarchical table and the recursive formula; it should be noted that the numbering obtained according to the recursive formula does not correspond to the position of the leaf node in the second data table, but corresponds to the position of the leaf node in the reordered third data table, and this correspondence can facilitate subsequent decompression operations; after obtaining the numbering of all leaf nodes, combined with the depth of each leaf node, a unique code corresponding to each leaf node can be formed, which can be represented in ternary to adapt to the three-pronged Huffman tree structure, and the length of the code is consistent with the depth of the corresponding leaf node in the second data table, so the layer of the corresponding leaf node in the standard Huffman tree can be determined by the length of the code;

[0020] After obtaining the code corresponding to each character, each character in the data to be compressed can be compressed, and the new data obtained after compression can be decompressed by the data decompression method provided by the present invention. When decompressing data, the decompression method needs to obtain the coding table or weight table corresponding to the data to be decompressed, and obtain the depth corresponding to each character and the hierarchy table used in the compression process based on it, wherein the depth is used to determine the minimum reading unit when reading the data to be decompressed, and the hierarchy table is used to determine the number of each leaf node. After reading and converting the data to be decompressed one by one, the data before compression can be obtained.

[0021] The data compression and decompression method provided by the present invention does not require traversal operations during the compression process, but directly obtains the number of each leaf node through a formula. The number is arranged according to a specific rule and is therefore unique. A unique code can be obtained through the number and the corresponding depth. The data compressed according to the code can be reversed to obtain the original data in the opposite way. Furthermore, no traversal operations are required during decompression, and the read code can be directly compared and matched with the fourth data table. The compression and decompression efficiency of the data are significantly improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0022] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0023] Figure 1 A flow chart of a data compression method provided by an embodiment of the present invention;

[0024] Figure 2 A schematic diagram of a three-branch Huffman tree corresponding to a first data table provided in an embodiment of the present invention;

[0025] Figure 3 A schematic diagram of a three-branch Huffman tree corresponding to the second data table provided in an embodiment of the present invention;

[0026] Figure 4 A schematic diagram of a three-branch Huffman tree corresponding to the third data table provided in an embodiment of the present invention. DETAILED DESCRIPTION

[0027] The technical solutions in the embodiments of the present invention will be described clearly and completely below in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are preferred embodiments of the present invention and should not be regarded as excluding other embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0028] In the claims, description and the above-mentioned drawings of the present invention, unless otherwise clearly defined, the use of terms such as "first", "second" or "third" etc. are for distinguishing different objects rather than for describing a specific order.

[0029] In the claims, description and drawings of the present invention, if the terms "include", "have" and their variations are used, they are intended to mean "including but not limited to".

[0030] Example 1

[0031] Embodiment 1 of the present invention provides a data compression method based on Huffman coding, which is used to compress data including multiple characters.

[0032] First, it is necessary to briefly introduce Huffman coding to help understand the data compression method provided by the present application. Huffman coding, also known as Huffman coding or Huffman coding, is an efficient lossless data compression coding method. It is based on the principle of variable-length coding and implements data encoding by constructing a special binary tree, the Huffman Tree. The working principle of Huffman coding is as follows: Frequency statistics: First, count the frequency of occurrence of each symbol (such as letters, numbers, characters, etc.) in the data to be encoded. Construct a Huffman tree: Then, sort these symbols from small to large according to the frequency of occurrence, and then merge the two symbols with the lowest frequency into a new node, and the frequency of the new node is the sum of the frequencies of the two symbols. Repeat this process until all symbols are merged into a single tree root node. In this process, each time a merge is performed, the newly generated node is always placed at the end of the queue to ensure that the two nodes with the lowest current frequency are always selected when the next merge is performed. Generate a coding table: The path from the root node of the Huffman tree to each leaf node defines the encoding of the symbol represented by the leaf node. Usually, the path from the root to the leaf is marked as 0 to the left and 1 to the right, so that each symbol has a unique binary code, and the codes for symbols with high frequency are shorter, and the codes for symbols with low frequency are longer. Encoding and decoding: According to the generated coding table, each symbol in the original data is encoded to obtain a compressed data stream. During decoding, the original data can be restored from the encoded data stream according to the coding rules and the Huffman tree, because Huffman coding is a prefix coding, that is, no code is a prefix of another code, which ensures the uniqueness of the code. The purpose of Huffman coding is to achieve data compression by reducing the average number of bits of the encoded data, which is particularly suitable for data sets with uneven symbol frequencies.

[0033] Secondly, it is necessary to clearly define several technical terms used in the description and claims of the present invention.

[0034] The "data" referred to in the specification and claims of the present invention is divided into data to be compressed, compressed data and decompressed data according to different states. After a piece of data to be compressed is compressed into compressed data and decompressed, the decompressed data obtained is the same as the data to be compressed before compression.

[0035] The "data including multiple characters" referred to in the specification and claims of the present invention means that for different types of data, "character" is used as the smallest unit of storage information, and a piece of data may include multiple characters. For example, for text type data, each character is a character; for image type data, the value of each pixel is a character. Other types of data can be understood by reference.

[0036] The "node" referred to in the specification and claims of the present invention refers to a node in a three-branch Huffman tree. The node type is divided into a leaf node and a non-leaf node according to whether it includes child nodes. The leaf node corresponds to each character in the data to be compressed.

[0037] The "weight" referred to in the specification and claims of the present invention, for a leaf node, is the frequency or number of times its corresponding character appears in the data to be compressed. For example, in a string of text, if character A appears 50 times, its weight is 50; for a non-leaf node, it is the sum of the weights of its own child nodes in its first data table.

[0038] The "depth" referred to in the specification and claims of the present invention corresponds to the layer where a node is located in the corresponding ternary Huffman tree, with the root node being the first layer and its depth being 0; for example, for a 4-layer ternary Huffman tree, the node is located at the 4th layer, and its depth is 3.

[0039] On the premise of understanding the above basic concepts, the data compression method provided by the present invention is introduced in detail below.

[0040] Reference Figure 1 The data compression method based on Huffman coding provided by the present invention mainly comprises the following steps:

[0041] Step 1: Process the data to be compressed according to the Huffman coding method, and output a first data table with a ternary tree structure; the information recorded in the first data table includes the weight of each node, the parent node of the child node, and the depth of each node;

[0042] Step 2: Standardize the first data table and output a second data table; the standardization process is to adjust the number of child nodes of the parent nodes of other layers above the second-to-last parent node in the first data table to 3, and adjust the depth of the nodes whose tree relationship has changed;

[0043] Step 3: Output a hierarchical table based on the second data table, wherein the hierarchical table records the number of leaf nodes in each layer, and each leaf node corresponds to a character; the hierarchical table is represented by M={m i , i=1,2,3……k} means, where m i is the number of leaves in each layer in the second data table, i represents the corresponding layer in the second data table, and k represents the total number of layers in the second data table;

[0044] Step 4: Determine the numbers of all leaf nodes according to the recursive formula; the recursive formula is: N i =R i -m i , Ri+1 =3N i +2, P i =N i +1, where R2 = 2, P i is the number of the first leaf node in the i-th layer when all leaf nodes in the second data table are sorted in ascending order according to their weights, and the number of subsequent leaf nodes in the i-th layer is the number of the previous leaf node plus one;

[0045] Step 5: Output the code corresponding to each character according to the numbers of all leaf nodes and the corresponding depths in the second data table;

[0046] Step six: compress the data to be compressed according to the encoding.

[0047] Among them, in step one, the processing of the data to be compressed according to the Huffman coding method means that the weights of the characters in the compressed data are calculated according to the frequency, starting with one character corresponding to one node, and the three nodes with the lowest weights are cyclically added, and at each addition, the depth of the nodes and their child nodes involved in the addition operation is increased by one.

[0048] In step 2, the standardization of the first data table means that when there is a parent node with only two or fewer child nodes, if the depth of its child node is not the maximum value among all depths, then a node with the largest weight is selected from the next layer of its child node as its new child node, and the weight and depth of the child node are modified until the number of child nodes of the parent nodes of other layers above the penultimate layer are all 3.

[0049] In step three, the hierarchical table corresponds to the third data table, and the third data table is obtained by normalization processing based on the second data table; the normalization processing refers to adjusting the non-leaf nodes of each layer in the second data table to before the leaf nodes, and sorting the leaf nodes of the layer in ascending order with reference to the weight of each node.

[0050] In step 4, in the recursive formula, R i Indicates the node number at the last position in the i-th layer of the third data table, N i Indicates the number of the last non-leaf node in the third data table; when node N i When only two child nodes are included, R in the recursive formula i+1 =3N i +2 adjusted to R i+1 =3N i +1.

[0051] In step five, the code corresponding to a character is a ternary representation of the number of the leaf node corresponding to the character; and the length of the code corresponding to a character is consistent with the depth of the leaf node corresponding to the character in the second data table.

[0052] The above data compression method is described below with a specific example.

[0053] Take the compression of a text type of data as an example. The specific text of the data is:

[0054] FGHFHFEAJBHFEJBIEJDIEHEBIFAGJEFCFCDHJDJJGJHJBBGAABCGIIHHEDIDEEIJJCGDFEEIEFCEDJFFBHHEHCAADHHBHDJJDHGHIGFDAHCDJGGDGIAGGAJIEDECCJHH GBACFGFJIJEBHFDEFJJIFHIDGFGICGBFADJAIABFJCAADCACFFGHHIGICFHAIBCJIIBCBJJFJCBDGDEACCHBACIBDGIHBEFCIJIEHDABAIDEEBEGEIJIIJHCBGGDGFAG.

[0055] The text data is preliminarily processed according to the conventional Huffman coding method, that is, the corresponding weight is calculated according to the number of times each character appears. The following is the weight table of the data, which includes the corresponding relationship between each character in the data and its weight.

[0056] Weight table:

[0057] character A B C D E F G H I J Weight 22 22 23 24 25 26 27 28 29 30

[0058] According to the weight table, the following first data table with a ternary tree structure is output:

[0059] First data table:

[0060]

[0061]

[0062]

[0063] The above calculation process can refer to the following description: the initial data is the data in the weight table, each character is a node, no calculation is performed at this time, so there is no parent node yet, and the depth of each node is 0; the first addition is performed, the weights of the three nodes A, B, and C with the lowest weight are added together to obtain the parent node ABC, and the depth of these three nodes is increased by one; the second addition is performed, the weights of the three nodes D, E, and F with the lowest weight are added together to obtain the parent node DEF, and the depth of these three nodes is increased by one; the third addition is similar and will not be repeated; the fourth addition, the three nodes with the lowest weight and that have not yet been added are ABC, DEF, and J. These three nodes are added together to obtain the parent node ABCDEFJ, and the depth of the corresponding child nodes and child nodes of the child nodes are increased by one; then the fifth addition is performed to complete the output of the first data table. At this time, refer to Figure 2 , which shows the structure of the three-fork Huffman tree corresponding to the first data table. In the three-fork Huffman tree, node A is at the 4th layer, its depth is 3, and its parent node is ABC. For each node, the information stored includes its own depth, parent node and weight.

[0064] After obtaining the first data table, the first data table is standardized and the following second data table is output:

[0065]

[0066]

[0067] For the processing results of the second data table, please refer to Figure 3 The purpose of normalization is to make each parent node include three child nodes as much as possible to form a sufficiently regular three-branch Huffman tree. The parent node of the second-to-last layer may have less than three child nodes, which is determined by the number of characters in the data to be compressed. If the number of characters in the data to be compressed is an even number, there will inevitably be a parent node that includes only two child nodes. Corresponding to the hierarchical table, there is only R in the last layer. i+1 =3N i +1.

[0068] In the process of adjusting the second data table, first determine the number of child nodes of each parent node, and locate the layer where the parent node with only two child nodes is located. This location can be performed through the stored depth information; then select the node with the largest weight from the two layers below the layer where the parent node is located, and adjust the layer where the node is located to the upper layer of the layer where it was originally located, that is, it becomes the new child node of the parent node. The above adjustment is repeated until the parent nodes of all layers except the second to last layer have three child nodes.

[0069] Afterwards, according to the second data table, the following hierarchical table M can be obtained:

[0070]

[0071]

[0072] According to the definition of the hierarchical table, the number of leaf nodes in the first layer is 0, the number of leaf nodes in the second layer is 0, the number of leaf nodes in the third layer is 8, and the number of leaf nodes in the fourth layer is 2.

[0073] In fact, since the hierarchical table is derived based on the second data table, the third data table is established by creating rules, so that the hierarchical table can be corresponded to the third data table. The third data table can be obtained by normalizing the second data table, and its form is as follows:

[0074]

[0075] The form of the Huffman tree of the third data table can be referred to Figure 4 .from Figure 3 to Figure 4 It can be seen that the normalization process is to sort the positions of the nodes twice. The first sorting is to adjust all non-leaf nodes to before the leaf nodes. Here, before actually means on the left side in order from left to right. The second sorting is to sort all leaf nodes in ascending order according to their weights, so that the position of each leaf node in the third data table corresponds to its weight. After that, each node is assigned a number from front to back, starting from 0. For example, in the fourth layer, node A is numbered 0 and node B is numbered 1. In the third layer, node ABC is numbered 0 and node C is numbered 1, and so on. According to the above-mentioned normalization rules, a recursive formula can be formed to conveniently calculate the number of each leaf node.

[0076] In the recursive formula, N i =R i -m i , R i+1 =3N i +2, P i =N i +1, where R2 = 2, and m is obtained through the hierarchical table i In the case of the value of, P can be obtained by loop calculation. i , and then according to P i Get the numbers of all subsequent leaf nodes.

[0077] Afterwards, according to the numbers of all leaf nodes and the depth of each leaf node in the second data table, the codes corresponding to each character are output. It should be noted that since the numbers correspond to the ordering relationship of the leaf nodes, the corresponding code table also needs to be filled in according to the ordering relationship.

[0078] Coding table:

[0079]

[0080]

[0081] In order to correspond to the three-pronged Huffman tree structure, the numbering is converted into a ternary code. It can be seen that for a leaf node with a depth of 3, the length of its code is also 3 bits, and for a leaf node with a depth of 2, the length of its code is also 2 bits. If the Huffman tree includes 6 layers, then the longest code length should be 6 bits, and its coding method is the same as the example given in this embodiment.

[0082] Compress the original string according to the above encoding table, and the compressed encoding stream is:

[0083] 111220201120111010000221001111100022102110102102020102011100012000011001000010010012212012021221022122012012002222000000010012212102220110211012101112201012011120220200021000212021101012012120210221001012020201120100000001010122220102201200210221012120122120221022101021022010002001120121001000010100101001011010122110002110001021101011022112101220000212021012010001120100000001100002122001001121020201221012120100022101001000211021221021120000101011101110122012011001010002112010010021101220110011021012000020101012201012112210012100020110220221022120000000001111122120210110022120110210021012112011121110002111100100001000010001011001111021200001210001011210201022221002011120121101010110000021001122000012000111010010002002110021200021210110002012110212020100010111002010211011200010002001110。

[0084] In the data compression method provided in this embodiment, the data to be compressed is first processed into a first data table with a ternary tree structure according to the Huffman coding method. The first data table records the weight of each node, the parent node of the child node, and the depth of each node. The first data table corresponds to an initial Huffman tree, wherein the depth refers to the layer where each node is located. For example, a depth of 1 indicates that the node is in the first layer, that is, the layer of the root node, and a depth of 3 indicates that the node is in the third layer and is one of the child nodes of one of the child nodes of the root node. Processing according to the Huffman coding method refers to referring to the conventional Huffman coding method. The method adds several nodes with the lowest weight to form a new parent node in each calculation. The number of nodes added each time in this method is 3. Compared with the conventional binary Huffman tree, it has a higher information density at the same number of layers. Then, the first data table is standardized to obtain a second data table. The second data table corresponds to a standard Huffman tree after the initial Huffman tree is standardized. The standardization process is to adjust the parent nodes with less than 3 child nodes in the initial Huffman tree except the parent node on the second-to-last layer to parent nodes with 3 child nodes, so that the standard The weighted paths of all nodes of the Huffman tree are the shortest, and the depth of the nodes whose tree relationships have changed is adjusted. At this time, the depth of each leaf node is determined, that is, the number of leaf nodes included in each layer can be known; obviously, one leaf node corresponds to a character of the data to be compressed; a hierarchical table can be formed according to the number of leaf nodes in each layer, and then the numbering of all leaf nodes can be obtained according to the hierarchical table and the recursive formula; it should be noted that the numbering obtained according to the recursive formula does not correspond to the position of the leaf node in the second data table, but corresponds to the position of the leaf node in the reordered third data table. This correspondence can facilitate subsequent decompression operations; after obtaining the numbering of all leaf nodes, combined with the depth of each leaf node, a unique code corresponding to each leaf node can be formed, which can be expressed in ternary to adapt to the three-pronged Huffman tree structure, and the length of the code is consistent with the depth of the corresponding leaf node in the second data table, so the layer of the corresponding leaf node in the standard Huffman tree can be determined by the length of the code; after obtaining the code corresponding to each character, each character in the data to be compressed can be compressed.

[0085] Since no traversal operation is required during the compression process, the number of each leaf node is directly obtained through the formula. This number is arranged according to specific rules and is therefore unique. A unique code can be obtained through the number and the corresponding depth, which significantly improves the compression efficiency.

[0086] Example 2

[0087] Embodiment 2 of the present invention provides a data compression device based on Huffman coding, which is used to compress data including multiple characters, and includes:

[0088] A first processing module, which is used to process the data to be compressed according to the Huffman coding method, and output a first data table with a ternary tree structure; the information recorded in the first data table includes the weight of each node, the parent node of the child node, and the depth of each node;

[0089] A second processing module is used to perform standardization processing on the first data table and output a second data table; the standardization processing refers to adjusting the number of child nodes of the parent nodes of other layers above the second-to-last parent node in the first data table to 3, and adjusting the depth of the node whose tree relationship has changed;

[0090] The third processing module is used to output a hierarchical table based on the second data table, wherein the hierarchical table records the number of leaf nodes in each layer, and each leaf node corresponds to a character; the hierarchical table is converted into M={m i , i=1,2,3……k} means, where m i is the number of leaves in each layer in the second data table, i represents the corresponding layer in the second data table, and k represents the total number of layers in the second data table; according to the recursive formula, the numbers of all leaf nodes are determined; the recursive formula is: N i =R i -m i , R i+1 =3N u +2, P i =R i +1, where R2 = 2, P i is the number of the first leaf node in the i-th layer when all leaf nodes in the second data table are sorted in ascending order according to their weights, and the number of subsequent leaf nodes in the i-th layer is the number of the previous leaf node plus one;

[0091] An encoding module, which is used to output the code corresponding to each character according to the numbers of all leaf nodes and the corresponding depths in the second data table;

[0092] A compression module is used to compress the data to be compressed according to the encoding.

[0093] For the description of the above modules, please refer to Example 1, which will not be elaborated here.

[0094] Example 3

[0095] Embodiment 3 of the present invention provides a data decompression method based on Huffman coding, which is used to decompress data obtained after being compressed by the data compression method based on Huffman coding of Embodiment 1, and mainly includes the following steps:

[0096] Step 1: According to the weight table corresponding to the data to be decompressed, the depth corresponding to each character is obtained, and the hierarchical table is output; the weight table includes the corresponding relationship between each character and its weight;

[0097] Step 2: According to the hierarchy table, determine the number of each leaf node after all leaf nodes are sorted in ascending order according to their weights by the recursive formula, and output a fourth data table; or output the fourth data table according to a coding table corresponding to the data to be decompressed, wherein the coding table includes a corresponding relationship between each character and the coding;

[0098] Step 3: Take the depth of the leaf node with the smallest depth as the smallest unit, read the data to be decompressed, and compare it with the number and depth of each leaf node in the fourth data table. If the match is successful, convert it into the original character. If the match fails, increase the number of read bits and re-match in the fourth data table until the data to be decompressed is completely decompressed.

[0099] Among them, in step one, when the hierarchical table is output according to the weight table corresponding to the data to be decompressed, the weight table is processed in turn according to the Huffman coding method to output a first data table with a ternary tree structure, the first data table is standardized to output a second data table, and the hierarchical table is output based on the second data table.

[0100] In addition, the form of the encoding table referred to in step 1 can refer to the encoding table given in Example 1. It should be noted that since the depth and number corresponding to the character can be determined by encoding, when decompressing, the obtained encoding table can only include the two items of character and encoding, without the need to supplement the depth and number. However, the above complete encoding table is easier to perform matching operations.

[0101] The fourth data table in step 2 is actually similar to the third data table in content, except that the fourth data table is obtained directly through the hierarchical table and the recursive formula, so it only includes leaf nodes and corresponding numbers.

[0102] In step three, a specific method for reading the data to be decompressed is given, in which the minimum depth in the leaf node is used as the minimum unit of reading. It should be noted that the depth range here is only considered in the leaf node, and the depth of the non-leaf node is not considered.

[0103] Specifically in this embodiment, the coded stream given in the above-mentioned embodiment 1 is decompressed.

[0104] Since it is relatively simple to decompress using a coding table, the decompression process is described based on a weight table in this embodiment.

[0105] First, it is necessary to obtain a weight table corresponding to the data to be decompressed. In the present embodiment, while transmitting the data to be decompressed, the corresponding weight table is also transmitted synchronously. After obtaining the weight table, the hierarchy table can be obtained according to the relevant processing steps in embodiment 1 according to the weight table, and the specific processing process will not be repeated. After obtaining the hierarchy table, the numbering of each leaf node can be obtained according to the given recursive formula, and then the fourth data table including the leaf node and the numbering can be formed. Afterwards, the coded stream is read, and the read code is converted into a decimal numbering, and compared with the numbering in the fourth data table and the depth of the corresponding node, and converted according to the matching result.

[0106] Specifically, according to the weight table of the string in Example 1, it can be obtained that the minimum depth of the leaf node is 2, so the encoding stream is read with 2 as the minimum reading unit. The encoding stream is:

[0107] .

[0108] First, read the first 2 bits of the encoding stream, which is 11, matching the character F, so it is converted to character F. Then read the next 2 bits, which is 12, matching the character G, so it is converted to character G. And so on. When a mismatch is encountered, continue to read the next bit. For example, when no matching character is found after reading 2 bits, read the next bit and convert it to a 3-bit encoding for matching until a match is successful.

[0109] After the above decompression steps, the coded stream of Example 1 can be decompressed again into the original string:

[0110] FGHFHFEAJBHFEJBIEJDIEHEBIFAGJEFCFCDHJDJJGJHJBBGAABCGIIHHEDIDEEIJJCGDFEEIEFCEDJFFBHHEHCAADHHBHDJJDHGHIGFDAHCDJGGDGIAGGAJIEDECCJHH GBACFGFJIJEBHFDEFJJIFHIDGFGICGBFADJAIABFJCAADCACFFGHHIGICFHAIBCJIIBCBJJFJCBDGDEACCHBACIBDGIHBEFCIJIEHDABAIDEEBEGEIJIIJHCBGGDGFAG.

[0111] Example 4

[0112] Embodiment 4 of the present invention provides a data decompression device based on Huffman coding, which is used to decompress data obtained after being compressed by the data compression method based on Huffman coding described in Embodiment 1, and mainly includes:

[0113] A first decoding module, which is used to obtain the depth corresponding to each character according to the weight table corresponding to the data to be decompressed, and output the hierarchy table; the weight table includes the corresponding relationship between each character and its weight;

[0114] A second decoding module is used to determine the number of each leaf node after all leaf nodes are sorted in ascending order of their weights according to the hierarchical table by the recursive formula, and output a fourth data table; or output the fourth data table according to a coding table corresponding to the data to be decompressed, the coding table including the corresponding relationship between each character and the code;

[0115] The decompression module is used to read the compressed data with the depth of the leaf node with the smallest depth as the smallest unit, and compare it with the number and depth of each leaf node in the fourth data table. If the match is successful, it is converted into the original character. If the match fails, the reading bit is increased by one and re-matched in the fourth data table until the data to be decompressed is completely decompressed.

[0116] For the description of the above modules, please refer to Example 3, which will not be elaborated here.

[0117] The description of the above specification and embodiments is used to explain the protection scope of the present invention, but does not constitute a limitation on the protection scope of the present invention. Through the enlightenment of the present invention or the above embodiments, ordinary technicians in this field can obtain modifications, equivalent substitutions or other improvements to the embodiments of the present invention or part of the technical features thereof through logical analysis, reasoning or limited experiments, which should be included in the protection scope of the present invention.

Claims

1. A data compression method based on Huffman coding, which is used to compress data including multiple characters, characterized in that: Processing the data to be compressed according to the Huffman coding method, and outputting a first data table with a ternary tree structure; the information recorded in the first data table includes the weight of each node, the parent node of the child node, and the depth of each node; The first data table is standardized and a second data table is output; the standardized processing refers to adjusting the number of child nodes of the parent nodes of other layers above the penultimate parent node in the first data table to 3, and adjusting the depth of the node whose tree relationship has changed; Output a hierarchical table based on the second data table, wherein the hierarchical table records the number of leaf nodes in each layer, and each leaf node corresponds to a character; the hierarchical table is represented by M={m i , i=1,2,3……k} means, where m i is the number of leaves in each layer in the second data table, i represents the corresponding layer in the second data table, and k represents the total number of layers in the second data table; According to the recursive formula, determine the number of all leaf nodes; the recursive formula is: N i =R i -m i , R i+1 =3N i +2, P i =N i +1, where R2 = 2, P i is the number of the first leaf node in the i-th layer when all leaf nodes in the second data table are sorted in ascending order according to their weights, and the number of subsequent leaf nodes in the i-th layer is the number of the previous leaf node plus one; Output the code corresponding to each character according to the numbers of all leaf nodes and the corresponding depths in the second data table; The data to be compressed is compressed according to the encoding.

2. A data compression method based on Huffman coding as claimed in claim 1, characterized in that: The code corresponding to each character is the ternary representation of the number of the leaf node corresponding to the character.

3. A data compression method based on Huffman coding as claimed in claim 2, characterized in that: The length of the code corresponding to each character is consistent with the depth of the leaf node corresponding to the character in the second data table.

4. A data compression method based on Huffman coding as claimed in claim 3, characterized in that: The hierarchical table corresponds to the third data table, and the third data table is obtained by normalizing the second data table; the normalization process refers to adjusting the non-leaf nodes of each layer in the second data table to before the leaf nodes, and sorting the leaf nodes of the layer in ascending order with reference to the weight of each node; In the recursive formula, R i Indicates the node number at the last position in the i-th layer of the third data table, N i Indicates the number of the last non-leaf node in the third data table; when node N i When only two child nodes are included, R in the recursive formula i+1 =3N i +2 adjusted to R i+1 =3N i +1.

5. A data compression method based on Huffman coding as claimed in claim 4, characterized in that: The processing of the data to be compressed according to the Huffman coding method means that the weights of the characters in the compressed data are calculated according to the frequency, starting with one character corresponding to one node, and the three nodes with the lowest weights are cyclically added, and at each addition, the depth of the nodes and their child nodes involved in the addition operation is increased by one.

6. A data compression method based on Huffman coding as claimed in claim 5, characterized in that: The standardized processing of the first data table means that when there is a parent node with only two or fewer child nodes, if the depth of its child node is not the maximum value among all depths, then a node with the largest weight is selected from the next layer of its child node as its new child node, and the weight and depth of the child node are modified until the number of child nodes of the parent nodes of other layers above the penultimate layer are all 3.

7. A data compression device based on Huffman coding, which is used to compress data including multiple characters, characterized in that: include: A first processing module, which is used to process the data to be compressed according to the Huffman coding method and output a first data table with a ternary tree structure; The information recorded in the first data table includes the weight of each node, the parent node of the child node, and the depth of each node; A second processing module is used to perform standardization processing on the first data table and output a second data table; the standardization processing refers to adjusting the number of child nodes of the parent nodes of other layers above the second-to-last parent node in the first data table to 3, and adjusting the depth of the node whose tree relationship has changed; The third processing module is used to output a hierarchical table based on the second data table, wherein the hierarchical table records the number of leaf nodes in each layer, and each leaf node corresponds to a character; the hierarchical table is converted into M={m i , i=1,2,3……k} means, where m i is the number of leaves in each layer in the second data table, i represents the corresponding layer in the second data table, and k represents the total number of layers in the second data table; according to the recursive formula, the numbers of all leaf nodes are determined; the recursive formula is: N i =R i -m i , R i+1 =3N i +2, P i =N i +1, where R2 = 2, P i is the number of the first leaf node in the i-th layer when all leaf nodes in the second data table are sorted in ascending order according to their weights, and the number of subsequent leaf nodes in the i-th layer is the number of the previous leaf node plus one; An encoding module, which is used to output the code corresponding to each character according to the numbers of all leaf nodes and the corresponding depths in the second data table; A compression module is used to compress the data to be compressed according to the encoding.

8. A data decompression method based on Huffman coding, used for decompressing data obtained after being compressed by the data compression method based on Huffman coding as claimed in claim 6, characterized in that: According to the weight table corresponding to the data to be decompressed, the depth corresponding to each character is obtained, and the hierarchical table is output; the weight table includes the corresponding relationship between each character and its weight; According to the hierarchy table, the number of each leaf node after all leaf nodes are sorted in ascending order according to their weights is determined by the recursive formula, and a fourth data table is output; or the fourth data table is output according to a coding table corresponding to the data to be decompressed, wherein the coding table includes a corresponding relationship between each character and a coding; The data to be decompressed is read with the depth of the leaf node with the smallest depth as the smallest unit, and compared with the number and depth of each leaf node in the fourth data table. If the match is successful, it is converted into the original character. If the match fails, the number of read bits is increased and re-matched in the fourth data table until the data to be decompressed is completely decompressed.

9. A data decompression method based on Huffman coding as claimed in claim 8, characterized in that: When the hierarchical table is output according to the weight table corresponding to the data to be decompressed, the weight table is processed in turn according to the Huffman coding method to output a first data table with a ternary tree structure, the first data table is standardized to output a second data table, and the hierarchical table is output based on the second data table.

10. A data decompression device based on Huffman coding, used for decompressing data compressed by the data compression method based on Huffman coding as claimed in claim 6, characterized in that: include: A first decoding module, which is used to obtain the depth corresponding to each character according to the weight table corresponding to the data to be decompressed, and output the hierarchy table; the weight table includes the corresponding relationship between each character and its weight; A second decoding module is used to determine the number of each leaf node after all leaf nodes are sorted in ascending order of their weights according to the hierarchical table by the recursive formula, and output a fourth data table; or output the fourth data table according to a coding table corresponding to the data to be decompressed, the coding table including the corresponding relationship between each character and the code; The decompression module is used to read the compressed data with the depth of the leaf node with the smallest depth as the smallest unit, and compare it with the number and depth of each leaf node in the fourth data table. If the match is successful, it is converted into the original character. If the match fails, the reading bit is increased by one and re-matched in the fourth data table until the data to be decompressed is completely decompressed.

Citation Information

Cited By

  • Unmanned aerial vehicle cluster adaptive communication method, system and program product

    CN120239121A

  • Model parameter determination method, model reasoning system, decoding device and electronic equipment

    CN120745717A

  • A model parameter determination method, a model inference system, a decoding device, and an electronic device

    CN120745717B