Suppose a complete binary tree has 1000 nodes, then this complete binary tree has () leaf nodes and () nodes with degree 2.

1. Problem

Suppose a complete binary tree has 1000 nodes, then this complete binary tree has (500) leaf nodes, and there are (500) a>) nodes with degree 2. 499

Analysis:
1. Leaf node: a node with degree 0.
2. Let n0 represent the node with degree 0, and n2 represent the node with degree 2 .
3. According to the properties of the binary tree, n0 = n2 +1.

Therefore, we only need to find n0 to find the number of nodes of n2. The implementation of the idea is as follows:

2. Implementation of ideas

  • The first step is to find the number of n0.
    A complete binary tree with 1000 nodes has 10 levels ( 2 9 2^9 29 - 1 < 1000 < 2 10 2^{10} 210-1), of which the first 9 levels of are full binary trees with a total of 512-1=511 nodes.

    Therefore, there are 1000-511=489, indicating thatthere are 489 nodes in the 10th layer, and the nodes in the 10th layer are all Leaf nodes (nodes with degree 0).

    And 489/2 = 244...1, it means that there are 244+1 (245) nodes with child nodes in the 9th level, and according to the full binary tree, there are in the 9th level 2 8 2^8 28 = 256 nodes, then the number of nodes (leaf nodes) with degree 0 in the ninth layer is 256-245 = 11.

    n0 = the number of leaf nodes in the 9th layer + the number of leaf nodes in the 10th layer = 489+11 = 500, that is, the number of leaf nodes is 500.

  • The second step is to find n0 = n2 +1 and solve for the number of n2.
    From the number of leaf nodes obtained in the first step, the number of nodes with degree 2 in the binary tree can be obtained:
    n2=n0-1=500-1 =499
    That is: there are 499 nodes with degree 2.

Problem recording time: 2023.11.5

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/134237133