Suppose F is a forest and B is a binary tree transformed from F. If there are n non-terminal nodes in F, then there are () nodes in B whose right pointer field is empty

1. Problem

SupposeF is a forest, B is composed of FThe binary tree transformed. If there are n non-terminal nodes in F, then the node whose right pointer field is empty in B< /span>There are ( C )

  • A.n-1
  • B.n
  • C.n+1
  • D.n+2

Analysis:
Concept rules that need to be understood when solving this problem:
1️⃣Concept
Terminal node: A node with degree 0. (leaf node)
Non-terminal node: node with degree > 0. (Non-leaf node)
2️⃣Rule
Forest and binary tree conversion rules: left child right brother.
The basic idea of ​​this rule is: for each node in the tree, only keep the connection between it and the first child node, and regard other child nodes as the first child. The node's sibling nodes are connected with the right pointer. In this way, each node has only two pointers: the left pointer points to the first child node, and the right pointer points to the next sibling node. This representation can keep the hierarchical structure of the tree or forest unchanged, while taking advantage of the properties of binary trees for operations.

So based on these, we can first formulate an idea, which is to first find the total number of null pointer fields, subtract the number of empty left pointer fields, and finally get the number of nodes with empty right pointer fields.

2. Implementation of ideas

  • The first step is to find the total number of null pointer fields (the total number of null pointer fields = the total number of pointer fields - the number of occupied pointer fields)
    is known, Forest F, binary tree B, F has n non-terminal nodes.

    Assume that we can assume that the number of terminal nodes of the forest is m, then the summary number of points of the forest is m+n.
    Each node of binary tree B has two pointers (binary linked list storage), so the total number of pointers is 2(m+n);

    According to the method ofleft child and right brother, except for the root node, all other nodes in the binary tree are children of a certain node, that is, they must There will be a pointer pointing to it, then the number of pointer fields that have been used at this time ism+n-1 (-1 is the part with the root node removed ).

    ThereforeThe total number of null pointer fields = 2(m+n) - (m+n-1) = m+n+1 ①

  • The second step is to find the number of empty left pointer fields.

    Since the m terminal nodes have no child nodes, according to the left child and right brother, after the forest F is converted into a binary tree B, the left pointers of these nodes must be empty (because the left pointer field points to It is the first child, it has no child node, so it is empty), so the number of empty left pointer fields is m ②.

  • The third step is to find the number of nodes in B whose right pointer field is empty according to the relationship between the two. (the nodes whose right pointer field is empty
    Combining ①②, we can get:
    The number of nodes with empty right pointer field = ① - ② = (m+n+1) - m = n+1.< a i=4>So choose C for this question. The number of nodes with empty right pointer field is n+1.

Problem recording time: 2023.10.24

Code_Lissel (CSDN) (A Coder who likes ancient poetry and programming)
Like and follow, collect and not get lost! If this article is helpful to you, please like it and support it!

Guess you like

Origin blog.csdn.net/qq_51646682/article/details/134006723