【Data Structure】—Tree Related Exercises

1. Choose to fill in the blanks

Question 1

1. Suppose there are only nodes with degree 0 and degree 2 on the binary tree with height h, then the number of nodes contained in the binary tree is at least () and at most ().
A, h; 2 h -1
B, 2h-1; 2 h -1
C, 2h+1; 2 h-1 -1
D, h+1; 2 h -1

Analysis: (B)
In the least case, except for the root node, which has 1 node, the other h-1 layers have 2 nodes, so 2(h-1), that is, 2(h-1)+1= 2h-1.
In the most cases, except the last layer with degree 0, the other nodes are nodes with degree 2, that is, 2 h -1 nodes.

Question 2

2. A complete binary tree with 124 leaf nodes has at most ( ) nodes.
A, 247
B, 248
C, 249
D, 250

Analysis: (B)
Since n 0 =n 2 +1, and N=n 0 +n 1 +n 2 , the difference between the nodes with degree 0 and degree 1 is 1, so there are only n 1 nodes with degree 1 in the complete binary tree It may be 0 or 1, that is, n 1 =1 when the number of summary points is even , and n 0 =0 when it is odd .
In this question, n 0 =124, n 2 =123 can be obtained , because the maximum number of nodes is considered, that is, n 1 =1, so N=124+123+1=248.

Question 3

3. If a binary tree has 126 nodes, there are at most () nodes in the seventh layer (the root node is in the first layer).
A, 32
B, 64
C, 63
D, there is no layer 7

Analysis: (C)
Consider the situation that the nodes from the first layer to the sixth layer are full, that is, the number of nodes from the first layer to the sixth layer is 1+2+4+8+16+32=63. The seventh layer can have up to 64 nodes. Since the binary tree has 126 nodes, there are 126-63=63 nodes in the seventh layer.

Question 4

4. A binary tree with n nodes uses a secondary linked list to store nodes, in which the number of non-null pointers is () and the number of null pointers is ().
A, n+1; n-1
B, n; n-1
C, n-1; n+1
D, 2n; 2n-1

Analysis: (A)
The number of n nodes has n-1 branches, each branch corresponds to a pointer, so the number of non-null pointers is n-1; in addition, since each node contains 2 pointer fields (the left pointer field lchild , right pointer domain rchild), the total number of pointer domains minus the number of non-null pointers is the number of null pointers, 2n-(n-1)=n+1.

Question 5

5. In the preorder sequence, inorder sequence and postorder sequence of the binary tree, the order of all leaf nodes is ().
A. Not the same
B. Exactly the same
C. The pre-sequence and the mid-sequence are the same, but different from the post-sequence
D. The mid-sequence and the post-sequence are the same, but different from the pre-sequence

Analysis: (B)
In the three traversal methods, the order of visiting the left and right subtrees is the same, but the order of visiting the root node is different, so the order of all leaf nodes is the same.

Question 6

6. The clue binary tree is a () structure.
A, logical
B, logical and storage
C, physical
D, linear

Analysis: (C)
A binary tree is a logical structure, while a threaded binary tree is a linked list structure with clues, which is a physical structure.

Question 7

7. In a Huffman tree with n leaf nodes, the total number of non-leaf nodes is (); if the Huffman tree has 215 nodes, perform Huffman encoding on it, and you can get () different codewords.
A, n-1; 108
B, n; 107
C, 2n-1; 214
D, 2n; 215

Analysis: (A)
There are only nodes with degrees 0 and 2 in the Huffman tree. From n 0 =n 2 +1, it can be obtained that the total number of non-leaf nodes is n-1.
The total number of nodes N=n 0 +n 2 , and n 0 =n 2 +1, N=215 can be substituted, n 0 =108.

Question 8

8. Find the binary tree with the following conditions:
A. The pre-order traversal sequence is the same as the post-order traversal sequence, which is ()
B. The middle-order traversal sequence is the same as the post-order traversal sequence, which is ()
C. The pre-order traversal sequence is the same as the middle order The traversal sequence is the same, which is ()
D. The in-order traversal sequence is the same as the hierarchical traversal sequence, which is ()

Analysis:
A. Empty tree or binary tree with only root node;
B. Empty tree or any node has at most left subtree;
C. Empty tree or any node has at most right subtree;
D. Empty tree or any node A node has at most only right subtree.

Question 9

9. (Judgment) In the Huffman coding tree, two characters with the same frequency have the same Huffman coding.

Analysis: (×)
There will not be the same Huffman code, unless their characters are the same, so the resulting Huffman code is also the same.

2. Application questions

Question 10 (traversal of sequences)

Question type :通过已知树的遍历序列,求其他遍历序列

10. The in-order traversal sequence of a binary tree is ABCDEFG, and the post-order traversal sequence is BDCAFGE. Find its pre-order traversal sequence.

Solution: It can be seen that the traversal order of the first, middle and post-order traversal sequences and the hierarchical traversal sequences of the binary tree is as follows:

iterate over names traversal rules
preorder traversal sequence root > left > right
inorder traversal sequence left > root > right
postorder traversal sequence left > right > root
hierarchy traversal sequence left > right

