Data Structure Review - Chapter 5: Tree


Part One: Trees and Basic Binary Trees

1. Basic terminology of trees

Degree: The number of subtrees (children, child nodes) of a certain node.
Degree of the tree: The maximum degree of the node in the tree.

The two terms "node" and "node" have the same meaning and can be used.
Root node: a (non-empty) tree has one and only one node.
Leaf node: A node with degree 0, also called Terminal node.
Branch node: A node with a degree other than 0, also called non-leaf node< a i=12> ornon-terminal node (the root node may also belong to a branch node).

Number of levels: The root node is on the first level, the child nodes of the root node are on the 2nd level, and so on.
Depth: starting from the root node top to bottom layer by layer Cumulative.
Height: starting from the leaf node bottom-up and accumulating layer by layer of.

Ordered tree: There is a sequential relationship between the child nodes of any node in the tree.
Unordered tree: There is no sequential relationship between the child nodes of any node in the tree.

Between two nodes in the tree:
Path: The path passed between these two nodes Composed of node sequence.
Path length: The number of edges passed on the path.

2. Tree family relationship

Children: The subtree (child node) of the node.
Parents: The node is the parent of the subtree.
Brothers: Children of the same parents are called brothers.
Cousins: Nodes where parents are on the same level are cousins ​​of each other.
Ancestors: All nodes on the branches from the root to this node are called the ancestors of this node. Descendants: Any node in the subtree rooted at a certain nodeare called the descendants of this node.

3. Basic binary tree

The characteristic of a binary tree is that each node has at most two subtrees, that is, there is no node with degree greater than 2 in the binary tree.
The subtrees of a binary tree are divided into left and right subtrees, and their order cannot be reversed.

full binary tree complete binary tree

Snow height n n n, and contains 2 n − 1 2^n-1 2nBinary tree with 1 nodes;

Each level of the tree contains the most nodes;

The leaf nodes are concentrated at the lowest level of the full binary tree;

The degrees of other nodes except leaf nodes are all 2;

Each node corresponds to the number of a full binary tree node with the same height as it;

Leaf nodes can only appear in the bottom two layers;

There can be at most one node with degree 1, and the node has only a left child;

If a node has only a left child, or is a leaf node, then as long as there is a node with a number greater than the node, it must be a leaf node;

When the number of nodes is an odd number, each branch node contains left and right children;

When the number of nodes is an even number, only the branch node with the largest number has no right child;

Full binary trees are also a type of complete binary trees. The formula to generate a complete binary tree is:“From top to bottom, from left to right”.

4. Storage structure of binary tree

(1) Sequential storage structure:
Sequential storage of binary trees
(2) Chain storage structure:
Binary tree chain storage

5. Basic properties of binary trees

(1) The number ofnodes in the tree is equal to the degree of all nodes plus 1 a> . If the total number of nodes in the tree is n, then: n = number of branches (that is, the sum of degrees) + 1.

[2010 Unified Examination Real Questions] In a tree T with degree 4, if there are 20 nodes with degree 4, 10 nodes with degree 3, and 1 node with degree 2, There are 10 nodes with degree 1, then the number of leaf nodes of tree T is (B).
A.41
B.82
C.113
D.122 n = ( 20 + 10 + 1 + 10 ) + k = ( 20 ∗ 4 + 10 ∗ 3 + 1 ∗ 2 + 10 ∗ 1 ) + 1 n=(20+10+1+10)+k=(20*4+10*3+1*2+10*1)+ 1
[Analysis] Suppose there are k leaf nodes and the number of summary points is n, then:n=(20+10+1+10)+k=(204+103+12+101)+1,achieved 41 + k = 123 41+k=123 41+k=123,即 k = 82 k=82 k=82

[Training Example] It is known that in a tree with degree 4, the number of nodes with degrees 0, 1, 2, and 3 are 14, 4, 3, and 2 respectively. Find the total number of nodes of the tree n The number of nodes with degree 4.
【Analysis】Suppose the number of nodes with degree 4 is k, then: n = ( 14 + 4 + 3 + 2 ) + k = (k*4 + 2*3 + 3*2 + 4*1) + 1 n=(14+4+3+2)+k=(k*4+2*3+3*2+4*1) +1 n=(14+4+3+2)+k=(k4+23+32+41)+1,achieved 23 + k = 4 k + 17 23+k=4k+17 23+k=4k+17,即 k = 2 , n = 25 k=2,n=25 k=2n=25

