[Data Structure]——Tree and Binary Tree Short Answer Question Template

1. The concepts of trees and binary trees

(1) Definition and properties of binary tree

1. Briefly explain the concept of binary tree. What are the properties of binary trees. (Write at least 3)

: Binary tree is a tree structure. Each node has at most two subtrees, that is, there is no node with degree greater than 2 in the binary tree, and the subtrees of the binary tree are also divided into left and right.
Properties:
①The leaf nodes of a non-empty binary tree are equal to the number of nodes with degree 2 plus 1, that is, n0 =n2+1;
②The binary tree with height h has at most 2h-1 node;
③A complete binary tree with tree height h has at least 2 a>h-1 nodes, at most 2h-1 nodes ,
④In a complete binary tree with tree height h, the relationship between the summary point N and the height h is h=⌈log2
< a i=17>(n+1)⌉; ⑤For a binary tree containing n nodes, when it is a complete binary tree, it has a minimum height, and the minimum height is h=⌈log2 (n+1)⌉ or ⌊log2n⌋+1; when it is a single branch tree, it has the maximum height and is a linear table, that is, the maximum height is n.

(2) The difference between trees and binary trees

1. What is the difference between a tree and a binary tree?

: Both trees and binary trees have one and only one root node. The root node has no predecessor node. Except for the root node, all other nodes have only one predecessor node. The remaining nodes are m (m≥0) mutual nodes. Disjoint, non-empty sets. There can be any number of successor nodes of a node in a tree, but there can only be at most two successor nodes of a node in a binary tree, that is, the degree of a binary tree is less than or equal to 2.

2. Complete binary tree and full binary tree

1. What is a complete binary tree? What is a full binary tree? What is the relationship between them?

: A binary tree with depth (height) h and 2h-1 nodes, in which the nodes at each level are full A binary tree is called a full binary tree. If, except for the last layer, all the nodes in the remaining layers are full or the right subtree of a node lacks several consecutive nodes, it is called a complete binary tree. A full binary tree is a special case of a complete binary tree. It can be said that if a tree is a full binary tree, it must be a complete binary tree, but it cannot be said that a complete binary tree must be a full binary tree.

3. Binary tree traversal

(1) Determine the binary tree from the sequence

1. Can a binary tree be uniquely determined by pre-order traversal and post-order traversal sequence of a binary tree? Please explain with examples.

:cannot. For example, the preorder and postorder sequences of the two binary trees below are the same, but they are not the same binary trees.
Insert image description here

2. Briefly explain why the in-order traversal sequence of a binary tree and its pre-order or post-order traversal sequence can uniquely determine a binary tree.

: In the in-order traversal sequence of a binary tree, the left, right, left, middle, right traversal relationship and the root node can confirm the left and right subtrees of the binary tree, and at the same time, the parent-child relationship in the node can be determined by the pre-order or post-order traversal sequence of the binary tree. Thus the combination can uniquely determine a binary tree.

(2) Relationship between different traversal sequences

1. Try to analyze under what circumstances the pre-order traversal sequence of a non-empty binary tree is the same as the post-order traversal sequence, and the pre-order traversal sequence is opposite to the post-order traversal sequence?

Answer: If a binary tree is an empty tree or has only a root node, its preorder traversal sequence and postorder traversal sequence are the same. If a non-empty binary tree has only one leaf node or the height of the binary tree is equal to the number of nodes, the preorder traversal sequence and the postorder traversal sequence are opposite.

4. Properties of binary trees

(1) Relationship between node degrees of binary trees

1. Prove that in any binary tree, if the number of leaf nodes is n0, the number of nodes with degree 2 The number is n2, then n0=n+1. 2

: Let the number of nodes with degree 1 in the binary tree be n1, since the degree of all nodes in the binary tree is less than or equal to 2, so the number of summary points is N=n0+n1+n2, in addition, except for the root node, other nodes in the binary tree have at least one branch, and the node with degree 1 has one branch , a node with degree 2 has two branches, then the total number of branches =N-1=n1+2n2, combining the two formulas, we can get n0=n 2+1.

(3) The minimum and maximum depth (height) of a binary tree

1. When does a binary tree with n nodes reach its maximum depth? At what point does its depth reach a minimum?

: When a single tree is used, the depth of a binary tree is the largest, with a maximum depth of n levels; if the depth is h, the number of nodes satisfies 2h+1-1, that is, when the binary tree is full, the depth of the binary tree is the smallest.

5. Binary tree and tree storage structure

(1) Sequential storage structure of binary tree

1. Briefly describe the sequential storage structure of binary trees and its advantages and disadvantages.