First, restore the binary tree through the given two sequences, and the post-order traversal sequence is BDCAFGE, so the last element "E" of the sequence is the root node of the binary tree, so it can be obtained: continue to the left subtree and right
insert image description here
subtree In-order traversal and post-order traversal are split, first look at the left subtree of E, from the post-order sequence we can know that A is the root node in the left sub-tree, and from the post-order sequence we can know that A has no left subtree, and the right subtree is BCD; similarly, in the right subtree of E, G is the root node in the right subtree, but it is impossible to judge whether F is the left subtree or the right subtree, so in the middle order traversal sequence, the left side of G is F, According to the in-order traversal rules, F is the left subtree of G, and G has no right subtree, as follows: Same as above, it can be
insert image description here
obtained that C is the right subtree of the root node A, the left subtree of C is B, and the right subtree As D, the following is a complete binary tree:
insert image description here
so its preorder traversal sequence can be obtained: EACBDGF.

Question 11 (Storage structure)

Question type :根据树的存储结构,画出二叉树

11. The figure below is a sequential storage structure of a binary tree, where a blank indicates that the node does not exist, draw the binary tree, and find the inorder sequence and postorder sequence of the binary tree.

insert image description here
Solution: It is stored as a complete binary tree, and the blank is empty, as follows:
insert image description here
Note: The black mark is just to show the binary tree more clearly. It is recommended to only leave the blue part of the binary tree.
Inorder sequence: BADCFE; postorder sequence: BDFECA.

Question 12 13 (conversion between binary tree/tree and forest)

Question type one :根据所给二叉树,将其转换为树

12. Convert the following binary tree to a tree.

insert image description here
Solution: The first step is to rotate. Rotate all the nodes in the binary tree except the left and right subtrees of the root node to the right by 45°, as follows:
insert image description here
The second step is to connect the lines. If the left child of a node has a right subtree, the right subtree is connected to the node, and the left child B of node A has a right subtree D, so A is connected to D; the left child E of node C has The right subtree is H, so connect C to H, as follows:
insert image description here
The third step is to disconnect. Delete the connection between the right branch of the right subtree of each node and its parent node except the root node, as follows:
insert image description here
You can get the tree converted from the binary tree:
insert image description here
Question type 2 :根据所给森林,将其转换为二叉树

13. Given the following forest consisting of three trees, convert it into a binary tree.

insert image description here
Solution: The first step is to connect. First connect the sibling nodes of all nodes, and also connect the root nodes of each tree, as follows:
insert image description here
The second step is to disconnect. Only the leftmost children of all nodes are kept, while other nodes are disconnected, as follows:
insert image description here
After disconnection:
insert image description here
The third step is to rotate. Taking the root node of the first tree as the axis, rotate 45° clockwise, as follows:
insert image description here

Question 14 (threaded binary tree)

Question type :根据所给二叉树,画出该二叉树的线索二叉树

14. Let the pre-order and in-order traversal sequences of a binary tree be ABDFCEGH and BFDAGEHC respectively, and find the post-order clue tree of this binary tree.

Solution: First, find the binary tree, you can get:
insert image description here
the first step, find the first/middle order traversal sequence of the corresponding clue binary tree to be drawn. It can be obtained that the postorder traversal sequence is FDBGHECA.
The second step is to draw clues (for nodes that lack left and right children in the binary tree). If the node has no left child, the clue points to the predecessor of the first/middle/post-order traversal sequence of the corresponding clue binary tree, and if there is no predecessor, it points to NULL; if the node has no right child, the clue points to the first/middle/post-order traversal sequence of the corresponding clue binary tree The successor of the in/postorder traversal sequence, or NULL if there is no successor.
For example, node D has no right child, and the successor of node D in the post-order traversal sequence is B, so it points to B; node F has no left child, and node F has no predecessor node in the post-order traversal sequence, so it points to NULL; node H has no left child and right child. In the postorder traversal sequence, the predecessor of node H is G, and the latter is E, so they point to G and E respectively, as follows:
insert image description here

Question 15 16 (Huffman tree)

Question type one :已知字符的频数,设计哈夫曼编码,并画出相应的哈夫曼树

15. The characters appearing in a message are a, b, e, l, m, n, o, u, y, g, and their frequencies of appearing in the message are 43, 15, 11, 7, 9, 6 respectively , 45, 25, 5, 3.
(1) Design the Huffman encoding of each character;
(2) Draw the Huffman tree.

Solution:
Question Type 2 :已知字符的概率,设计哈夫曼编码,并画出相应的哈夫曼树

16. If only five kinds of characters A, B, C, D and E can appear in the communication system, their probabilities are 0.12, 0.15, 0.19, 0.21 and 0.33 respectively.
(1) Draw the Huffman tree;
(2) Design the Huffman code.

Solution: (1) Expand the frequency by 100 times, corresponding to 12, 15, 19, 21, and 33 respectively,
first select two trees with the smallest root node weights as the left and right subtrees, and the root node weights of the new binary tree The value is the sum of its weights, and new nodes are added to it.
12+15=27, add 27, as shown in the figure below:
insert image description here
Continue to merge:
insert image description here
27 and 33 are merged, 27+33=60:
insert image description here
40+60=100, 100 is merged:
insert image description here
(2) Carry out Huffman coding, from the root From node to leaf node, the left branch is 0, and the right branch is 1, as follows:
insert image description here
You can get:

A B C D E
100 101 00 01 11

Guess you like

Origin blog.csdn.net/qq_43085848/article/details/130982176