Data structure: complete binary tree appetizer small exercise

Table of contents

I. Introduction

2. Important structural features of complete binary tree

3. Complete binary tree appetizer exercise

1. An important mathematical conclusion

2. Simple exercises


I. Introduction

For the basic concepts of trees and complete binary trees (and tree node numbering rules), see : http://t.csdn.cn/imdra icon-default.png?t=N176http://t.csdn.cn/imdra

A complete binary tree is a very important data structure:

The node numbers of the complete binary tree of n nodes are arranged continuously from 0 to (n-1) ( assuming that the root node number is 0 ), so the memory utilization efficiency when mapping the complete binary tree to the linear storage structure in memory Very high ( the array subscript and the tree node number establish an absolute mapping relationship ).

The most classic complete binary tree linear storage structure is the size root heap (the data structure basis of heap sorting)

2. Important structural features of complete binary tree

  • Assuming that the total number of nodes is n and the height of the complete binary tree T is k , in order to meet the structural definition that the numbers of each node are arranged continuously from 0 to (n-1) , the complete binary tree consists of all nodes in the 1~(k-1) layer The substructure is a full binary tree (meaning that all leaf nodes of the complete binary tree are distributed in the last layer of the tree (kth layer)):
  • Assuming that the total number of nodes is n and the height of a complete binary tree T is k , in order to satisfy the structural definition that each node number is 0~(n-1) consecutively arranged , the leaf nodes of the kth layer (the last layer) of the complete binary tree must be It is arranged consecutively (that is, a complete binary tree with an odd number of nodes does not have a branch node with an out-degree of 1 , and a complete binary tree with an even number of nodes has and only one branch node with an out-degree of 1 point )

3. Complete binary tree appetizer exercise

1. An important mathematical conclusion

  • For any binary tree, if the out-degree is 0, the number of leaf nodes is N0 , and  the number of branch nodes with out-degree 2 is N2 (including the root), then N0=N2+1

For the specific proof of this conclusion, please refer to Xiaoqingcai's blog: http://t.csdn.cn/imdra icon-default.png?t=N176http://t.csdn.cn/imdra

2. Simple exercises

  • Given a complete binary tree T with 2n nodes , find the number of leaf nodes

Solving:

Let the number of nodes whose out-degree T is 0 be N0 ( that is, the number of leaf nodes ) ,

The number of nodes with out degree 1 is N1 ,

The number of nodes with out degree 2 is N2.

According to the structural analysis in the second chapter of this article, N1 is either 1 or 0

  1. If N1 = 0 , according to the relationship N0 = N2 + 1 , it can be obtained: 2n = N0 + N1 + N2 , and N0 = (2n+1)/2 can be obtained by simplification . N0 is not an integer, so this situation is excluded
  2. If N1 = 1 , according to the relationship N0 = N2 + 1 , we can get: 2n = N0 + N1 + N2 , and we can get N0 = n by simplification , which satisfies the meaning of the question

Therefore, the number of T leaf nodes is n

  • The total number of nodes in a complete binary tree is 531, find the height of this tree

Solve:

Let the height of the tree be k

According to the structural analysis in the second chapter of this article, it can be seen that the first k-1 layers of the tree constitute a full tree ( according to the summation formula of the geometric series, the number of summary points of the full tree is 2^(k-1)-1 )

And 2^9 = 512<531<2^10 = 1024 , so k-1 = 9, the height of the binary tree can be found to be 10

  • Given a complete binary tree T with 767 nodes , find the number of leaf nodes

Solving:

Let the number of nodes whose out-degree T is 0 be N0 ( that is, the number of leaf nodes ) ,

The number of nodes with out degree 1 is N1 ,

The number of nodes with out degree 2 is N2.

According to the structural analysis in the second chapter of this article, N1 is either 1 or 0

  1. If N1 = 0 , according to the relationship N0 = N2 + 1 , it can be obtained: 767 = N0 + N1 + N2 , and N0 = 384 can be obtained by simplification , which satisfies the meaning of the question
  2. If N1 = 1 , according to the relationship N0 = N2 + 1 , it can be obtained: 767 = N0 + N1 + N2 , and N0 = 767/2 can be obtained by simplification . N0 is not an integer, so this situation is excluded

Therefore, the number of T leaf nodes is 384

 

 

 

Supongo que te gusta

Origin blog.csdn.net/weixin_73470348/article/details/129213247
Recomendado
Clasificación