Abstract:
Methods and devices for creating, compressing and searching a binary tree are provided by the present disclosure, which relates to the field of traffic classification. Binary tree compression includes: determining a number of a compression layer or a number of an intermediate node, compressing a binary tree according to the number of the compression layer or the number of the intermediate node to form at least one compression node, and creating a bitmap of the compression node. Therefore, the depth of the decision tree is reduced, the search speed is increased, rule expansion is avoided, and rule replication is reduced.

Description:
CROSS-REFERENCE TO RELATED APPLICATIONS 
       [0001]    This application is a continuation of U.S. patent application Ser. No. 13/353,884, filed on Jan. 19, 2012, which is a continuation application of International Application No. PCT/CN2010/076299, filed on Aug. 24, 2010, which are hereby incorporated by reference in their entirety. 
     
    
     FIELD 
       [0002]    The present disclosure relates to the field of traffic classification, and in particular, to methods and devices for creating, compressing and searching a binary tree. 
       BACKGROUND 
       [0003]    In traffic classification, multiple fields of a packet header are checked according to a predefined rule, and corresponding processing is performed based on a matching situation. A set of rules used in the traffic classification is called a traffic classifier. Each rule in the traffic classifier is related to several fields in the packet header. For example, a standard Internet Protocol Version 4 (IPv4) quintuple rule includes five fields, that is, a source IP address, a destination IP address, a protocol type, a source port number, and a destination port number. Matching modes vary for different fields. The IP address uses prefix matching; the protocol type uses precise matching; and the port number uses range matching. 
         [0004]    A traffic classification algorithm based on a decision tree is a rule set segmentation algorithm which segments a rule set in a recursive manner by using a certain segmentation policy until the number of rules in each sub-rule set is less than a preset bucket size. A binary decision tree, called a binary tree for short, may be created through segmentation. An intermediate node of the binary tree saves the method for segmenting the rule set; and a leaf node of the binary tree saves all sub-rule sets that may be matched. In searching, related fields are extracted from a packet header to compose a keyword, and then, the keyword is used to traverse a created decision tree until a corresponding leaf node is found. The keyword is compared with a rule in the leaf node to finally obtain a rule that matches the packet and has the highest priority. 
         [0005]    Currently, a Modular algorithm, which is a stage-by-stage bit selection segmentation traffic classification algorithm based on a decision tree, is provided. The Modular algorithm regards a rule as a 3-bit string composed of ‘0’, ‘1’ and ‘*’ without a concept of dimension, in which the ‘*’ represents a wildcard, and the binary digit of the ‘*’ may be 0 or 1. In segmentation, the number of rules corresponding to a certain bit whose value is ‘0’, ‘1’ or ‘*’ is calculated, and an optimal bit is selected for segmentation according to a priority metric formula. When a certain bit is selected for segmentation, rules with the value ‘0’ of this bit are put in a sub-rule set; rules with the value ‘1’ of this bit are put in another sub-rule set; and rules with the value ‘*’ of this bit appear in both sub-rule sets. In this way, the original rule set is divided into two sub-rule sets. A range rule may be converted into a prefix before being segmented by using the aforementioned method. The original rule set is segmented in a recursive manner by using this method until the number of rules in each sub-rule set is less than a preset maximum number of rules allowed in the leaf node. In this way, a binary decision tree may be created. Meanwhile, in order to reduce rule replication, the Modular algorithm divides the rule set into four sub-rule sets which respectively correspond to four conditions, that is, neither source IP nor destination IP is ‘*’, only source IP is ‘*’, only destination IP is ‘*’, and both source IP and destination IP are ‘*’. Different binary decision trees are created for the four sub-rule sets respectively. In searching, multiple binary decision trees are searched in a parallel manner. 
         [0006]    During the implementation of the present disclosure, the inventors find that the prior art has at least the following problems. 
         [0007]    When one bit is selected for segmentation, one binary decision tree is created in the end. The depth of the tree is large, which affects the decision efficiency. During the creation of the binary tree, if a range is extended to a prefix, a random range may be converted into 30 prefixes in the worst case. Taking a standard IPv4 quintuple as an example, each rule includes two ranges: a source port number and a destination port number; in the worst case, one rule is extended into 900 rules, which excessively occupies memory space. In addition, the method for reducing the rule replication in creating the binary tree is rough. When the sub-rule set includes many ‘*’, a rule is still replicated for many times. 
       SUMMARY 
       [0008]    To reduce the depth of a binary tree, an embodiment of the present disclosure provides a method for compressing a binary tree by a processor. The method includes: determining a compression parameter, in which the compression parameter is a number n of a compression layer or a number K of an intermediate node; compressing a binary tree according to the compression parameter to form at least one compression node; and creating a bitmap of the compression node. 
         [0009]    To avoid rule expansion caused by conversion of a range rule into a prefix in creating a binary tree, this embodiment provides a method for creating a binary tree. The method includes: segmenting a non-range rule in a rule set by using a bit selection segmentation algorithm; converting a range rule in the rule set into a prefix when segmentation efficiency is lower than a preset threshold, and keeping an identifier of a rule corresponding to the prefix and an identifier corresponding to the range rule unchanged; segmenting a converted rule set by using the bit selection segmentation algorithm; and creating a binary tree corresponding to the rule set according to all segmentation results. 
         [0010]    To reduce rule replication in creating a binary tree, this embodiment provides a method for creating a binary tree creation. The method includes: segmenting a rule set by using a bit selection segmentation algorithm; extracting rules in a segmentation process that need to be replicated, and putting the rules in another sub-rule set; and creating binary trees corresponding to the rule set and the another sub-rule set respectively. 
         [0011]    To reduce search depth and increase search speed, this embodiment provides a method for searching a binary tree. The method includes: obtaining a search keyword; determining whether each node of a binary tree is a leaf node or a compression node; when the node is a compression node, parsing the compression node; and when the node is a leaf node, traversing a linear table corresponding to the leaf node to search for a rule matching the keyword. 
         [0012]    To reduce the depth of a binary tree, an embodiment of the present disclosure provides a device for compressing a binary tree. The device includes: a determination module, configured to determine a compression parameter, in which the compression parameter is a number n of a compression layer or a number K of an intermediate node; a compression module, configured to compress a binary tree according to the compression parameter to form at least one compression node; and a bitmap module, configured to create a bitmap of the compression node. 
         [0013]    To avoid rule expansion caused by conversion of a range rule into a prefix in creating a binary tree, this embodiment provides a device for creating a binary tree. The device includes: a first segmentation module, configured to segment a non-range rule in a rule set by using a bit selection segmentation algorithm; a conversion module, configured to convert a range rule in the rule set into a prefix when segmentation efficiency is lower than a preset threshold; and a second segmentation module, configured to segment the prefix by using the bit selection segmentation algorithm. 
         [0014]    To reduce rule replication in creating a binary tree, this embodiment provides a device for creating a binary tree. The device includes: a segmentation module, configured to segment a rule set by using a bit selection segmentation algorithm; and an extraction module, configured to extract rules that need to be replicated, and put the rules in another sub-rule set. 
         [0015]    To reduce search depth and increase search speed, this embodiment provides a device for searching a binary tree. The device includes: an obtaining module, configured to obtain a search keyword; a determination module, configured to determine whether each node of a binary tree is a leaf node or a compression node; and a processing module, configured to, when a node is a compression node, parse the compression node; and when a node is a leaf node, traverse a linear table corresponding to the leaf node to search for a rule matching the keyword. 
         [0016]    The beneficial effects of the solutions provided in the embodiments of the present disclosure are as follows. 
         [0017]    The number of the compression layer or the number of the intermediate node is determined, and multiple nodes are compressed into one node according to the number of the compression layer or the number of the intermediate node, and therefore the depth of a decision tree is greatly reduced and the search speed is increased. 
         [0018]    The non-range rule in the rule set is first segmented by using the bit selection segmentation algorithm, and the range rule in the rule set is then converted into the prefix when the segmentation efficiency is lower than the preset threshold. Such a method of converting a range into a prefix “if necessary” effectively avoids the rule expansion caused when all ranges are converted into prefixes in creating the binary tree. 
         [0019]    The rules that need to be replicated are extracted and put in another sub-rule set, that is, multiple decision trees are created, which therefore effectively reduces the rule replication in creating the binary tree. 
         [0020]    It is determined whether each node of the binary tree is a leaf node or a compression node, and when the node is a compression node, the compression node is parsed; when the node is a leaf node, the linear table corresponding to the leaf node is traversed to search for the rule matching the keyword, and therefore the search depth of the binary tree is reduced and the search speed is increased. 
     
    
     
       BRIEF DESCRIPTION OF THE DRAWINGS 
         [0021]      FIG. 1  is a flow chart of a method for compressing a binary tree according to Embodiment 1 of the present disclosure; 
           [0022]      FIG. 2  is a flow chart of a shape compression method according to Embodiment 2 of the present disclosure; 
           [0023]      FIG. 3  is a schematic diagram of a binary tree fragment according to Embodiment 2 of the present disclosure; 
           [0024]      FIG. 4  is a schematic diagram of an updated binary tree fragment according to Embodiment 2 of the present disclosure; 
           [0025]      FIG. 5  is a flow chart of searching a binary tree search according to Embodiment 2 of the present disclosure; 
           [0026]      FIG. 6  is a flow chart of parsing of a compression node after shape compression according to Embodiment 2 of the present disclosure; 
           [0027]      FIG. 7  is a flow chart of an adaptive compression method according to Embodiment 3 of the present disclosure; 
           [0028]      FIG. 8  is a schematic diagram of a binary tree fragment according to Embodiment 3 of the present disclosure; 
           [0029]      FIG. 9  is a schematic diagram of adaptive compression of another binary tree fragment according to Embodiment 3 of the present disclosure; 
           [0030]      FIG. 10  is a schematic diagram of breadth-first pruning of another binary tree fragment according to Embodiment 3 of the present disclosure; 
           [0031]      FIG. 11  is a flow chart of parsing of a compression node after adaptive compression according to Embodiment 3 of the present disclosure; 
           [0032]      FIG. 12  is a flow chart of a method for creating a binary tree according to Embodiment 4 of the present disclosure; 
           [0033]      FIG. 13  is a schematic diagram of rule coverage according to Embodiment 4 of the present disclosure; 
           [0034]      FIG. 14  is a flow chart of a method for creating a binary tree according to Embodiment 5 of the present disclosure; 
           [0035]      FIG. 15  is a schematic structural diagram of a device for compressing a binary tree according to Embodiment 6 of the present disclosure; 
           [0036]      FIG. 16  is a schematic structural diagram of a device for creating a binary tree according to Embodiment 7 of the present disclosure; 
           [0037]      FIG. 17  is a schematic structural diagram of a device for creating a binary tree according to Embodiment 8 of the present disclosure; and 
           [0038]      FIG. 18  is a schematic structural diagram of a device for searching a binary tree according to Embodiment 9 of the present disclosure. 
       
    
    
     DETAILED DESCRIPTION OF THE EMBODIMENTS 
       [0039]    In order to make the objectives, solutions, and advantages of the present disclosure more comprehensible, the embodiments of the present disclosure are further described in detail below with reference to the accompanying drawings. 
       Embodiment 1 
       [0040]    Referring to  FIG. 1 , this embodiment provides a method for compressing a binary tree by a processor, which includes the following steps: 
         [0041]      101 : Determine a compression parameter, in which the compression parameter is a number n of a compression layer or a number K of an intermediate node. 
         [0042]      102 : Compress a binary tree according to the compression parameter to form at least one compression node. 
         [0043]      103 : Create a bitmap of the compression node. 
         [0044]    In the method according to this embodiment, the number of the compression layer or the number of the intermediate node is determined, and multiple nodes are compressed into one node according to the number of the compression layer or the number of the intermediate node, and therefore the depth of the decision tree is greatly reduced and the search speed is increased. 
       Embodiment 2 
       [0045]    This embodiment provides a method for compressing a binary tree by a processor. The method compresses a binary tree according to a number of a compression layer, and is also called a shape compression method. 
         [0046]    Referring to  FIG. 2 , the shape compression method includes the following steps: 
         [0047]      201 : Determine a number n of the compression layer according to a number of bits Nb of data read in one memory access, a number of bits Ni used by a bit index of each intermediate node, a number of bits Na used by a start address of a sub-node of a compression node, a number of bits Nt used by a compression node type, and a number of bits used by a bitmap. 
         [0048]    The sub-node of the compression node, also called a child node, refers to a node that is attached to the compression node. 
         [0049]    For example, 
         [0000]    
       
         
           
             n 
             ≤ 
             
               
                 Log 
                 2 
               
                
               
                 ( 
                 
                   
                     
                       
                         N 
                         b 
                       
                       - 
                       
                         N 
                         a 
                       
                       - 
                       
                         N 
                         t 
                       
                     
                     
                       
                         N 
                         i 
                       
                       + 
                       1 
                     
                   
                   + 
                   1 
                 
                 ) 
               
             
           
         
       
     
         [0000]    is determined according to (2 n −1)×N i +(2 n −1)+N a +N t ≦N b , where (2 n −1) represents the number of bits used by the bitmap. 
         [0050]    For example, assume Na=32, Nt=2, and Ni=9. Then, if Nb=128, n=3, that is, 3 layers may be compressed into one layer; and if Nb=256, n=4, that is, 4 layers may be compressed into one layer. 
         [0051]      202 : Compress a binary tree according to the number n of the compression layer to form at least one compression node. 
         [0052]    For example, starting from a root node or leaf node of the binary tree, nodes within a layer number less than or equal to the number n of the compression layer are compressed as a compression node. Then, starting from a sub-node of the compression node, the binary tree is continuously compressed in the same way as compressing the compression node until the binary tree is traversed. 
         [0053]    For example, referring to  FIG. 3 , when the number of the compression layer is 3, starting from the root node of the binary tree, three layers of the binary tree are compressed into one layer to form a big compression node  1 . Then, the compression is continued from the sub-node of the compression node  1  to form big compression nodes  2  and  3 . 
         [0054]    In addition, when the root node is a leaf node, the root node does not need to be compressed. In this case, no compression node exists. 
         [0055]      203 : Create a bitmap of each compression node. 
         [0056]    For the shape compression method, the bitmap refers to a shape bitmap. For example, the compression node is traversed in a breadth-first order, and a type of each node in the compression node is identified in turn. For example, the intermediate node is identified as 1, the leaf node or an empty node is identified as 0, and an identification result is used as the shape bitmap of the compression node. The “breadth-first order” refers to an order from top to bottom and from left to right. 
         [0057]    A binary tree fragment shown in  FIG. 3  is taken as an example. A hollow circle represents an intermediate node, and a solid circle represents a leaf node; 1 represents an intermediate node, and 0 represents a leaf node or an empty node; and the compression node  1  is traversed in the breadth-first order, so the shape bitmap of the compression node  1  is 1100100, in which the last two bits of 0 represent empty nodes. 
         [0058]    Further, when the compression node is incrementally updated, the shape bitmap is updated directly. For example, referring to  FIG. 4 , an updated binary tree fragment is shown, and then the shape bitmap of an updated compression node  1  is 1110100. 
         [0059]      204 : Store all sub-nodes of one compression node contiguously, and save the start address in the compression node. 
         [0060]    Further, in searching, an address of the sub-node is determined according to the start address and an index of the sub-node. 
         [0061]    A node search process in the shape compression includes: obtaining a search keyword; determining whether each node of the binary tree is a leaf node or a compression node; when a node is a compression node, parsing the compression node; and when a node is a leaf node, traversing a linear table corresponding to the leaf node to search for a rule matching the keyword. For example, referring to  FIG. 5 , the binary tree search process is as follows: 
         [0062]    A1: Obtain a search keyword. 
         [0063]    A2: Determined whether a root node of a decision binary tree is a leaf node. 
         [0064]    For example, if the root node is not a leaf node, the root node corresponds to a compression node which is used as a current compression node, and step A3 is executed; if the root node is a leaf node, step A5 is executed. 
         [0065]    For example, the root node in  FIG. 3  is not a leaf node, so the root node corresponds to a compression node, and step A3 is executed. 
         [0066]    A3: Parse the current compression node. 
         [0067]    A4: Determined whether a sub-node of the current compression node is a leaf node. 
         [0068]    For example, if the sub-node is not a leaf node, the root node corresponds to a compression node which is used as a current compression node, and step A3 is executed; if the sub-node is a leaf node, step A5 is executed. 
         [0069]    A5: Traverse a linear table corresponding to the leaf node to search for a rule matching the keyword; and ends the process. 
         [0070]    Based on the search process shown in  FIG. 5 , parsing by layer may be performed for the shape compression, in which the root node of the compression node is used as a first layer, and it is determined whether each node of the compression node is a leaf node according to the shape bitmap of the compression node. Referring to  FIG. 6 , the process of parsing the compression node is as follows: 
         [0071]    B1: Enter a compression node. 
         [0072]    B2: Extract a bit corresponding to a first bit index from a search keyword. 
         [0073]    B3: Enter a next layer. 
         [0074]    B4: Determine whether the current number of layers is greater than a number n of the compression layer. 
         [0075]    For example, if the current number of layers is not greater than a number n of the compression layer, step B5 is executed; if the current number of layers is greater than a number n of the compression layer, step B7 is executed. 
         [0076]    B5: Calculate a position of a current node (that is, a current binary tree node) in a shape bitmap, and extract a bit corresponding to the position from the shape bitmap. 
         [0077]    B6: Determine whether the shape bitmap corresponding to the current node is 0. 
         [0078]    For example, if the shape bitmap is 0, step B9 is executed; if the shape bitmap is not 0, step B3 is executed. 
         [0079]    B7: Enter a sub-node, and read a sub-node type. 
         [0080]    B8: Determine whether the sub-node is a leaf node. 
         [0081]    For example, if the sub-node is a leaf node, step B9 is executed; if the sub-node is not a leaf node, step B10 is executed. 
         [0082]    B9: The sub-node is a leaf node; the process ends. 
         [0083]    B10: The sub-node is a compression node; the process ends. 
         [0084]    In the method for searching, it is determined whether each node of the binary tree is a leaf node or a compression node, and when a node is a compression node, the compression node is parsed; when a node is a leaf node, the linear table corresponding to the leaf node is traversed to search for a rule matching the keyword, and therefore the search depth of the binary tree is reduced and the search speed is increased. 
         [0085]    In the method according to this embodiment, the number of the compression layer is determined according to the number of bits of data read in one memory access, the number of bits used by the bit index of each intermediate node, the number of bits used by the start address, the number of bits used by the compression node type, and the number of bits used by the bitmap, and multiple nodes are compressed into one node according to the number of the compression layer, and therefore the depth of the decision tree is greatly reduced and the search speed is increased. 
       Embodiment 3 
       [0086]    This embodiment provides a method for compressing a binary tree by a processor. The method compresses a binary tree according to a number of an intermediate node, and is also called an adaptive compression method. 
         [0087]    First, a theorem used in this embodiment is introduced. If each intermediate node of a binary tree includes two sub-nodes, N connected the intermediate node with one of the intermediate node as a root must have (N+1) sub-nodes, which is proved as follows: 
         [0088]    1) In a situation of only one intermediate node, the intermediate node must have two sub-nodes, which therefore satisfies the theorem. 
         [0089]    2) In a situation of K connected the intermediate node with one of the intermediate node as a root, assume that the K intermediate nodes have (K+1) sub-nodes. In this case, if the number of connected the intermediate node becomes (K+1) while the intermediate node used as the root remains unchanged, one of original sub-nodes inevitably becomes an intermediate node to be considered. Because the sub-node is an intermediate node, the node must have two sub-nodes. Therefore, the number of sub-nodes becomes (K+1)−1+2=(K+1)+1. 
         [0090]    The theorem is proved by combining 1) and 2). 
         [0091]    Referring to  FIG. 7 , the adaptive compression method includes the following steps: 
         [0092]      301 : Determine a number K of the intermediate node according to a number of bits Nb of data read in one memory access, a number of bits Ni used by a bit index of each intermediate node, a number of bits Na used by a start address of a sub-node of a compression node, a number of bits Nt used by a compression node type, and a number of bits used by a bitmap. 
         [0093]    For the adaptive compression method, the bitmap refers to a shape bitmap and an external bitmap. The shape bitmap is used to represent the type of each node in the compression node. The external bitmap is used to represent the type of each sub-node in the compression node. 
         [0094]    For example, 
         [0000]    
       
         
           
             K 
             ≤ 
             
               
                 
                   N 
                   b 
                 
                 - 
                 
                   N 
                   a 
                 
                 - 
                 
                   N 
                   t 
                 
                 + 
                 1 
               
               
                 
                   N 
                   i 
                 
                 + 
                 3 
               
             
           
         
       
     
         [0000]    is determined according to K×N i +2(K−1)+K+1+N a +N t ≦N b . 
         [0095]    2(K−1) represents the number of bits used by the shape bitmap. One compression node involves 2K+1 nodes in total, and the first node of a compressed binary tree fragment is surely the intermediate node, so the first bit of the shape bitmap is surely 1; and the last two nodes are surely uncompressed nodes, so the last two bits of the shape bitmap are surely ‘00’. Therefore, the shape bitmap is represented by 2K+1−3=2(K−1). Because a total of K+1 external nodes exist, the external bitmap is represented by K+1 bits. 
         [0096]      302 : Compress a binary tree according to the number K of the intermediate node to form at least one compression node. 
         [0097]    For example, starting from a root node of the binary tree, a node with the number less than or equal to the number K of the intermediate node are used as a compression node; and starting from the sub-node of the compression node, the binary tree is continuously compressed in the same way as compressing the compression node until the binary tree is traversed. 
         [0098]    For example, assume that Na=32, Nt=2, Ni=9, so when Nb=128, the number K of the intermediate node equals 8, and the number n of the compression layer equals 3. Referring to the binary tree fragment shown in  FIG. 8 , if the shape compression method according to Embodiment 2 is used, every 3 layers are compressed into one big node (namely, a compression node), and a fragment should be compressed into three big nodes. According to the adaptive compression method according to this embodiment, every 8 intermediate nodes are compressed into one big node; therefore, the fragment only needs to be compressed into one big node, and thereby higher compression efficiency is achieved. 
         [0099]    In addition, when the root node is a leaf node, the root node does not need to be compressed. In this case, no compression node exists. 
         [0100]      303 : Use a breadth-first pruning algorithm for optimization so as to further improve the compression efficiency. 
         [0101]    For example, a number of all sub-intermediate nodes of each intermediate node including the intermediate node itself is counted; starting from the root node of the binary tree, it is determined whether the number of all sub-intermediate nodes corresponding to the intermediate node including the intermediate node itself is less than or equal to the number K of the intermediate node; when the number of all sub-intermediate nodes corresponding to each intermediate node in the compression node including the intermediate node itself is greater than the number K of the intermediate node, the compression node remains unchanged; when the number of all sub-intermediate nodes corresponding to the intermediate node in the compression node including the intermediate node itself is less than or equal to the number K of the intermediate node, the intermediate node and all the sub-nodes of the intermediate node are pruned and used as a new compression node. After the pruning, other nodes in the compression node than the intermediate node are still kept in the compression node, that is, the number of the intermediate node in the compression node associated with the compression node formed by pruning is adjusted. 
         [0102]    For example, if a calculated number K of the intermediate node equals 7, and in a binary tree fragment shown in  FIG. 9 , a hollow circle represents an intermediate node, and a solid circle represents a leaf node, the binary tree fragment needs to be compressed into 9 compression nodes by using the adaptive compression method. In the breadth-first pruning algorithm, the number of all sub-intermediate nodes of a first intermediate node including the first intermediate node itself is calculated as 15, which is greater than K, so the first intermediate node is not pruned; the number of all sub-intermediate nodes of a second intermediate node including the second intermediate node itself is calculated as 7, which is less than or equal to K, so the second intermediate node and all the sub-nodes of the second intermediate node are pruned and used as a compression node; in a same way, the number of all sub-intermediate nodes of a third intermediate node including the third intermediate node itself is calculated as 7, which is less than or equal to K, so the third intermediate node and all the sub-nodes of the third intermediate node are pruned and used as a compression node; after the pruning, the number of the intermediate node of the compression node associated with the two compression nodes formed by pruning is adjusted to 1; in the end, only 3 compression nodes are formed, referring to  FIG. 10  for details, which greatly improves the compression efficiency. 
         [0103]      304 : Create a bitmap of the compression node, including a shape bitmap and an external bitmap. 
         [0104]    For the shape bitmap, the compression node is traversed in a breadth-first order, the type of each node in the compression node is identified in turn, and an identification result is used as the shape bitmap of the compression node. 
         [0105]    For the external bitmap, the compression node is traversed in the breadth-first order, the type of each sub-node is identified in turn, and an identification result is used as the external bitmap of the compression node. 
         [0106]    Still taking  FIG. 8  as an example, the shape bitmap excluding a first node and last two nodes requires a total of 2(K−1)=12 bits, which is 010101010101; and the external bitmap requires a total of K+1 bits, which is 00000000. 
         [0107]    Further, an incremental update may be implemented through the shape bitmap and the external bitmap. That is, when the type of the sub-node of the compression node changes, the bit in the external bitmap corresponding to the sub-node whose type changes is adjusted, and the shape bitmap remains unchanged, so as to implement the incremental update. For example, if a leaf node is attached to a first leaf node in  FIG. 8  so that the first leaf node becomes an intermediate node, a corresponding bit in the external bitmap changes from 0 to 1, and the type of other sub-nodes remains unchanged, so the external bitmap is updated to 10000000, while the shape bitmap remains unchanged. 
         [0108]    After the above adaptive compression, parsing by layer may be performed for the adaptive compression based on the search process shown in  FIG. 5 , in which the root node of the compression node is used as a first layer, it is determined whether a node in the compression node is 0 in the external bitmap when the node is 0 in the shape bitmap according to the shape bitmap of the compression node, and it is determined, according to a determination result, whether the sub-node corresponding to the node in the compression node is a leaf node. Referring to  FIG. 11 , the process of parsing the compression node is as follows: 
         [0109]    C1: Enter a compression node. 
         [0110]    C2: Extract a bit corresponding to a first bit index from a search keyword. 
         [0111]    C3: Enter a next layer. 
         [0112]    C4: Determine whether a position of a current node (that is, a current binary tree node) in a shape bitmap is greater than 2(K−1). 
         [0113]    For example, if a position of a current node (that is, a current binary tree node) in a shape bitmap is not greater than 2(K−1), step C5 is executed; if yes, that is, the position of the current node is greater than 2(K−1), step C6 is executed. 
         [0114]    C5: Extract a bit corresponding to the current node from the shape bitmap, and execute step C7. 
         [0115]    C6: The shape bitmap corresponding to the current node is 0. 
         [0116]    C7: Determine whether the shape bitmap corresponding to the current node is 0. 
         [0117]    For example, if the shape bitmap corresponding to the current node is not 0, step C3 is executed; if yes, that is, the shape bitmap is 0, step C8 is executed. 
         [0118]    C8: Extract a bit corresponding to the current node from an external bitmap. 
         [0119]    C9: Determine whether the external bitmap corresponding to the current node is 0. 
         [0120]    For example, if the external bitmap is not 0, step C10 is executed; if the external bitmap is 0, step C11 is executed. 
         [0121]    C10: The sub-node is a compression node; the process ends. 
         [0122]    C11: The sub-node is a leaf node; the process ends. 
         [0123]    In the method for searching, it is determined whether each node of the binary tree is a leaf node or a compression node, and when the node is a compression node, the compression node is parsed; when the node is a leaf node, the linear table corresponding to the leaf node is traversed to search for a rule matching the keyword, and therefore the search depth of the binary tree is reduced and the search speed is improved. 
         [0124]    In the method according to this embodiment, the number of the intermediate node is determined according to the number of bits of data read in one memory access, the number of bits used by the bit index of each intermediate node, the number of bits used by the start address, the number of bits used by the compression node type, and the number of bits used by the bitmap, and multiple nodes are compressed into one node according to the number of the intermediate node, and therefore the depth of the decision tree is greatly reduced and the search speed is improved. Besides, the breadth-first pruning method is used, which further improves the compression efficiency and reduces the depth of the decision tree. 
       Embodiment 4 
       [0125]    Referring to  FIG. 12 , this embodiment provides a method for creating a binary tree by a processor. The method includes the following steps: 
         [0126]      401 : Segment a non-range rule in a rule set by using a bit selection segmentation algorithm. 
         [0127]    For example, a bit with the highest segmentation efficiency and least replication is selected each time. In addition, this embodiment does not limit a specific segmentation method. 
         [0128]      402 : Convert a range rule in the rule set into a prefix when segmentation efficiency is lower than a preset threshold. 
         [0129]    In the conversion, an identifier of a rule corresponding to the prefix and an identifier of a rule corresponding to a range remain unchanged. 
         [0130]    After the conversion, rules may overlap each other, and a rule that is completely covered and has a low priority is removed. For example, referring to  FIG. 13 , if the priority of Rule R 1  is higher than that of Rule R 2 , and meanwhile R 1  completely covers R 2 , R 2  may never be targeted, and therefore may be removed from the rule set. In addition, in the leaf node, if identifiers of multiple rules are the same, only one rule corresponding to the identifier is kept. 
         [0131]      403 : Segment the converted rule set by using the bit selection segmentation algorithm. 
         [0132]      404 : Create a binary tree corresponding to the rule set according to all segmentation results. 
         [0133]    All the segmentation results include the segmentation result of the non-range rule in the rule set and the segmentation result of the converted rule set. 
         [0134]    Further, to solve the problem of rule replication, rules that need to be replicated are extracted in the segmentation process by using the bit selection segmentation algorithm, and put in another sub-rule set, that is, an extra decision tree is created. 
         [0135]    In addition, after the range is converted, extended multiple rules share the same identifier with original rules. Therefore, all rules with the same identifier that need to be replicated should be extracted and put in the another sub-rule set. 
         [0136]    In the method according to this embodiment, the non-range rule in the rule set is first segmented by using the bit selection segmentation algorithm, and the range rule in the rule set is then converted into the prefix when the segmentation efficiency is lower than the preset threshold. Such a method of converting a range into a prefix “if necessary” effectively avoids the rule expansion caused when all ranges are converted into prefixes. In addition, the rules that need to be replicated are extracted and put in another sub-rule set, that is, multiple decision trees are created, which therefore effectively reduces the rule replication. 
       Embodiment 5 
       [0137]    Referring to  FIG. 14 , this embodiment provides a method for creating a binary tree by a processor. The method includes the following steps: 
         [0138]      501 : Segment a rule set by using a bit selection segmentation algorithm. 
         [0139]    Details are the same as those of step  401 , and are not described here again. 
         [0140]      502 : Extract rules in a segmentation process that need to be replicated and put the rules in another sub-rule set. 
         [0141]      503 : Create binary trees corresponding to the rule set and the another sub-rule set respectively. 
         [0142]    Further, after the range is converted, the extended multiple rules share the same identifier with the original rules. Therefore, all rules with the same identifier that need to be replicated should be extracted and put in the another sub-rule set. 
         [0143]    In the method according to this embodiment, the rules that need to be replicated are extracted and put in another sub-rule set, that is, multiple decision trees are created, which therefore effectively reduces the rule replication. 
       Embodiment 6 
       [0144]    Referring to  FIG. 15 , this embodiment provides a device for compressing a binary tree, which includes a determination module  601 , a compression module  602 , and a bitmap module  603 . The device has at least one processor configured to compress the binary tree. 
         [0145]    The determination module  601  is configured to determine a compression parameter, in which the compression parameter is a number n of the compression layer or a number K of the intermediate node. 
         [0146]    The compression module  602  is configured to compress a binary tree according to the compression parameter to form at least one compression node. 
         [0147]    The bitmap module  603  is configured to create a bitmap of the compression node. 
         [0148]    The determination module  601  is configured to determine the compression parameter according to a number of bits Nb of data read in one memory access, a number of bits Ni used by a bit index of each intermediate node, a number of bits Na used by a start address of a sub-node of a compression node, a number of bits Nt used by a compression node type, and a number of bits used by a bitmap. 
         [0149]    The compression parameter is the number n of the compression layer. 
         [0150]    The determination module  601  is configured to: 
         [0151]    determine 
         [0000]    
       
         
           
             n 
             ≤ 
             
               
                 Log 
                 2 
               
                
               
                 ( 
                 
                   
                     
                       
                         N 
                         b 
                       
                       - 
                       
                         N 
                         a 
                       
                       - 
                       
                         N 
                         t 
                       
                     
                     
                       
                         N 
                         i 
                       
                       + 
                       1 
                     
                   
                   + 
                   1 
                 
                 ) 
               
             
           
         
       
     
         [0000]    according to (2 n −1)×N i +(2 n −1)+N a +N t ≦N b , 
         [0152]    where Nb represents the number of bits of data read in one memory access, Ni represents the number of bits used by the bit index of each intermediate node, Na represents the number of bits used by the start address of the sub-node of the compression node, Nt represents the number of bits used by the compression node type, and (2 n −1) represents the number of bits used by the bitmap. 
         [0153]    The compression module  602  is configured to: 
         [0154]    use, starting from a root node or leaf node of the binary tree, a node with the number of layers less than or equal to the number n of the compression layer as a compression node; and 
         [0155]    continue to compress the binary tree in the same way as compressing the compression node starting from a sub-node of the compression node until the binary tree is traversed. 
         [0156]    The bitmap module  603  is configured to: 
         [0157]    traverse the compression node in a breadth-first order, identify a type of each node in turn, and use an identification result as a shape bitmap of the compression node. 
         [0158]    The compression parameter is the number K of the intermediate node. 
         [0159]    The determination module  601  is configured to: 
         [0160]    determine 
         [0000]    
       
         
           
             K 
             ≤ 
             
               
                 
                   N 
                   b 
                 
                 - 
                 
                   N 
                   a 
                 
                 - 
                 
                   N 
                   t 
                 
                 + 
                 1 
               
               
                 
                   N 
                   i 
                 
                 + 
                 3 
               
             
           
         
       
     
         [0000]    according to K×N i +2(K−1)+(K+1)+N a +N t ≦N b , 
         [0161]    where Nb represents the number of bits of data read in one memory access, Ni represents the number of bits used by the bit index of each intermediate node, Na represents the number of bits used by the start address of the sub-node of the compression node, Nt represents the number of bits used by the compression node type, 2(K−1) represents the number of bits used by a shape bitmap excluding the first node and the last two nodes, and (K+1) represents the number of bits used by an external bitmap. 
         [0162]    The compression module  602  is configured to: 
         [0163]    use, starting from a root node of the binary tree, a node with the number less than or equal to the number K of the intermediate node as a compression node; and 
         [0164]    continue to compress the binary tree in the same way as compressing the compression node starting from a sub-node of the compression node until the binary tree is traversed. 
         [0165]    Further, after the binary tree is traversed, the compression node  602  is configured to: 
         [0166]    count a number of all sub-intermediate nodes of each intermediate node including the intermediate node itself; 
         [0167]    determine, starting from the root node of the binary tree, whether the number of all sub-intermediate nodes corresponding to the intermediate node including the intermediate node itself is less than or equal to the number K of the intermediate node; 
         [0168]    keep the compression node unchanged when the number of all sub-intermediate nodes corresponding to each intermediate node in the compression node including the intermediate node itself is greater than the number K of the intermediate node; and 
         [0169]    use the intermediate node and all the sub-nodes of the intermediate node as a new compression node, and keep other nodes in the compression node than the intermediate node still in the compression node, when the number of all sub-intermediate nodes corresponding to the intermediate node in the compression node including the intermediate node itself is less than or equal to the number K of the intermediate node. 
         [0170]    The bitmap module  603  is configured to: 
         [0171]    traverse the compression node in a breadth-first order, identify the type of each node in turn, and use an identification result as a shape bitmap of the compression node; and 
         [0172]    traverse the compression node in the breadth-first order, identify the type of each sub-node in turn, and use an identification result as an external bitmap of the compression node. 
         [0173]    The device further includes an incremental update module. 
         [0174]    The incremental update module is configured to, when the type of a sub-node of the compression node changes after the bitmap of the compression node is created, adjust a bit in the external bitmap corresponding to the sub-node whose type changes, and keep the shape bitmap unchanged, so as to implement an incremental update. 
         [0175]    In the device according to this embodiment, the number of the compression layer or the number of the intermediate node is determined, and multiple nodes are compressed into one node according to the number of the compression layer or the number of the intermediate node, and therefore the depth of the decision tree is greatly reduced and the search speed is improved. 
       Embodiment 7 
       [0176]    Referring to  FIG. 16 , this embodiment provides a device having at least one processor for creating a binary tree, which includes a first segmentation module  701 , a conversion module  702 , a second segmentation module  703 , and a creation module  704 . 
         [0177]    The first segmentation module  701  is configured to segment a non-range rule in a rule set by using a bit selection segmentation algorithm. 
         [0178]    The conversion module  702  is configured to convert a range rule in the rule set into a prefix when segmentation efficiency is lower than a preset threshold, and keep an identifier of a rule corresponding to the prefix and an identifier corresponding to the range rule unchanged. 
         [0179]    For example, when the segmentation efficiency is lower than the preset threshold, a range in the rule set are converted into a prefix, and keep an identifier of a rule corresponding to the prefix and an identifier of a rule corresponding to the range unchanged. 
         [0180]    Further, after the range in the rule set is converted into the prefix, a rule that is covered and has a low priority is removed. 
         [0181]    The second segmentation module  703  is configured to segment a converted rule set by using the bit selection segmentation algorithm. 
         [0182]    The creation module  704  is configured to create a binary tree corresponding to the rule set according to all segmentation results. 
         [0183]    The device further includes an extraction module. 
         [0184]    The extraction module is configured to extract rules in a segmentation process that need to be replicated, and put the rules in another sub-rule set. 
         [0185]    For example, all rules with the same identifier that need to be replicated are extracted and put in the another sub-rule set. 
         [0186]    In the device according to this embodiment, the rule set is first segmented by using the bit selection segmentation algorithm, and the range in the rule set is then converted into the prefix when the segmentation efficiency is lower than the preset threshold. Such a method of converting a range into a prefix “if necessary” effectively avoids the rule expansion caused when all ranges are converted into prefixes. In addition, the rules that need to be replicated are extracted and put in another sub-rule set, that is, multiple decision trees are created, which therefore effectively reduces the rule replication. 
       Embodiment 8 
       [0187]    Referring to  FIG. 17 , this embodiment provides a device having at least one processor for creating a binary tree, which includes a segmentation module  801 , an extraction module  802 , and a creation module  803 . 
         [0188]    The segmentation module  801  is configured to segment a rule set by using a bit selection segmentation algorithm. 
         [0189]    The extraction module  802  is configured to extract rules in a segmentation process that need to be replicated, and put the rules in another sub-rule set. 
         [0190]    The creation module  803  is configured to create binary trees corresponding to the rule set and the another sub-rule set respectively. 
         [0191]    For example, all rules with the same identifier that need to be replicated are extracted and put in the another sub-rule set. 
         [0192]    In the device according to this embodiment, the rules that need to be replicated are extracted and put in another sub-rule set, that is, multiple decision trees are created, which therefore effectively reduces the rule replication. 
       Embodiment 9 
       [0193]    Referring to  FIG. 18 , this embodiment provides a device having at least one processor for searching a binary tree, which includes an obtaining module  901 , a determination module  902 , and a processing module  903 . The device may be a server that has at least one processor connected to storage medium accessible to the at least one processor. 
         [0194]    The obtaining module  901  is configured to obtain a search keyword. 
         [0195]    The determination module  902  is configured to determine whether each node of a binary tree is a leaf node or a compression node. 
         [0196]    The processing module  903  is configured to, when a node is a compression node, parse the compression node; and when a node is a leaf node, traverse a linear table corresponding to the leaf node to search for a rule matching the keyword. 
         [0197]    The processing module  903  includes a first parsing unit  903   a  configured to determine, according to a shape bitmap of the compression node, whether each node of the compression node is a leaf node. A detailed process is shown in  FIG. 6 , and is not described here again. 
         [0198]    The processing module  903  includes a second parsing unit  903   b  configured to determine, according to the shape bitmap of the compression node, whether a node in the compression node is 0 in an external bitmap when the node is 0 in the shape bitmap, and determine, according to a determination result, whether a sub-node corresponding to the node in the compression node is a leaf node. A detailed process is shown in  FIG. 11 , and is not described here again. 
         [0199]    In the device according to this embodiment, it is determined whether each node of the binary tree is a leaf node or a compression node, and when a node is a compression node, the compression node is parsed; when a node is a leaf node, the linear table corresponding to the leaf node is traversed to search for a rule matching the keyword, and therefore the search depth of the binary tree is reduced and the search speed is improved. 
         [0200]    The embodiments of the present disclosure may be implemented by software, and corresponding software programs may be stored in readable storage medium, such as a hard disk, a buffer, or an optical disk of a computer that has at least a processor configured to implement the method stored in the storage medium. 
         [0201]    The above descriptions are merely exemplary embodiments of the present disclosure, but not intended to limit the present disclosure. Any modification, equivalent replacement, and improvement made without departing from the spirit and principle of the present disclosure shall fall within the protection scope of the present disclosure.