Key points of learning tree and binary tree

Overview

The tree structure is a very important non-linear structure with strict hierarchical characteristics, which is the focus and difficulty of this course. The content of this chapter consists of two parts: tree and binary tree, and the storage structure of binary linked list is used as the medium to realize the conversion between tree and binary tree. The organizational structure of the knowledge points in this chapter is shown in the figure below:
Insert picture description here
Insert picture description here
Of course, there are forests, trees, and binary trees. The problem of conversion between.

Key points/difficulties/points

The focus of this chapter is:

  1. The nature of the binary tree;
  2. Storage representation of binary trees and trees;
  3. Traversal of binary tree and algorithm implementation;
  4. The conversion relationship between tree and binary tree;
  5. Huffman tree and its application.

The difficulty of this chapter is:

  1. Non-recursive implementation of binary tree traversal algorithm;
  2. Realize other operations of the binary tree based on the traversal of the binary tree;
  3. The realization of the basic operations of the tree.

Key points
for tree learning : For trees, we must grasp a clear line: the logical structure of the tree and the storage structure of the tree. One key point: tree traversal operations (three types).
For the logical structure of the tree, we should start from the definition of the tree, and on the basis of comparison with the definition of the linear table, grasp the main points to understand the definition of the tree and its logical characteristics, understand the basic terms of the tree through specific examples, and understand the tree logically Traverse operation, and finally give the abstract data type definition of the tree.
For the storage structure of the tree, it is necessary to start from how to represent the logical relationship between the nodes in the tree, and master the different storage methods of the tree and the relationship between them.

Binary tree learning points :
For a binary tree, we must grasp a clear line: the logical structure of the binary tree-the storage structure of the binary tree → the realization of the binary tree. One key point: the traversal operation of the binary tree (four kinds) and its realization.
For the logical structure of a binary tree, we should start from the definition of a binary tree, and on the basis of comparing with the definition of a tree, understand that tree and binary tree are two tree structures, and deepen the understanding of the logical structure of the binary tree through the nature of the binary tree, and grasp it logically The traversal method of the binary tree, and finally the abstract data type definition of the binary tree is given.
For the storage structure of a binary tree, it is necessary to start from the logical characteristics and basic properties of the binary tree, grasp the different storage methods of the binary tree and the relationship between them, and discuss the realization of the binary tree traversal operation based on the storage structure of the binary linked list.