The first property above applies to all trees, and the five properties that are exclusive to binary trees will be introduced below.

(2) The number of leaf nodeson a non-empty binary treeis equal tothe number of nodes with degree 2 Add 1, that is: n 0 = n 2 + 1 n_0 = n_2 +1 n0=n2+1

Based on property 1 "Total number of nodes = node degree + 1", we have: n = n 0 + n 1 + n 2 = ( n 0 ∗ 0 + n 1 ∗ 1 + n 2 ∗ 2 ) + 1 n = n_0 + n_1 + n_2 = (n_0*0+n_1*1+n_2*2)+1 n=n0+n1+n2=(n00+n11+n22)+1, the above formula is obtained after eliminating the left and right.

(3) In the complete binary tree, the left child number of node i is 2 i 2i < /span> 2i

Suppose node i is the x-th in the k-th row, then it belongs to the ( 2 k − 1 − 1 ) + x = 2 k − 1 + ( x − 1) (2^{k-1}-1)+x=2^{k-1}+(x-1) (2k11)+x=2k1+(x1) node;
and its left child node is the k+1th row , belonging to the ( 2 ( k + 1 ) − 1 − 1 ) + 2 ∗ ( x − 1 ) + 1 = 2 k + 2 ∗ ( x − 1 ) = 2 ∗ [ 2 k − 1 + ( x − 1 ) ] (2^{(k+1)-1}-1)+2*(x-1)+1=2^k+2*(x-1)=2 *[2^{k-1}+(x-1)] (2(k+1)11)+2(x1)+1=2k+2(x1)=2[2k1+(x1)] The end point;

(4) On the non-empty binary treekth level there are at most 2 k − 1 2 ^{k-1} 2k1 the end point (k≥1).

The general term of the geometric sequence.

(5)altitude h target forked tree most abundant 2 h − 1 2^h- 1 2h1 The end point (h≥1).

The formula for summation from geometric sequence: a 1 − a n ∗ q 1 − q \frac{a_1-a_n*q}{1-q} 1qa1anq,有: 1 − 2 h − 1 ∗ 2 1 − 2 = 2 h − 1 \frac{1-2^{h-1}*2}{1-2}=2^h-1 1212h12=2h1

(6) The height of a complete binary tree with n nodes (n>0) is: ⌊ l o g 2 n ⌋ + 1 \lfloor log_2n \rfloor+1 log2n+1

