Huffman coding method, system, apparatus, and medium
By using hardware parallel operations to adjust the depth of the binary tree, the problems of Hoffman encoding hardware resource overhead and complexity are solved, which improves decoding efficiency and saves hardware resources.
Patent Information
- Application Number
- PCT/CN2024/089505
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2023-06-28
- Filing Date
- 2024-04-24
- Publication Date
- 2025-01-02
AI Technical Summary
Hoffman encoding leads to a large hardware resource overhead during the hardware implementation process, increasing the complexity of encoding and decoding, and affecting efficiency.
By utilizing hardware parallel operations, the maximum length of character encoding is limited, the depth of binary tree is adjusted, the complexity of Hoffman decoding is reduced, and hardware resources are saved.
It realizes the adjustment of binary tree structure in a short time, improves Hoffmann decoding efficiency, and reduces hardware resource consumption.
Smart Images

Figure CN2024089505_02012025_PF_FP_ABST
Abstract
Description
Huffman coding method, system, device and medium
[0001] This application claims priority to the Chinese patent application filed with the China Patent Office on June 28, 2023, with application number 202310769907.3 and invention name “A Huffman Coding Method, System, Device and Medium”, the entire contents of which are incorporated by reference into this application. Technical Field
[0002] The present invention relates to the technical field of data compression, and in particular to a Huffman coding method, system, device and medium. Background Art
[0003] Huffman coding is a compression algorithm based on minimal redundancy coding. It reduces the storage space required by a data set by encoding the probability of a character appearing in that data. This special encoding method uses fewer bits to encode frequently occurring symbols and more bits to encode less frequently occurring symbols.
[0004] The entire Huffman coding compressed data requires building a standard binary tree based on the frequency of character occurrence. The depth of the binary tree can reach 255 layers, and the longest character encoding can be up to 255. At the same time, the hardware resource overhead will be large during the hardware coding implementation, and the complexity of the Huffman coding will be increased, which will affect the efficiency and resource consumption of the Huffman decoding.
[0005] Therefore, how to save hardware resource overhead and reduce the complexity of Huffman coding is an urgent problem that needs to be solved by those skilled in the art.
[0006] Summary of the Invention
[0007] The purpose of the present invention is to provide a Huffman coding method, system, device and medium, which utilize hardware parallel computing to limit the maximum length of character encoding and reduce the complexity of Huffman decoding to improve Huffman decoding efficiency and save hardware resources.
[0008] To solve the above technical problems, the present invention provides a Huffman encoding method, which is applied to a Huffman encoding circuit. The method includes:
[0009] Get the target data and preset binary tree depth value;
[0010] Constructing an initial binary tree for the target data based on frequency distribution, and obtaining an initial depth value of the initial binary tree;
[0011] In a case where the initial depth value exceeds the preset binary tree depth value, determining the number of nodes to be adjusted in the initial binary tree according to the preset binary tree depth value and the initial depth value;
[0012] Determining a binary tree adjustment strategy for the initial binary tree according to the number of nodes to be adjusted and the number of empty nodes, and adjusting the initial binary tree according to the binary tree adjustment strategy;
[0013] Among them, the empty node is an additional node generated after moving one or more leaf nodes from the first layer of the initial binary tree to the layer before the layer where the preset binary tree depth value is located to the layer where the preset binary tree depth value is located.
[0014] Preferably, the preset binary tree depth value is determined at least by a compression rate of Huffman coding.
[0015] Preferably, the determining the number of nodes to be adjusted of the initial binary tree according to the preset binary tree depth value and the initial depth value includes:
[0016] Obtain the parent node and character node corresponding to the layer beyond the preset binary tree depth value;
[0017] The sum of the number of the parent nodes and the number of the character nodes is used as the number of the nodes to be adjusted in the initial binary tree.
[0018] Preferably, the hollow nodes include theoretical hollow nodes and actual hollow nodes, and the determining of the binary tree adjustment strategy of the initial binary tree according to the number of the nodes to be adjusted and the number of hollow nodes includes:
[0019] Divide the number of nodes to be adjusted by 2 to determine the number of theoretical void nodes;
[0020] Starting from the layer before the layer where the preset binary tree depth value is located to each leaf node in the first layer of the initial binary tree, respectively move to the layer where the preset binary tree depth value is located, and generate the actual empty node in the layer where the preset binary tree depth value is located to determine the number of the actual empty nodes;
[0021] The binary tree adjustment strategy is determined according to the relationship between the number of nodes to be adjusted, the number of theoretical empty nodes, and the number of actual empty nodes.
[0022] Preferably, the process of determining the actual number of hollow nodes includes:
[0023] Subtracting the preset binary tree depth value from the sequence number of each layer of the initial binary tree to obtain a layer difference value corresponding to each layer;
[0024] Using the layer difference value corresponding to each layer as a power exponent and 2 as a variable to obtain the number of initial actual hole nodes constructed at the layer where the preset binary tree depth value is located;
[0025] Subtract 1 from the initial number of actual hole nodes in each layer to obtain the number of actual hole nodes in each layer constructed at the layer where the preset binary tree depth value is located;
[0026] The number of actual hole nodes in each layer is summed to obtain the number of actual hole nodes.
[0027] Preferably, determining the binary tree adjustment strategy according to the relationship between the number of nodes to be adjusted, the number of theoretical empty nodes, and the number of actual empty nodes includes:
[0028] Determining whether the number of actual void nodes is greater than the number of theoretical void nodes;
[0029] If yes, the actual number of void nodes is subtracted from the theoretical number of void nodes to obtain a void node difference;
[0030] The empty node difference is used as the adjusted number of leaf nodes;
[0031] Moving the leaf nodes corresponding to the node to be adjusted to a layer before the layer where the preset binary tree depth value is located according to the adjusted number of leaf nodes to determine an initial binary tree adjustment strategy;
[0032] Adjusting the initial binary tree according to the initial binary tree adjustment strategy to obtain the adjusted initial binary tree;
[0033] The binary tree adjustment strategy is determined according to the relationship between the adjusted initial binary tree and the current number of hollow nodes.
[0034] Preferably, determining the binary tree adjustment strategy according to the relationship between the adjusted initial binary tree and the current number of hollow nodes includes:
[0035] Determining the number of current empty nodes in the adjusted initial binary tree;
[0036] If the number of the current hole nodes is not 0, move the character node corresponding to the layer where the current hole node is located to the layer before the layer where the current hole node is located;
[0037] Sort the frequencies of the target data in the layer where the preset binary tree depth value is located according to the frequency sorting order from large to small, and select the character node of the target data corresponding to the target frequency as the target moving character node;
[0038] Moving the target moving character node to a layer before the layer where the preset binary tree depth value is located to determine a final binary tree;
[0039] If the current number of holes is 0, the initial binary tree adjustment strategy is determined to be the binary tree adjustment strategy.
[0040] To solve the above technical problems, the present invention further provides a Huffman coding system based on hardware resources, which is applied to a Huffman coding circuit and includes:
[0041] An acquisition module is used to obtain target data and preset binary tree depth value;
[0042] A construction module, configured to construct an initial binary tree for the target data based on frequency distribution, and obtain an initial depth value of the initial binary tree;
[0043] a determination module, configured to determine, when the initial depth value exceeds the preset binary tree depth value, the number of nodes to be adjusted of the initial binary tree according to the preset binary tree depth value and the initial depth value;
[0044] An adjustment module, configured to determine a binary tree adjustment strategy for the initial binary tree according to the number of nodes to be adjusted and the number of empty nodes, and adjust the initial binary tree according to the binary tree adjustment strategy;
[0045] Among them, the empty node is an additional node generated after moving one or more leaf nodes from the first layer of the initial binary tree to the layer before the layer where the preset binary tree depth value is located to the layer where the preset binary tree depth value is located.
[0046] In order to solve the above technical problems, the present invention further provides a Huffman encoding device, comprising:
[0047] Memory for storing computer programs;
[0048] A processor is configured to implement the steps of the Huffman coding method described above when executing the computer program.
[0049] To solve the above technical problems, the present invention further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the Huffman coding method described above are implemented.
[0050] The present invention provides a Huffman coding method, which is applied to a Huffman coding circuit. The method fully utilizes hardware parallel computing during the process of constructing and adjusting a binary tree. The depth value of the binary tree is constrained to obtain a preset binary tree depth value, thereby limiting the maximum length of a character code. The binary tree structure adjustment is completed in a relatively short time to implement Huffman coding. The entire coding process does not require the introduction of additional time overhead. Because the depth of the binary tree is constrained, when the preset binary tree depth value is exceeded, the number of nodes to be adjusted in the initial binary tree is determined based on the preset binary tree depth value and the initial depth value. A binary tree adjustment strategy for the initial binary tree is determined based on the number of nodes to be adjusted and the number of empty nodes. The initial binary tree is adjusted based on the strategy, reducing the complexity of Huffman decoding, improving Huffman decoding efficiency, and saving hardware resources.
[0051] In addition, the present invention also provides a Huffman coding system, device and medium, which have the same beneficial effects as the above-mentioned Huffman coding method. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] In order to more clearly illustrate the embodiments of the present invention, the following is a brief introduction to the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0053] FIG1 is a schematic diagram of a binary tree constructed using existing Huffman coding;
[0054] FIG2 is a flow chart of a Huffman encoding method provided by an embodiment of the present invention;
[0055] FIG3 is a hardware structure diagram of a Huffman encoding circuit provided by an embodiment of the present invention;
[0056] FIG4 is a schematic diagram of a binary tree to be adjusted according to an embodiment of the present invention;
[0057] FIG5 is a schematic diagram of an actual hollow node provided by an embodiment of the present invention;
[0058] FIG6 is a schematic diagram of an adjusted binary tree provided by an embodiment of the present invention;
[0059] FIG7 is a schematic diagram of a final binary tree structure provided by an embodiment of the present invention;
[0060] FIG8 is a structural diagram of a Huffman coding system provided by an embodiment of the present invention;
[0061] FIG9 is a structural diagram of a Huffman encoding device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0062] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making any creative efforts shall fall within the scope of protection of the present invention.
[0063] The core of the present invention is to provide a Huffman coding method, system, device and medium, which utilizes hardware parallel computing to limit the maximum length of character encoding and reduce the complexity of Huffman decoding to improve Huffman decoding efficiency and save hardware resources.
[0064] In order to enable those skilled in the art to better understand the present invention, the present invention will be further described in detail below with reference to the accompanying drawings and specific implementation methods.
[0065] It should be noted that Huffman coding compresses data by first constructing a standard binary tree based on the frequency of occurrence of characters. After the binary tree is constructed, each character is encoded. The encoding length is determined by the position of the character in the binary tree. The encoding rule is to traverse from the parent node to each leaf node. If the path is on the left side of the parent node, the encoding is 0, and if the path is on the right side of the parent node, the encoding is 1. Figure 1 is a schematic diagram of the binary tree constructed by the existing Huffman coding. As shown in Figure 1, the final encoding is: character a: 2'b00; character b: 2'b01; character f: 2'b11; character c: 3'b100; character d: 4'b1010; character e: 4'b1011; Before compression, each character requires 8 bits to identify, and the entire information requires 400 bits to represent, the formula is (9+12+6+3+5+15)*8bits=400bits. After compression, the information is less than the original information and needs to be represented by 122bits, the formula is 9*2bist+12*2bits+15*2bits+6*3bits+3*4bits+5*4bits=122bits. Although Huffman coding saves the number of characters, the maximum binary tree depth can reach 255, and the longest character encoding can be up to 255. At the same time, it leads to excessive hardware resource overhead during hardware implementation, and also increases the complexity of the subsequent Huffman decoding module, affecting the efficiency of Huffman decoding and increasing the resource consumption of the Huffman decoding module. The Huffman coding method provided by the present invention can solve the above technical problems.
[0066] FIG2 is a flow chart of a Huffman encoding method provided by an embodiment of the present invention. As shown in FIG2 , the method is applied to a Huffman encoding circuit, including:
[0067] S11: Obtain target data and preset binary tree depth value.
[0068] It can be understood that the target data comes from the target file. The target file can be any plaintext data. There is no restriction on the source of the plaintext. It can be network data, data of specific application scenarios, such as e-commerce data, or traffic data, etc. At the same time, the plaintext data can also be a previously encrypted file, and the plaintext file generated after the decryption algorithm can be used as the target file.
[0069] The character data appearing in the target file is used as the target data. This can be obtained by performing data analysis on the target file or directly obtaining the target data. The preset binary tree depth value can be set based on a maximum constraint value to maintain a balance of hardware resources according to product specifications, or it can be set based on current experience or determined based on the encoding compression rate.
[0070] As an embodiment, the preset binary tree depth value is determined by at least a compression rate of Huffman coding.
[0071] Huffman coding is a very effective coding method widely used for data file compression. Its compression rate is usually between 20% and 90%. It is a lossless compression method.
[0072] It should be noted that the encoding method provided by the present invention is applied to the Huffman encoding circuit. FIG3 is a hardware structure diagram of a Huffman encoding circuit provided by an embodiment of the present invention. As shown in FIG3 , the Huffman encoding circuit 1 includes a character frequency statistics module, a data parallel sorting module, a tree building module, a leaf node management module, a tree adjustment module and an encoding module. The character frequency statistics module is connected to the data parallel sorting module. After the character frequency statistics, the data is sent to the data parallel sorting module for data parallel sorting. The main function of the character frequency statistics module is to count the number of occurrences of each character in the input plain text file (plain text). The main function of the data parallel sorting module is to sort the characters in the plain text file according to the number of characters from small to large. When the number of characters is the same, the characters are sorted according to their own size. The data after parallel sorting is sent to the tree building module to build a binary tree. The main function of the tree building module is to build a standard binary tree based on the data parallel sorting module for the characters appearing in the plaintext file; the main function of the tree adjustment module is to adjust the position of the leaf node in the binary tree when the tree building module is completed, if the maximum depth of the standard binary tree exceeds the maximum depth D, and finally generate a standard binary tree with a depth not exceeding D. The tree building module and the leaf node management module adjust to obtain the adjusted binary tree, and then input it into the encoding module to implement Huffman coding to output the ciphertext file. The leaf node management module records the depth information and character encoding information of each character in the plaintext file in the binary tree; the encoding module refers to the process of replacing the characters in the plaintext file with character encoding according to the character encoding information in the leaf node management module to generate a ciphertext file.
[0073] S12: Construct an initial binary tree for the target data based on the frequency distribution, and obtain an initial depth value of the initial binary tree.
[0074] Based on the coding principle of Huffman coding, the frequency of each character's occurrence is counted and sorted from small to large according to the frequency distribution. When the number of characters is the same, they are sorted according to the size of the characters themselves. This is not limited here and can be referenced based on the existing Huffman coding process. The binary tree constructed for the target data is the initial binary tree. Because the present invention takes into account the overhead of hardware resources, it is necessary to obtain the initial depth value of the initial binary tree for comparison with the preset binary tree depth value.
[0075] S13: Determine whether the initial depth value exceeds the preset binary tree depth value. If so, proceed to step S14; if not, proceed to step S16.
[0076] If the initial depth value does not exceed the preset binary tree depth value, it means that the currently constructed initial binary tree will not consume too much hardware resources. The current initial binary tree is maintained without adjustment. If the initial depth value exceeds the preset binary tree depth value, the initial binary tree is adjusted to avoid consuming too much hardware resources and increasing the complexity of the decoding process.
[0077] S14: Determine the number of nodes to be adjusted in the initial binary tree according to the preset binary tree depth value and the initial depth value.
[0078] S15: determining a binary tree adjustment strategy for the initial binary tree according to the number of nodes to be adjusted and the number of empty nodes, and adjusting the initial binary tree according to the binary tree adjustment strategy;
[0079] Among them, the empty node is an additional node generated after moving one or more leaf nodes from the first layer of the initial binary tree to the layer before the layer where the preset binary tree depth value is located to the layer where the preset binary tree depth value is located.
[0080] It can be understood that a hollow node is an additional node generated after moving one or more leaf nodes from the first layer of the initial binary tree to the layer before the layer where the preset binary tree depth value is located to the layer where the preset binary tree depth value is located. It can also be a leaf node that can still be mounted in the initial binary tree and is not occupied by the character corresponding to the current target data. Specifically, if the additional nodes generated after moving a leaf node are sufficient, just move the leaf node. If the additional nodes generated after moving a leaf node are not sufficient, continue to move other leaf nodes in the same layer. If there are no other leaf nodes in the same layer, move the leaf nodes of the upper layer until the additional nodes are sufficient. As shown in Figure 1, its hollow nodes can be a, b, f, c, d, and e, and its attribute is that the leaf nodes can be expanded within the range of the preset binary tree depth value. Figure 1 is only a schematic diagram to illustrate the hollow nodes, and is not the hollow node that needs to be established in the present invention.
[0081] Considering that the adjusted binary tree depth value does not exceed the preset binary tree depth value, the range of its empty node establishment is to move one or more leaf nodes from the first layer of the initial binary tree to the layer before the preset binary tree depth value to the additional nodes generated after the layer where the preset binary tree depth value is located is located.
[0082] The present invention mainly adjusts the leaf nodes in the initial binary tree that are outside the layer where the preset binary tree depth value is located, so as to meet the requirements of being within the preset binary tree depth value and including the layer where the preset binary tree depth value is located, so that the depth value of the final adjusted binary tree is not too deep.
[0083] First, the number of nodes to be adjusted is determined, and then the relationship between the number of these nodes and the number of empty nodes determines the binary tree adjustment strategy. This embodiment does not consider the ordering of the two characters corresponding to the minimum frequency of Huffman coding. The binary tree adjustment strategy is determined solely based on the condition that the depth value is less than or equal to the preset binary tree depth value. The adjustment method can be based on a specific algorithm or on an adjustment path to simplify the adjustment path. This is not limited here and can be set according to actual circumstances.
[0084] As an embodiment, determining the number of nodes to be adjusted includes:
[0085] Get the parent node and character node corresponding to the layer beyond the preset binary tree depth value;
[0086] The sum of the number of parent nodes and character nodes is used as the number of nodes to be adjusted in the initial binary tree.
[0087] Specifically, the premise of this embodiment is that the initial depth value exceeds the preset binary tree depth value, so it is necessary to obtain the parent nodes and character nodes corresponding to the layer outside the preset binary tree depth value, that is, obtain the parent nodes and character nodes corresponding to the layer outside the layer where the initial binary tree exceeds the preset binary tree depth value, and use the parent nodes and character nodes as nodes to be adjusted, and the number of nodes to be adjusted is the number of parent nodes and character nodes.
[0088] Finally, the initial binary tree can be adjusted according to the determined binary tree adjustment strategy.
[0089] S16: Use the initial binary tree as the final binary tree.
[0090] When the initial depth value of the binary tree does not exceed the preset binary tree depth value, the initial binary tree does not need to be adjusted and can be directly used as the final binary tree.
[0091] A Huffman coding method provided by an embodiment of the present invention is applied to a Huffman coding circuit. In the process of constructing and adjusting a binary tree, the parallel operation of the hardware is fully utilized, the depth value of the binary tree is constrained to obtain a preset binary tree depth value, thereby limiting the maximum length of the character encoding, and the tree structure adjustment of the binary tree is completed in a relatively short time to implement Huffman coding. The entire coding process does not require the introduction of additional time overhead; because the depth of the binary tree is constrained, when the preset binary tree depth value is exceeded, the number of nodes to be adjusted in the initial binary tree is determined based on the preset binary tree depth value and the initial depth value; the binary tree adjustment strategy of the initial binary tree is determined based on the number of nodes to be adjusted and the number of empty nodes, and the initial binary tree is adjusted according to the strategy, reducing the complexity of Huffman decoding, thereby improving Huffman decoding efficiency and saving hardware resources.
[0092] It should be noted that the empty nodes include theoretical empty nodes and actual empty nodes. Based on the comparison process of the two types of empty nodes, it can be ensured that the final adjusted binary tree does not have redundant empty nodes. The empty nodes set in this embodiment are to facilitate the adjustment process of leaf nodes. The binary tree adjustment strategy of the initial binary tree is determined based on the number of nodes to be adjusted and the number of empty nodes in step S14, including:
[0093] Divide the number of nodes to be adjusted by 2 to determine the number of theoretical empty nodes;
[0094] Starting from the layer before the layer where the preset binary tree depth value is located, each leaf node in the first layer of the initial binary tree is moved to the layer where the preset binary tree depth value is located, and an actual hollow node is generated in the layer where the preset binary tree depth value is located to determine the number of actual hollow nodes;
[0095] The binary tree adjustment strategy is determined according to the relationship between the number of theoretical empty nodes and the number of actual empty nodes.
[0096] Specifically, the theoretical number of empty nodes is first calculated, that is, the number of empty nodes corresponding to the theoretically achievable adjustment strategy. Based on the relationship between the initial binary tree and the preset binary tree depth value, the theoretical number of additional empty nodes that can be generated at the layer corresponding to the preset binary tree depth value is calculated. In this embodiment, the number of empty nodes is determined primarily based on the relationship between the total number of characters involved in the subtraction and the total number of leaf nodes and parent nodes occupied by the characters in each layer. That is, the number of nodes to be adjusted is divided by 2 to obtain the theoretical number of empty nodes.
[0097] It should be noted that this embodiment obtains the number of theoretical empty nodes based on the nodes to be adjusted. It can also be obtained based on the character leaf nodes and parent nodes of each layer of the initial binary tree. As an embodiment, the process of determining the number of theoretical empty nodes includes:
[0098] Get the number of character leaf nodes and parent nodes at each level of the initial binary tree and the total number of nodes in the initial binary tree;
[0099] Subtract the number of leaf nodes and parent nodes of each layer from the total number of nodes in the initial binary tree to get the number of initial theoretical empty nodes;
[0100] The number of theoretical void nodes is obtained by dividing the initial number of theoretical void nodes by 2.
[0101] Specifically, the total number of nodes in a complete binary tree is 2n-1, where n is the number of characters. Based on the total number of nodes in the initial binary tree, subtract the leaf nodes of each layer. The leaf nodes of each layer in this embodiment include the leaf nodes of the 0th to the nth layers to obtain the initial theoretical number of empty nodes. Since a node can be used as a parent node, its parent node can be mounted on two leaf nodes. After calculating the empty nodes corresponding to all the leaf nodes that can be mounted, the calculation is repeated here. It is necessary to divide by 2 to process the repeated empty nodes to obtain the final theoretical number of empty nodes. The formula is as follows: X = (2n-2-num(1)-num(2)-...-num(D)) / 2;
[0102] Here, n represents the total number of characters involved in tree construction. In the formula, 2n-2 = 2n-1-1, where 2n-1 is the total number of binary tree nodes, minus 1, minus the leaf nodes at level 0. In num(i), i = 1-D layers, which refers to the total number of leaf nodes and parent nodes of the character at level i in the binary tree. D is the preset binary tree depth, and X is the theoretical number of empty nodes.
[0103] The number of actual hollow nodes is calculated by considering that the leaf nodes from the previous layer to the first layer of the layer where the preset binary tree depth value is located are moved to the D layer respectively, and a sufficient number of hollow nodes can be generated as the number of actual hollow nodes.
[0104] The binary tree adjustment strategy is determined based on the relationship between the number of the two types of empty nodes and the number of nodes to be adjusted. If the actual number of empty nodes is the same as the theoretical number of empty nodes, the current binary tree depth value is adjusted based on the actual number of empty nodes as the adjustment strategy. If the actual number of empty nodes is greater than the theoretical number of empty nodes, further adjustments are required until the adjusted leaf nodes are moved to the empty nodes, leaving zero empty nodes after the movement.
[0105] As an embodiment, the process of determining the actual number of hollow nodes includes:
[0106] Subtract the preset binary tree depth value from the sequence number of each layer of the initial binary tree to obtain the layer difference value corresponding to each layer;
[0107] The layer difference value corresponding to each layer is used as the power exponent and 2 is used as the variable to obtain the number of initial actual hollow nodes constructed at the layer where the preset binary tree depth value is located;
[0108] Subtract 1 from the initial number of actual hole nodes in each layer to obtain the number of actual hole nodes in each layer constructed at the layer where the preset binary tree depth value is located;
[0109] The number of actual hollow nodes in each layer is summed up to obtain the number of actual hollow nodes.
[0110] Specifically, the leaf nodes of layers D-1 to 1 are moved to the F layer in sequence until enough empty nodes N are generated. The leaf nodes of layer i are moved to layer D, which can generate additional actual empty nodes in layer D. The layer difference values corresponding to each layer are obtained by subtracting the values between layer i and layer D. With 2 as the variable and the layer difference as the power exponent, the formula for the initial number of actual empty nodes in each layer is: 2 (D-i) Since the empty node is a node that is expanded based on the original leaf node as the parent node, the parent node is occupied by a character. Therefore, 1 needs to be subtracted. Here, 1 is the parent node occupied by the character. The actual number of empty nodes in each layer can be obtained by adding up the actual number of empty nodes in each layer.
[0111] After the number of actual hollow nodes is generated, the leaf nodes outside the preset binary tree depth value D layer can be directly moved to the hollow nodes of the D layer.
[0112] As an embodiment, determining a binary tree adjustment strategy based on the relationship between the number of nodes to be adjusted, the theoretical number of empty nodes, and the actual number of empty nodes includes:
[0113] Determine whether the actual number of void nodes is greater than the theoretical number of void nodes;
[0114] If so, the actual number of void nodes is subtracted from the theoretical number of void nodes to obtain the void node difference;
[0115] The difference between the empty nodes is used as the number of leaf nodes to adjust;
[0116] According to the number of leaf nodes adjusted, the leaf nodes corresponding to the nodes to be adjusted are moved to the previous layer of the layer where the preset binary tree depth value is located to determine the initial binary tree adjustment strategy;
[0117] Adjusting the initial binary tree according to the initial binary tree adjustment strategy to obtain an adjusted initial binary tree;
[0118] The binary tree adjustment strategy is determined according to the relationship between the adjusted initial binary tree and the current number of empty nodes.
[0119] Specifically, when the actual number of empty nodes is greater than the theoretical number of empty nodes, the empty node difference obtained by subtracting the theoretical number of empty nodes from the actual number of empty nodes is used as the number of leaf nodes to be adjusted. Based on this number, the leaf nodes of the D layer (that is, the nodes to be adjusted) are moved to the D-1 layer to determine the final binary tree adjustment strategy.
[0120] For example, a binary tree is constructed with six characters ABCD'EF. FIG4 is a schematic diagram of a binary tree to be adjusted provided by an embodiment of the present invention. As shown in FIG4 , there is one leaf node A on the first layer, one leaf node B on the second layer, no leaf node on the third layer, and four leaf nodes CD'EF on the fourth layer. The depth of the tree is 4. If the tree depth is to be adjusted to 3 (the preset binary tree depth value), all four nodes on the fourth layer need to be moved to the third layer. In order to move all four nodes on the fourth layer to the third layer, it is necessary to generate X=2 additional empty nodes on the third layer (n=6, num(1)=2, num(2)=2, num(3)=2).
[0121] When actual empty nodes are generated, Figure 5 is a schematic diagram of an actual empty node provided by an embodiment of the present invention. As shown in Figure 5, the leaf nodes of the second layer are first moved to the third layer, generating M0 = 3 empty nodes (4 nodes in Figure 5). At this time, there are not enough empty nodes, and the leaf nodes of the first layer need to be moved to the fourth layer to generate M1 = 3 empty nodes (1 node, 2 nodes and 3 nodes in Figure 5). After the leaf nodes AB are moved to the third layer, the structure of the tree is as shown in Figure 5. There are a total of M0 + M1 = 4 empty nodes in the third layer. Figure 6 is a schematic diagram of an adjusted binary tree provided by an embodiment of the present invention. As shown in Figure 6, the depth of the binary tree can then be adjusted. The F leaf node can be moved to the 4 empty node, the E leaf node can be moved to the parent node p1, the C leaf node can be moved to the 3 empty node, and the D' leaf node can be moved to the parent node p0.
[0122] After adjusting the initial binary tree according to the initial binary tree adjustment strategy, the adjusted initial binary tree is obtained. Since all empty nodes need to be eliminated, it is necessary to check the current number of empty nodes. If the current number of empty nodes is 0, the adjustment is completed. If the current number of empty nodes is not 0, the adjustment is continued until the current number of empty nodes is 0.
[0123] Based on the above embodiment, as an embodiment, a binary tree adjustment strategy is determined according to the relationship between the adjusted initial binary tree and the current number of hollow nodes, including:
[0124] Determine the current number of empty nodes in the adjusted initial binary tree;
[0125] If the current number of empty nodes is not 0, move the character node corresponding to the layer where the current empty node is located to the layer before the layer where the current empty node is located;
[0126] Sort the frequencies of the target data in the layer where the preset binary tree depth value is located according to the frequency sorting order from large to small, and select the character node of the target data corresponding to the target frequency as the target moving character node;
[0127] Move the target moving character node to the previous layer of the layer where the preset binary tree depth value is located to determine the final binary tree;
[0128] If the current number of holes is 0, the initial binary tree adjustment strategy is determined to be the binary tree adjustment strategy.
[0129] Specifically, after completing the current adjustment, obtain the adjusted initial binary tree, and determine whether the adjusted initial binary tree has any empty nodes. If so, continue adjusting. It should be noted that the adjusted initial binary tree in the above embodiment satisfies that its depth value is less than or equal to the preset binary tree depth value, that is, hardware resources are saved on the basis of the original initial binary tree. Since the current number of empty nodes still exists in this implementation, the corresponding character nodes in the layer where the current empty nodes are located are moved to the corresponding previous layer. Which character nodes are moved to the corresponding previous layer needs to be sorted according to the frequency distribution, and characters with higher frequencies are preferentially selected as target mobile character nodes. For the target frequency, the first number of frequencies after sorting from large to small can be determined based on the current number of empty nodes. The target mobile character node is moved to the previous layer of the layer where the preset binary tree depth value is located to determine the final binary tree, that is, to determine the final binary tree adjustment strategy.
[0130] Combined with the above example, as shown in Figure 6, there are still two empty nodes in the third layer, and the two leaf nodes in the third layer nodes need to be moved to the second layer. Figure 7 is a schematic diagram of a final binary tree structure provided by an embodiment of the present invention. As shown in Figure 7, the adjusted binary tree structure is finally completed.
[0131] The binary tree adjustment strategy provided by the embodiment of the present invention constrains the depth of the binary tree. When the depth of the binary tree exceeds a preset value, the initial binary tree is adjusted according to the preset depth of the binary tree and the number of empty nodes, thereby saving hardware resources.
[0132] The above describes in detail various embodiments corresponding to the Huffman coding method. On this basis, the present invention also discloses a Huffman coding system corresponding to the above method. FIG8 is a structural diagram of a Huffman coding system provided by an embodiment of the present invention. As shown in FIG8 , the Huffman coding system includes:
[0133] An acquisition module 11 is used to acquire target data and a preset binary tree depth value;
[0134] A construction module 12 is used to construct an initial binary tree for the target data based on the frequency distribution and obtain an initial depth value of the initial binary tree;
[0135] A determination module 13 is configured to determine the number of nodes to be adjusted in the initial binary tree according to the preset binary tree depth value and the initial depth value when the initial depth value exceeds the preset binary tree depth value;
[0136] An adjustment module 14 is configured to determine a binary tree adjustment strategy for the initial binary tree according to the number of nodes to be adjusted and the number of empty nodes, and adjust the initial binary tree according to the binary tree adjustment strategy;
[0137] Among them, the empty node is an additional node generated after moving one or more leaf nodes from the first layer of the initial binary tree to the layer before the layer where the preset binary tree depth value is located to the layer where the preset binary tree depth value is located.
[0138] Since the embodiments of the system part correspond to the above embodiments, the embodiments of the system part please refer to the description of the embodiments of the method part, which will not be repeated here.
[0139] For an introduction to a Huffman coding system provided by the present invention, please refer to the above method embodiment, and the present invention will not be described in detail here. It has the same beneficial effects as the above Huffman coding method.
[0140] FIG9 is a structural diagram of a Huffman encoding device provided by an embodiment of the present invention. As shown in FIG9 , the device includes:
[0141] Memory 21, for storing computer programs;
[0142] The processor 22 is configured to implement the steps of the Huffman coding method when executing a computer program.
[0143] Among them, the processor 22 may include one or more processing cores, such as a 4-core processor, an 8-core processor, etc. The processor 22 can be implemented in at least one hardware form of a digital signal processor (DSP), a field programmable gate array (FPGA), and a programmable logic array (PLA). The processor 22 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the awake state, also known as a central processing unit (CPU); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 22 may be integrated with a graphics processing unit (GPU), which is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 22 may also include an artificial intelligence (AI) processor, which is used to process computing operations related to machine learning.
[0144] The memory 21 may include one or more computer-readable storage media, which may be non-transitory. The memory 21 may also include a high-speed random access memory, and a non-volatile memory, such as one or more disk storage devices, flash memory storage devices. In this embodiment, the memory 21 is at least used to store the following computer program 211, wherein, after the computer program is loaded and executed by the processor 22, it can implement the relevant steps of the Huffman coding method disclosed in any of the aforementioned embodiments. In addition, the resources stored in the memory 21 may also include an operating system 212 and data 213, etc., and the storage method may be temporary storage or permanent storage. Among them, the operating system 212 may include Windows, Unix, Linux, etc. The data 213 may include but is not limited to data involved in the Huffman coding method, etc.
[0145] In some embodiments, the Huffman encoding device may further include a display screen 23 , an input / output interface 24 , a communication interface 25 , a power supply 26 , and a communication bus 27 .
[0146] Those skilled in the art will appreciate that the structure shown in FIG9 does not limit the Huffman encoding device, and may include more or fewer components than shown in the figure.
[0147] The processor 22 implements the Huffman encoding method provided by any of the above embodiments by calling the instructions stored in the memory 21 .
[0148] For an introduction to a Huffman encoding device provided by the present invention, please refer to the above method embodiment, and the present invention will not be repeated here. It has the same beneficial effects as the above Huffman encoding method.
[0149] Furthermore, the present invention also provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by the processor 22 , the steps of the Huffman coding method described above are implemented.
[0150] It is understandable that if the method in the above embodiment is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and executes all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes various media that can store program code, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.
[0151] For an introduction to a computer-readable storage medium provided by the present invention, please refer to the above method embodiment, which will not be described in detail herein. The method has the same beneficial effects as the above Huffman encoding method.
[0152] The above is a detailed introduction to a Huffman coding method, a Huffman coding system, a Huffman coding device and a medium provided by the present invention. The various embodiments in the specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same and similar parts between the various embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part description. It should be pointed out that for ordinary technicians in this technical field, without departing from the principle of the present invention, several improvements and modifications can be made to the present invention, and these improvements and modifications also fall within the scope of protection of the claims of the present invention.
[0153] It should also be noted that, in this specification, relational terms such as first and second, etc., are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the terms "comprises," "comprising," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or apparatus. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not exclude the presence of additional identical elements in the process, method, article, or apparatus comprising the element.
Claims
1. A Huffman coding method, characterized in that: Applied to a Huffman coding circuit, the method comprises: Get the target data and preset binary tree depth value; Constructing an initial binary tree for the target data based on frequency distribution, and obtaining an initial depth value of the initial binary tree; In a case where the initial depth value exceeds the preset binary tree depth value, determining the number of nodes to be adjusted of the initial binary tree according to the preset binary tree depth value and the initial depth value; Determining a binary tree adjustment strategy for the initial binary tree according to the number of nodes to be adjusted and the number of empty nodes, and adjusting the initial binary tree according to the binary tree adjustment strategy; Among them, the empty node is an additional node generated after moving one or more leaf nodes from the first layer of the initial binary tree to the layer before the layer where the preset binary tree depth value is located to the layer where the preset binary tree depth value is located.
2. The Huffman coding method according to claim 1, characterized in that: The preset binary tree depth value is determined at least by the compression rate of Huffman coding.
3. The Huffman coding method according to claim 1 or 2, characterized in that: The determining the number of nodes to be adjusted of the initial binary tree according to the preset binary tree depth value and the initial depth value includes: Obtain the parent node and character node corresponding to the layer beyond the preset binary tree depth value; The sum of the number of the parent nodes and the character nodes is used as the number of the nodes to be adjusted in the initial binary tree.
4. The Huffman coding method according to claim 3, characterized in that: The hollow nodes include theoretical hollow nodes and actual hollow nodes, and the binary tree adjustment strategy of the initial binary tree is determined according to the number of the nodes to be adjusted and the number of hollow nodes, including: Dividing the number of nodes to be adjusted by 2 to determine the number of theoretical empty nodes; Starting from the previous layer of the layer where the preset binary tree depth value is located to each leaf node in the first layer of the initial binary tree, move to the layer where the preset binary tree depth value is located, and The layer where the preset binary tree depth value is located generates the actual hollow node to determine the number of the actual hollow nodes; The binary tree adjustment strategy is determined according to the relationship between the number of nodes to be adjusted, the number of theoretical empty nodes, and the number of actual empty nodes.
5. The Huffman coding method according to claim 4, characterized in that: The process of determining the number of actual hollow nodes includes: Subtracting the preset binary tree depth value from the sequence number of each layer of the initial binary tree to obtain a layer difference value corresponding to each layer; Using the layer difference value corresponding to each layer as a power exponent and 2 as a variable to obtain the number of initial actual hole nodes constructed at the layer where the preset binary tree depth value is located for each layer; Subtract 1 from the number of initial actual hole nodes of each layer to obtain the number of actual hole nodes of each layer constructed at the layer where the preset binary tree depth value is located; The number of actual hole nodes in each layer is summed up to obtain the number of actual hole nodes.
6. The Huffman coding method according to claim 5, characterized in that: The determining the binary tree adjustment strategy according to the relationship between the number of nodes to be adjusted, the number of theoretical empty nodes, and the number of actual empty nodes includes: Determine whether the number of actual void nodes is greater than the number of theoretical void nodes; If yes, the actual number of void nodes is subtracted from the theoretical number of void nodes to obtain a void node difference; Using the hole node difference as the number of leaf nodes to be adjusted; According to the adjusted number of leaf nodes, the leaf nodes corresponding to the nodes to be adjusted are moved to a layer before the layer where the preset binary tree depth value is located to determine an initial binary tree adjustment strategy; Adjusting the initial binary tree according to the initial binary tree adjustment strategy to obtain the adjusted initial binary tree; The binary tree adjustment strategy is determined according to the relationship between the adjusted initial binary tree and the current number of hollow nodes.
7. The Huffman coding method according to claim 6, characterized in that: The determining the binary tree adjustment strategy according to the relationship between the adjusted initial binary tree and the current number of hollow nodes includes: Determining the number of current empty nodes in the adjusted initial binary tree; If the number of the current hole nodes is not 0, move the character node corresponding to the layer where the current hole node is located to the previous layer where the current hole node is located; Sort the frequencies of the target data in the layer where the preset binary tree depth value is located according to the frequency sorting order from large to small, and select the character node of the target data corresponding to the target frequency as the target moving character node; Move the target mobile character node to a layer before the layer where the preset binary tree depth value is located to determine a final binary tree; If the current number of holes is 0, the initial binary tree adjustment strategy is determined to be the binary tree adjustment strategy.
8. A Huffman coding system based on hardware resources, characterized in that: Applied to Huffman coding circuits, including: An acquisition module is used to obtain target data and preset binary tree depth value; A construction module, used to construct an initial binary tree for the target data based on frequency distribution, and obtain an initial depth value of the initial binary tree; A determination module, configured to determine the number of nodes to be adjusted of the initial binary tree according to the preset binary tree depth value and the initial depth value when the initial depth value exceeds the preset binary tree depth value; An adjustment module, configured to determine a binary tree adjustment strategy for the initial binary tree according to the number of nodes to be adjusted and the number of empty nodes, and adjust the initial binary tree according to the binary tree adjustment strategy; Among them, the empty node is an additional node generated after moving one or more leaf nodes from the first layer of the initial binary tree to the layer before the layer where the preset binary tree depth value is located to the layer where the preset binary tree depth value is located.
9. A Huffman encoding device, characterized in that: include: Memory for storing computer programs; A processor, configured to implement the steps of the Huffman coding method according to any one of claims 1 to 7 when executing the computer program.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the Huffman coding method according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Data compression method and device, electronic equipment and storage medium
CN113746487A
Data coding method and device and storage medium
CN114640357A
Adaptive Huffman coding system and method
CN114900193A
Huffman coding method, system and device and medium
CN116505954A
Huffman decoding method and decoder, huffman decoding table, method of preparing the table, and storage media
US20020130796A1
Cited By
Man-machine interaction method and system for legal consultation
CN119938873A
Trusted data sharing method and system based on supply chain collaboration
CN120165693A