Knowledge points

  1. The tree is n (n ≥ 0) n(n\geq0)n(n0 ) A finite set of nodes. Any non-empty tree satisfies:
    ①There is one and only one specific node called the root;
    ②Whenn> 1 n>1n>At 1 , the nodes other than the root node are divided intom (m> 0) m (m>0)m(m>0 ) a finite set of disjointT 1, T 2, ..., T m T_1,T_2,\cdots ,T_mT1,T2,,Tm, Each set is a tree, and is called a subtree of this root node.
  2. The number of subtrees owned by a node is called the degree of the node; the maximum degree of each node in the tree is called the degree of the tree. A node with a degree of 0 is called a leaf node; a node with a degree of non-zero is called a branch node. The root node of the subtree of a node is called the child node of that node; otherwise, the node is called the parent node of its child node. The level of the root node is specified as 1. For any other node, if a node is at level k, its child node is at level k+1; the maximum level of all nodes in the tree is called the tree’s depth. m (m ≥ 0) m(m\ge0)m(m0 ) A collection of disjoint trees constitutes a forest.
  3. Tree traversal means starting from the root node and visiting all nodes in the tree in a certain order, so that each node is visited once and only once. There are usually three ways of pre-order traversal, post-order traversal and layer sequence traversal.
  4. The storage structure of the tree includes parent representation, child representation, child parent representation, and child brother representation.
  5. Binary tree is n (n ≥ 0) n(n\geq0)n(n0 ) A finite set of nodes, which is either an empty set (called an empty binary tree), or consists of a root node and two disjoint left and right subtrees called root nodes, respectively The composition of the binary tree.
  6. Binary trees and trees are two tree structures, and binary trees are not trees with degree 2.
  7. Binary tree having the following properties:
    ① is binary tree iiThere are at most2 i − 1 2^{i-1}on the i layer2i 1 node (i ≥ 1 i ≥ 1i1 );
    ②In a binary tree with depth k, there are at most2 k − 1 2^{k}-12k1 node, there are at least k nodes;
    ③In a binary tree, if the number of leaf nodes isn 0 n_0n0, The number of nodes with degree 2 is n 2 n_2n2, Then n 0 = n 2 + 1 n_0=n_2+1n0=n2+1
  8. A complete binary tree has the following properties:
    ① The depth of a complete binary tree with n nodes is ⌊ log ⁡ 2 n ⌋ + 1 \lfloor \log_2n\rfloor+1log2n+1 .
    ②For a tree withnnThe nodes in a complete binary tree with n nodes are numbered in the order of hierarchy starting from 1, then for any number,i (1 ≤ i ≤ n) i(1≤i≤n)i(1in ) 's node (referred to as nodeiii ), there are:
    ■ifi> 1 i>1i>1 , then the parent number of node i is⌊ i / 2 ⌋ \lfloor i/2\rfloori / 2 , otherwise nodeiii is the root node, without parents;
    ■if2 i ≤ n 2i\leq n2 in , then nodeiiThe left child of i is numbered2 i 2i2 i , otherwise nodeiii has no left child;
    ■if2 i + 1 ≤ n 2i+1≤n2 i+1n , then nodeiii the right of the child number2 i + 1 2i + 12 i+1 , otherwise nodeiii has no right child.
  9. Binary tree traversal methods usually include pre-order traversal, middle-order traversal, post-order traversal and layer-order traversal.
  10. Knowing the pre-order and middle-order sequence of a binary tree, or the middle-order sequence and the post-order sequence, can uniquely determine the binary tree; however, knowing the pre-order and post-order sequences of a binary tree cannot uniquely determine a binary tree .
  11. The sequential storage structure of a binary tree is generally only suitable for storing a complete binary tree.
  12. The most commonly used storage structure of a binary tree is a binary linked list. In addition, there are three-point linked lists and clues linked lists.
  13. There is a one-to-one correspondence between the tree and the binary tree and can be converted to each other.
  14. The Huffman tree is a binary tree with the smallest weighted path length. In the Huffman tree constructed with given n weights, there are nnn leaf nodes,n − 1 n-1n1 branch node.
  15. The code constructed by the Huffman tree is an unequal-length code that can minimize the total length of the code, and the Huffman code is a prefix code.

Exercise

The logical structure of
the tree There is no loop in the tree. The root cause is (A).
A. Subtrees do not intersect each other. B. A tree is a hierarchical structure. C. The definition of a tree is recursive. D. A tree has only one root node.
In the tree structure, the logical relationship is the relationship between brothers. (×)
In the tree structure, the path is sometimes not unique. (×)
In the tree structure, there is only one root node, but there must be multiple leaf nodes. (×)
In the preorder traversal sequence of the tree, any node is in front of its children. (√)

The storage structure
of the tree The key to the storage structure of the tree is how to store the parents or children of the node. (√)
The parent representation of the tree can store node data information in any order. (√)
In the parent representation of the tree, the average time complexity for finding the child nodes of a node is O(n). (√)
In the child representation of the tree, to find the parent node of a node, the average time complexity is O(n). (×)
The child brother representation of the tree is a link storage structure. (√)