It can be deduced from formula 5: 2 h − 1 ≤ n ≤ 2 h − 1 2^{h-1} \leq n \leq 2^h-1 2h1n2h1,因此 { h ≤ l o g 2 n + 1 = l o g 2 ( 2 n ) h ≥ l o g 2 ( n + 1 ) \begin{cases} h \leq log_2n+1=log_2(2n) \\ h \geq log_2(n+1) \end{cases} { hlog2n+1=log2(2n)hlog2(n+1)

6. Binary tree traversal

(1) Pre-order traversal: If the binary tree is not empty, the root node is visited, the left subtree is traversed by the root first, and the right subtree is traversed by the root later—— "Left and right of the root ”.
(2) In-order traversal: If the binary tree is not empty, the middle root traverses the left subtree, visits the root node, and the middle root traverses the right subtree - " Left root right”.
(3) Postorder traversal: If the binary tree is not empty, the posterior root traverses the left subtree, the posterior root traverses the right subtree, and visits the root node——"Left and right roots".
(4) Level-order traversal: Starting from the root node of the binary tree, traverse from top to bottom and from left to right.

For an expression, it can be represented by a binary tree. For example, for the expression: a − b / ( c + d ) + e ∗ f a-b/(c+d) +e*f ab/(c+d)+It isf

We can easily find that preorder traversal andpostorder traversalInfix expressions can be converted into prefix expressions and postfix expressions< a i=8>.

[Classic example] For example, find the binary tree and post-order sequence determined by the pre-order sequence (ABCDEFGHI) and the in-order sequence (BCAEDGHFI).

Part 1 Exercises

  1. It is known that the post-order sequence of a binary tree is DABEC and the in-order sequence is DEBAC, then the pre-order sequence is?

  2. In the following sequence, it is not possible to uniquely determine a binary tree (DIntermediate sequence (cannot be without D. Prior sequence and postorder sequence C. Post-order sequence and Intermediate sequence B. Pre-order sequence and mid-order sequence
    A. Hierarchical sequence and mid-order sequence).



Summary of Part One

Because the knowledge structure distribution of the tree section is different, and each part involves a large number of knowledge points, it is summarized separately.
Part One Mind Map


Part 2: Clue Binary Tree

Clue binary trees are an improvement over binary linked list storage.

  • Traditional binary linked list storage can only reflect a parent-child relationship, and cannot directly obtain the predecessor or successor of a node in the traversal.
  • And each leaf node and node with degree 1< a i=4> all have null pointers. Effective use of these null pointers to point to their corresponding predecessor nodes can speed up the search for the predecessor and successor of a node.

To this end, two identification fields need to be added to identify whether the pointer points to itsleft/right child or to its. Predecessor/successor during traversal

  • ltag: { 0, l c h i l d field indicates the left child 1 of the node, l c h i l d field indicates the predecessor of the node during traversal\begin{ cases} 0, the lchild field indicates the left child of the node\\ 1, the lchild field indicates the predecessor of the node during traversal\end{cases} { 0lchi ldarea indicating endpoint left child1lchi ldArea indication range time and point front
  • rtag: { 0, r c h i l d field indicates the right child 1 of the node, r c h i l d field indicates the successor of the node during traversal\begin{ cases} 0, the rchild field indicates the right child of the node\\ 1, the rchild field indicates the successor of the node during traversal\end{cases} { 0,rchil dRight child of area indication end1rchil dArea indication range time and point after point

For example: Construct the inorder clue binary tree of the following binary tree
Schematic diagram of in-sequence clue binary tree

Part 2 Exercises

Insert image description here


Part 3: How trees are stored

The storage structure of a binary tree was introduced earlier. Trees can also use sequential storage structures and chain storage structures, but no matter which storage structure is used, it needs to be able to uniquely reflect the logical relationship between the nodes in the tree.
The following describes three representations, corresponding to three storage methods (including one or two storage structures).

1. Expression of parents

The parent representation uses a set of continuous spaces (i.e. static linked list) to store each node.
Adopt sequential storage structure, and add a dummy to each node Pointer (a node is a structure object), indicating the position of its parent node in the array mark.

2. Child representation

Link the child nodes of each node with a single linked list in order from left to right (n nodes have n child linked lists).
Adopt the structure of sequence list + linked list and combine it with hierarchical traversal to implement this method. Each node of the linked list stores the child nodes of the current node in the treeThe corresponding array subscript, andthe address of the next linked list node.

3. Representation of children’s brothers

Child brother representation is also calledbinary tree representation. This representation is the most importantof the three!
uses binary linked list as the storage structure of the tree. The child sibling notation makes each node include three parts:

  • node value;
  • The left pointer points to the first child node of the node;
  • The right pointer points to the next sibling node of the node (all sibling nodes of the node can be found along this field).

Child brother representation can convert the tree into a binary tree:
Tree conversion binary tree flow chart
① Convert to the brothers of a tree The nodes are connected, and then the parents only retain the connection to the first child node, thereby generating a binary tree corresponding to ; < a i=6> ② Represent the binary tree corresponding to as a linked list. The nodes connected to the left of each node are pointed to by the left pointer, and the nodes connected to the right are pointed to by the left pointer. The node is pointed with the right pointer, forming binary tree representation; ③ In order to reflect the left and right pointers of each node more intuitively In the pointing situation, draw the binary tree linked list into the form of binary tree.


Part 4: Trees and Forests

1. Conversion of trees, forests and binary trees

  • The steps for converting tree to binary tree have been introduced in the previous section, and converting binary tree Back to the tree is also very simple:
    ① Reconnect the sibling node back to the parent node (the parent of each left child is it Parents of the node on the entire path on the right);
    ② Disconnect the connection between the sibling nodes ( The entire path on the right sideThe connection between the nodes).

In addition, binary trees and forests can also be converted to each other. (Direct conversion between general trees and forests is not introduced)

  • Convert forest to binary tree is relatively simple:
    ① Convert < in the forest a i=4>Each tree is first converted into a binary tree. At this time, the root node of each binary tree must have no right child; ② Convert The latter binary tree is regarded as the right subtree ofthe previous binary tree, and is spliced ​​in sequence. At the same time, the forest is converted into a binary tree.

  • Perform the above operation in reverse, and you can convert the binary tree into a forest:
    ① If the binary tree is not Empty, treat itsroot node and the left subtree of the root node as the first binary tree, disconnected from the right subtree;< a i=6> ② Repeat operation ① on the disconnected right subtree until a node has no right child (a node can also be alone into a tree); ② Convert each of the obtained binary trees back to a tree to obtain a forest.

2. Traversal of numbers and forests

Tree traversal:

  • Root first traversal: If the tree is not empty, visit the root node first, and then traverse each subtree of the root node in order from left to right.
    The access sequence is the same as the preorder traversal of the corresponding binary tree of this tree, that is, The sequence obtained by root-first traversal is the same as the sequence obtained by pre-order traversal of the binary tree converted by this tree.
  • Post-root traversal: If the tree is not empty, traverse each subtree of the root node in order from left to right, and then visit the root node.
    Its access sequence is the same as the in-order traversal of the corresponding binary tree of this tree, that is, The sequence obtained by post-root traversal is the same as the sequence obtained by in-order traversal of the binary tree converted by this tree.

Part 4 Exercises

  1. It is known that the preorder sequence and inorder sequence of a certain binary tree are ABDEHCFIMGJKL and DBHEAIMFCGKLJ respectively. Please draw this binary tree and the forest corresponding to the binary tree.
    Sequence to find binary tree and convert to forest

Part 5: Binary sorting tree

Binary sorting tree (BST), also calledbinary search tree or binary decision Tree (Fan formula: Small on the left and big on the right). Knowledge points that originally belonged to the search part of Chapter 7.

1. Definition of binary sorting tree

(1) If the left subtree is not empty, thenthe key values ​​of all nodes on the left subtree are< a i=3> is less than the key value of the root node. (2) If the right subtree is not empty, then the key values ​​of all nodes on the right subtree are < /span> (3) The left and right subtrees themselves are also a binary sorting tree (satisfying the above two points). . is greater than the keyword value of the root node

2. Insertion into binary sorting tree

The insertion operation is strictly abided by "If it is smaller than the current node, it enters the left subtree area, and if it is larger than the current node, it enters the right subtree area——< a i=2>The left is small and the right is large, until it becomes a leaf node.
Please insert in order: 53 17 78 9 45 65 94 81 88 23. The generated binary sorting tree is as shown in the figure:

3. Deletion of binary sorting tree

The implementation process of the deletion operation is handled according to three situations:
① If the deleted nodez is a leaf node , delete it directly.
② If node z has only one left subtree or right subtree , let the subtree of z be the top on, replacing the z position.
③ If node z has two subtrees , left and right, then first traverse in order to obtain the sequence, let z Replace z with the direct successor (or direct predecessor) in the sequence, and then delete the direct successor (or direct predecessor) from the binary sorting tree. At this time, it is converted to the first or second case.

The first small point example: delete 88;
The second small point example: delete 45;
The third small point example : Delete 78. Since the inorder expression: 9 17 23 45 53 65 78 81 88 94, so:

  • The direct precursor of 78 is 65, replace it with the type of the first small point;
  • The direct successor of 78 is 81, replace it with the type of the second small point;

4. Search in binary sorting tree

The search process isstarting from the root node and comparing the given value with the keyword of the node.

  • If the given value is equal to the keyword of the node, the search is successful;
  • If the given value is not equal to the node keyword, is less than the node keyword and enters the current oneThe left subtree of the nodecontinues to search,keywords greater than the nodeenter the current Search on the right subtree of node a>.

Find all given values ​​(the given value is generally the set of keywords that make up the tree) that need to be comparedThe total number of keywords, divided by the total number of given values ​​, the result is called average search length: ASL ——A: average; S: search; L: len.

Situation (a) Situation (b)
Find success/failureThe average search length ASL is:
{ A S L Success = ( 1 + 2 ∗ 2 + 3 ∗ 4 + 4 ∗ 3 ) / 10 = 2.9 A S L failure = ( 3 ∗ 5 + 4 ∗ 6 ) / 11 = 39 / 11 \begin{cases} ASL_{success}=(1+2*2 +3*4+4*3)/10= 2.9 \\ ASL_{failure}=(3*5+4*6)/11 = 39/11 \end{cases} { ASLSuccess=(1+22+34+43)/10=2.9ASLFailure=(35+46)/11=39/11
Find success/failureThe average search length ASL is:
{ A S L Success = ( 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 ) / 10 = 2.9 A S L Failure = ( 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 ∗ 2 ) / 11 = 65 / 11 \begin{cases} ASL_{success}=(1+2+3+4+5+6+7+8+9+10)/10= 2.9 \\ ASL_{failure}=(1+2 +3+4+5+6+7+8+9+10*2)/11 = 65/11 \end{cases} { ASLSuccess=(1+2+3+4+5+6+7+8+9+10)/10=2.9ASLFailure=(1+2+3+4+5+6+7+8+9+102)/11=65/11
Confirmation allSuccessUnder the circumstances:
First layer< a i=4>1Demand ratio for each element1th;th Two layers2Each element demand ratio2Next;Next;4 Demand ratio for each element3Fourth layerNext;3Demand ratio for each element4Third layer


All given values are searched successfully:
Each layer1 elements need to be comparedk times, k is the number of layers (depth);
All given valuesIf the search fails:
The third layer 12, 53, 60 There are a total of 5 failure possibilities, each of which needs to be compared 3 times; a> times;4, each needs to be compared6
There are 28, 40, and 70 possibilities for the fourth layer
All given valuesThe search fails:
Each of the first nine levels times;10 possibilities for failure in the tenth floor, all of which need to be compared2There are times, where k is the number of layers ( Depth);k failure possibilities require comparison1

Part 5 Exercises

  1. For the following keyword sequence, it is impossible to form a search path in a binary sorting tree (A).
    A.95, 22, 91, 24, 94, 71 (The binary sorting tree formed has a branch node with degree 2, which cannot be The number is the search path)
    B.92, 20, 91, 34, 88, 35
    C.21, 89, 77, 29, 36, 38< /span>
    D.12, 25, 71, 68, 33, 34

  2. Start from the empty tree and insert elements 52, 26, 14, 32, 71, 60, 93, 58, 24 and 41 in order to form a binary sorting tree. Find 60 in the tree. The number of comparisons to be made is (D).
    A.3 (In the binary sorting tree formed, the node depth of 60 is 3)
    B .4
    C.5
    D.6

  3. The sequence obtained by traversing a binary sorting tree in order is (50, 38, 30, 45, 40, 48, 70, 60, 75, 80). Try to draw the binary sorting tree and find the equal probability Average search length for successful and failed searches.


Summary of Parts 2 to 5


Part 6: Balanced Binary Tree

1. Definition of balanced binary tree

also known asBalanced Tree (AVL): The absolute value of the height difference between the left and right subtrees of any node does not exceed 1, and Subtrees are all balanced trees.
Define the height difference between the left subtree and the right subtree of each node as the balance factor of the node, The only possible values ​​for the balancing factor are -1, 0 or 1. The balance factor of a leaf node must be 0. You are generally not asked to calculate the balance factor in the exam. Knowing this concept is to judge the balanced binary tree.

2. Minimum balanced binary tree formula★

Minimal balanced binary tree formula:F(n)=F(n-1)+F(n-2)+1, use It is used to calculate the minimum number of nodes required to construct a balanced binary tree of a certain height. Among them:

  • nhigher altitude,F(n)more points;
  • F(n-1) is the total number of nodes in the left subtree, F(n-2)< a i=3> is the total number of nodes in the right subtree;
  • 1This is the root point.

Example: F(1)=1, F(2)=2, Obtain: F(3)=2+1+1=4;{ F(h) | h = 1, 2, 3, … } = { 1, 2, 4, 7, 12, 20, 33, … }.

【example】

  1. The maximum depth of a balanced binary tree containing 20 nodes is (C).
    A.4
    B.5
    C.6 (List the sequence: 1, 2, 4, 7, 12, 20, it can be seen that F(6)=20, that is, the maximum depth h=6)
    D.7

  2. Among the following two sorted trees, the one that satisfies the definition of a balanced binary tree is (B).

3. Insertion of balanced binary tree

The balanced binary tree actually introduces new constraints to the binary sorting tree tolimit the height of the binary sorting tree, thus Speed ​​up searches (reduce average search paths). Therefore, the insertion operation of a balanced binary tree also follows the "left is small and the right is large" method, and attention must be paid to ensure that each node is The absolute value of the balance factor cannot exceed 1.

When a node whose absolute value of the balance factor is greater than 1 appears after insertion, the current tree needs to be adjusted to turn it back into a balanced binary tree. Adjustment needs to follow two principles:① Reduce the height; ② Maintain the binary sorting tree properties.

(1)LL type:right rotation
LL type insertion process
( 2)RR type:left rotation
RR type insertion process
(3) LR type:first left turn then right turn
LR type insertion process
(4)RL type:first right turn then left turn
RL type insertion process
:

  • The "root node" transformed here is not necessarily the root node of the entire tree (so the ellipsis is shown above). The tree we transformed is called "Minimal Unbalanced Subtree" , and the root node of this subtree is The nearest node to the insertion point whose absolute value of the balance factor is greater than 1;
  • L and R in each type only depend on the selection of the two downward paths of"root node", and subsequent insertions The path does not affect the category of"minimally unbalanced subtree". That is to say, after walking through two paths and entering the subtree, there will still be left insertion and right insertion. In two cases, the above four categories can be further subdivided into: LL(L), LL(R), RR(R), RR( L), LR(L), LR(R), RL(L), RL(R); (The picture above shows LR(R) and RL(R)) a>

4. Deletion of balanced binary tree

The deletion operation of a balanced binary tree is also the same as that of a binary sorted tree. Different transformations are required according to the different situations of deleting nodes. If the "balance" of the tree is also destroyed during the transformation, find out just like in the insertion operation "Minimal unbalanced subtree", and then reach the deleted node from the "root node" according to >Determine the type of the first two segments of the path of Perform corresponding transformations. the "minimum unbalanced subtree", and then compare

Part 6 Exercises

  1. After inserting keyword 48 into the balanced binary tree as shown below, a new balanced binary tree is obtained. In the new balanced binary tree, the keywords stored in the left and right child nodes of the node where keyword 37 is located are They are (C) respectively.
       A.13, 48   B.24, 48   C.24, 53   D.24, 90

[Analysis] This insertion is of RL type. After corresponding transformation, the child nodes of 37 are 24 and 53.   


Part 7: Huffman Tree

1. Definition of Huffman tree

Given the node weight of the tree, the result is weighted tree.

  • We call the product of the path length from the root node to a weighted node and the weight value, called the weighted path length of the node.
  • The sum of the weighted path lengths of all leaf nodes is called the weighted path of the tree Path length - WPL (W: weighted; P: path; L: len).
  • The binary tree with weighted path lengthminimum is calledHuffman tree< a i=4>, also called optimal binary tree.

[Classic example] A binary tree has 4 leaf nodes a, b, c, and d, with weights 7, 5, 2, and 4 respectively. The minimum weighted path length of this binary tree is:

2. Structure of Huffman tree

The structure of the Huffman tree is described in detail in the textbook. Here is a brief summary with instructions:
(1) The structure of the forest is all roots;
(2) Select two small ones to create new ones;
(3) Delete two small ones and add new ones;
(4) Repeat 2 and 3 to leave a single root.

A simple summary is to use a single node to store the weight, select the two nodes with the smallest weight each time as the left and right child nodes to construct a tree, then delete these two nodes and replace The sum of their weights is stored in the root node, then the root node is added to the optional nodes, and then two new minimum nodes are re-selected and the above steps are repeated until there is only one optional node left.

  • In this way, selecting the minimum weight each time can ensure that the node with a larger weight is close to the root node, so that the node is weighted The path is small.

3. Huffman coding

The purpose of Huffman coding is to obtainthe optimal prefix encoding.

  • Prefix encoding: In an encoding scheme, any encoding is not the same as any other encodingPrefix (that is, the leftmost substring, such as "01" is the prefix of "010");
  • Optimal encoding: Compress and encode the file with this encoding scheme, which can make the obtained binary file the shortest.

Realization of Huffman coding: (Function: Left and right )

  • Construct a Huffman tree using the number of times the character appears in the file as the weight;
  • Assign 0 to the left branch of the Huffman tree and 1 to the right branch, then from the root node to each leaf node On the path, the assignment of each branch constitutes a binary string, and this binary string is used as the encoding of the character corresponding to the leaf node.

Part 7 Exercises

  1. The 5 characters of have the following 4 encoding schemes. The ones that are not prefix encoding are (D).
    A.01,0000,0001,001,1
    B.011,000,001,010,1
    C .000, 001, 010, 011, 100
    D.0, 100, 110, 1110, 1100 (Obviously 110 is the prefix of 1100. Of course we also You can draw a binary tree with 0 on the left and 1 on the right. Assume that the five characters are A, B, C, D, and E. After drawing, you will find that C is the parent node of E, not a leaf node.)

  2. Assume a given weight set w = [5, 7, 2, 3, 6, 8, 9], try to construct a Huffman tree about w, and find its weighted path length WPL.

  


Summary of Parts Six to Seven

Guess you like

Origin blog.csdn.net/qq_50571974/article/details/126654353