: The sequential storage structure of the binary tree is stored by using a set of storage unit arrays with consecutive addresses. The advantage is that when reading the specified node, it can be accessed directly through the array subscript, which is highly efficient and can be quickly accessed. The left and right children and parent nodes are found, but the disadvantage is that there is a waste of space. A non-complete binary tree with a height (depth) of h needs to occupy 2h-1 array storage unit.

(2) Three storage structures of trees

1. Introduce the three storage methods of trees.

: ① Parent representation: For each node, only the position of its parent node is stored, that is, a one-dimensional array is used to store the data field of the node and the array subscript to store the node's parents. . It is easy to find the parents and ancestor nodes of each node through the parent representation, but it is more troublesome to find the child nodes or sibling nodes of the node. It needs to traverse the entire array, which is less efficient; < /span>
Each node is assigned a structure type, including the data field and the parent field, which respectively store the node's data field and the array subscript that stores the node's parents.

②Child representation: For each node, only the position of its first child node and the position of the next sibling node are stored. It is easy to find the child nodes of the node through the child linked list representation, but it is more troublesome to find the parents of the node. It is necessary to traverse the n child linked lists pointed to by the child linked list pointer fields in the n nodes;

③Child brother representation: For each node, only the position of its first child node and the position of its next sibling node are stored. This method is very efficient in finding all child nodes and all ancestor nodes of a node. It is also very efficient in finding all sibling nodes of a node. However, the disadvantage is that it is more troublesome to find its parent nodes from the current node. .

6. Transformation of trees, forests and binary trees

1. What is the ultimate purpose of converting trees and forests into binary trees?

: ① Simplify operations. In a binary tree, only the left and right sub-nodes of each node need to be considered, which makes operations such as search, insertion, deletion, etc. simpler. For trees or forests with multiple sub-nodes, it can be converted into a binary tree. Reduce complexity;

② It is convenient to use the binary tree algorithm. After conversion, many efficient and effective binary tree algorithms, such as search, insertion, deletion and other operations of the binary search tree, can be easily applied to the converted binary tree, but the tree and forest are complex;

③Unified storage. After converting trees and forests into binary trees, unified storage can be achieved, that is, various advantages of binary trees can be used to simplify operations and improve efficiency, such as being able to organize data in a recursive manner, etc.

7. Clue Binary Tree

1. What is the function of clue binary tree?

: Since there are n+1 null pointers in a binary tree containing n nodes. For leaf nodes, it has two null pointers, and for nodes with degree 1, there is only one null pointer. Utilize these null pointers to store the predecessor or successor information pointing to the node, thereby making it easier to traverse the binary tree and speeding up the search for the predecessor or successor of the node. At the same time, it can also save memory overhead.

8. Prefix encoding

1. What is prefix encoding? Is Huffman coding a prefix coding? Briefly describe its function.

: When encoding a character set, if the encoding of any character in the character set is not a prefix of the encoding of other characters, this encoding is called prefix encoding. Huffman coding is a prefix-free encoding that uses binary information of unequal lengths for encoding through variable-length encoding, so that the average encoding length of characters is shortened, thereby better compressing data for transmission.

9. Huffman tree

(1) Definition of Huffman tree

1. What is a Huffman tree? If a Huffman tree has n leaf nodes, what is the total number of nodes?

: Huffman tree is an optimal binary tree. Given n leaf nodes with weights, construct a binary tree so that the weighted path length of the constructed binary tree is the minimum, which is the optimal binary tree, also known as Huffman tree. If the Huffman tree has n leaf nodes, since there are only nodes with degrees 0 and 2 in the Huffman tree, and there is no node with degree 1, the total number of nodes is N=2n-1.

(2) Construction steps of Huffman tree

1. Write down the idea of ​​Huffman algorithm for constructing Huffman tree.

: Based on the initial forest composed of a given n weighted nodes, first, select the two trees with the smallest weights as the left and right subtrees and add them together. The sum of the weights is a new root node. Then, insert the new node into the forest, delete the left and right subtrees from the forest, and repeat the selection until there is only one tree in the forest, which is the Huffman tree.

(3) Huffman coding

1. What is Huffman coding?

: Huffman coding is a variable-length coding and a prefix-free coding. It sets a binary code for each character according to the probability of its occurrence. It stipulates that one code cannot be the prefix of other codes. It is mainly used in data compression. , encryption and decryption and other scenarios.

10. Binary sorting tree

1. Briefly describe the binary sorting tree.

: Binary sorting tree is also called binary search tree or binary search tree, in which the size relationship of each node value is: left subtree<root node<right subtree, and the left and right subtrees are also the same A binary sorting tree satisfies its conditions. If the tree is traversed in inorder, an increasing sequence can be obtained.

Guess you like

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