The number of nodes and the height of the binary tree are explained in detail (with illustrations)

The number and height of nodes in a binary tree



foreword

This article introduces the number and height of the nodes of the binary tree , and each question is accompanied by source code + diagram


NO.1 Define a chained binary tree

The code is as follows (example):

typedef char BTDataType;
typedef struct BinaryTreeNode
{
    
    
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;
	BTDataType data;
}BTNode;

NO.2 Create a binary tree

insert image description here

We want to implement a chained binary tree as shown in the figure. The code is implemented as follows ( link each node one by one )

The code is as follows (example):

BTNode* BuyNode(BTDataType x)
{
    
    
	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
	if (node == NULL)
	{
    
    
		printf("malloc fail\n");
		exit(-1);
	}
	node->data = x;
	node->left = node->right = NULL;
	return node;
}
BTNode* CreatBinaryTree()
{
    
    
	BTNode* nodeA = BuyNode('A');
	BTNode* nodeB = BuyNode('B');
	BTNode* nodeC = BuyNode('C');
	BTNode* nodeD = BuyNode('D');
	BTNode* nodeE = BuyNode('E');
	nodeA->left = nodeB;
	nodeA->right = nodeC;
	nodeB->left = nodeD;
	nodeC->left = nodeE;
	return nodeA;
}

1. Number of Binary Tree Nodes

In the binary tree we just created, the number of nodes is: 5 , the following is the code display + recursive diagram !

1. Code display

The code is as follows (example):

int BinaryTreeSize(BTNode* root)
{
    
    
	return root == NULL ? 0 :
		BinaryTreeSize(root->left)
		+ BinaryTreeSize(root->right)
		+ 1;
}

2. Recursive Diagram

insert image description here

insert image description here
Here this draws the recursive expansion diagram of the left node of A, and the recursive expansion diagram of the right node is similar to the one below. If you are interested, you can draw it yourself!


insert image description here


Second, the number of leaf nodes of the binary tree

Leaf node: A node with a degree of 0 is called a leaf node , such as the two nodes D and E in our double-shoulder binary tree!

1. Code display

The code is as follows (example):

// 二叉树叶子节点个数
int BinaryTreeLeafSize(BTNode* root)
{
    
    
	if (root == NULL)
	{
    
    
		return 0;
	}
	if (root->left == NULL && root->right == NULL)
	{
    
    
		return 1;
	}
	return BinaryTreeLeafSize(root->left) + 
		BinaryTreeLeafSize(root->right);
}

2. Recursive Diagram

insert image description here

insert image description here
Here this draws the recursive expansion diagram of the left node of A, and the recursive expansion diagram of the right node is similar to the one below. If you are interested, you can draw it yourself!


insert image description here


3. The number of nodes in the kth layer of the binary tree

1. Code display

The code is as follows (example):

// 二叉树叶子节点个数
int BinaryTreeLeafSize(BTNode* root)
{
    
    
	if (root == NULL)
	{
    
    
		return 0;
	}
	if (root->left == NULL && root->right == NULL)
	{
    
    
		return 1;
	}
	return BinaryTreeLeafSize(root->left) + 
		BinaryTreeLeafSize(root->right);
}

2. Recursive Diagram

insert image description here

insert image description here
Here this draws the recursive expansion diagram of the left node of A, and the recursive expansion diagram of the right node is similar to the one below. If you are interested, you can draw it yourself!


insert image description here


4. Binary tree height and depth

Tree height or depth: The maximum level of nodes in the tree . The height or depth of the binary tree we build is 4

1. Code display

The code is as follows (example):

// 二叉树深度/高度
int BinaryTreeDepth(BTNode* root)
{
    
    
	if (root == NULL)
	{
    
    
		return 0;
	}
	//return BinaryTreeDepth(root->left) > BinaryTreeDepth(root->right) 
	/*? BinaryTreeDepth(root->left) + 1 
		: BinaryTreeDepth(root->right) + 1;*/
	int leftDepth = BinaryTreeDepth(root->left);
	int rightDepth = BinaryTreeDepth(root->right);
	return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}

2. Recursive Diagram

insert image description here

insert image description here
Here this draws the recursive expansion diagram of the left node of A, and the recursive expansion diagram of the right node is similar to the one below. If you are interested, you can draw it yourself!


insert image description here


5. Find the node whose value is x in the binary tree

1. Code display

The code is as follows (example):

// 二叉树查找值为x的节点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
    
    
	if (root == NULL)
		return NULL;
	if (root->data == x)
		return root;
	BTNode* leftRet = BinaryTreeFind(root->left, x);
	if (leftRet)
		return leftRet;
	BTNode* rightRet = BinaryTreeFind(root->right, x);
	if (rightRet)
		return rightRet;
	return NULL;
	//return BinaryTreeFind(root->right, x);
}

2. Recursive Diagram

insert image description here
Here this draws the recursive expansion diagram of the left node of A, and the recursive expansion diagram of the right node is similar to the one below. If you are interested, you can draw it yourself!


insert image description here


Summarize

The above is what I will talk about today. This article introduces the number and height of the nodes of the binary tree and their respective recursive expansion diagrams!
If my blog is helpful to you, remember to support it three times, thank you for your support!
insert image description here

Guess you like

Origin blog.csdn.net/2201_75587702/article/details/129936508