The logical structure of a
binary tree A binary tree is a tree whose degree is less than or equal to 2. (×) Also divided into left and right
Binary tree is the simplest binary tree. (√)
A binary tree with a depth of k and k nodes must be an oblique tree. (×) can be a thunderbolt shape.
If all nodes are leaves, or there are left and right subtrees, it must be a full binary tree. (×) To be at the same level
, the depth of the complete binary tree is the smallest among the binary trees with the same number of nodes. (√)
There are at most 6 nodes on the third level of the binary tree. (×) If
a binary tree has 10 leaf nodes, there must be (B) nodes with degree 2.
A.8 B.9 C.12 D. Uncertain.
For the complete binary tree numbering from 1 in order, the left child of node 5 is (B), and the right child is (A).
A.11 B.10 C.9 D.8
As shown in the figure below, the pre-order traversal sequence of the binary tree is (B), the middle-order traversal sequence is (C), the post-order traversal sequence is (E), and the layer-order traversal sequence is (A).
A.(A,B,C,D,E,F,G)
B.(A,B,D,G,C,E,F)
C.(D,G,B,A,E,C,F )
D.(G,D,B,A,E,C,F)
E.(G,D,B,E,F,C,A)
F.(D,G,B,E,F,C, A)
Insert picture description here

The storage structure
of the binary tree For the sequential storage of the binary tree, a one-dimensional array is used to traverse the storage nodes in preorder. (×)
The sequential storage of binary trees is generally only used to store complete binary trees. (√)
In a binary linked list, the left and right child pointers of the leaf nodes are all null pointers. (√)
There are n+1 null pointers in the binary linked list of n nodes, which reduces the space utilization. Therefore, the binary linked list is usually not used to store the binary tree. (×)
5. Finding the parent node of a node in the binary linked list, the average time complexity is O(n). (√)
As the binary tree shown in Figure 5-4 above, the preorder traversal sequence of the extended binary tree is (C).
A. (ABD#G##CEF#####)
B. (AB DG####CEF####)
C. (ABD#G###CE##F##)
D. (AB#D# G##CE##F##)


Expanding (expanding) a binary tree is a type of binary tree, which refers to a binary tree formed by adding empty leaves where empty subtrees appear in the binary tree.
Since none of the pre-order, middle-order and post-order sequences can uniquely determine a binary tree, the binary tree is processed as follows, and the empty nodes of the binary tree are filled with · as shown in the figure. We call the binary tree processed in this way the extended binary tree of the original binary tree, and the pre-order and post-order sequences of the extended binary tree can uniquely determine its binary tree.
Insert picture description here


The tree
is converted from a tree to a binary tree, and the right subtree of its root node is always empty. (√) After the
tree is converted to a binary tree, the sibling relationship in the tree is a (B) relationship in the binary tree, and the parent and eldest-child relationship in the tree is a (A) relationship in the binary tree.
A. Parents and left children B. Parents and right children C. Brothers D. Ancestor
The preorder traversal sequence of the tree is equivalent to the preorder traversal sequence of the binary tree, and the postorder traversal sequence of the tree is equivalent to the postorder traversal sequence of the binary tree. () To
convert a binary tree to a tree or forest, you need to remove all connections between the parent and the right child in the binary tree. (×)
Binary tree is converted to forest. The number of trees in the forest depends on the number of nodes on the rightmost branch of the binary tree. (√)

Optimal Binary
Tree There is no node with degree 1 in the optimal binary tree. (√)
In the optimal binary tree, the leaf node with the larger weight is closer to the root node. (√)
In the optimal binary tree, the value of the root node is equal to the sum of the weights of all leaf nodes. (√)
Generally speaking, the coding efficiency of unequal length coding is higher than that of equal length coding. (×)
In a group of codes, if a code is a prefix of other codes, the decoding may not be unique. (√)
Given weights {3, 4, 5, 6, 7}, the Huffman algorithm is used to construct the optimal binary tree, and the weighted path length is (B).
A.25 B.57 C.62 D.61
Reference materials: "Data Structure (From Concept to C++ Implementation)" Tsinghua University Press, Wang Hongmei

Guess you like

Origin blog.csdn.net/oldmao_2001/article/details/109034518