[Data structure]——Binary tree function

Preface: We have already understood some concepts of binary trees before, so today we will learn about the implementation of binary tree traversal and some properties.

Insert image description here

There are three ways to traverse a binary tree: Preorder, Inorder,post sequence.

Preorder: root node first, then left subtree, and finally right subtree.
In-order: left subtree first, then root node, and finally right subtree.
Postorder: first the left subtree, then the right subtree, and finally the root node.

Preorder traversal:

void PrevOrder(TreeNode* root)
{
    
    
	if (root == NULL)
	{
    
    
		printf("N ");
		return;
	}

	printf("%d ", root->data);
	PrevOrder(root->left);
	PrevOrder(root->right);
}

If our root node is empty, it returns empty. If it is not empty, it recursively accesses the left subtree. If the left subtree is empty, it returns recursively accessing the right subtree.

In-order traversal:

void InOrder(TreeNode* root)
{
    
    
	if (root == NULL)
	{
    
    
		printf("N");
		return;
	}

	InOrder(root->left);
	printf("%d ", root->data);
	InOrder(root->right);
}

The left subtree is visited first, then the root node, and finally the right subtree is visited.

Postorder traversal:

void Tailorder(TreeNode* root)
{
    
    
	if (root == NULL)
	{
    
    
		printf("N");
		return;
	}
	Tailorder(root->left);
	Tailorder(root->right);
	printf("%d", root->data);
}

First traverse the left subtree, then the right subtree, and finally the root node.

Find the number of nodes in a binary tree:

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

We implement it recursively. The number of nodes in the left subtree plus the number of nodes in the right subtree plus the number of root nodes is the total number of nodes.
Insert image description here

Find the number of leaf nodes:

int TreeLeafSize(TreeNode* root)
{
    
    
	// 空 返回0
	if (root == NULL)
		return 0;
	// 不是空,是叶子 返回1
	if (root->left == NULL
		&& root->right == NULL)
		return 1;

	// 不是空 也不是叶子  分治=左右子树叶子之和
	return TreeLeafSize(root->left) +
		TreeLeafSize(root->right);
}

Find the height of a binary tree:

int TreeHeight(TreeNode* root)
{
    
    
	if (root == NULL)
		return 0;
	int leftHeight = TreeHeight(root->left);
	int rightHeight = TreeHeight(root->right);

	return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}

Because our recursion combined with the ternary operator will make it very complicated, so we use a data to save the height of the left and right subtrees. The height of our binary tree is the higher subtree of the left and right subtrees plus 1, so we What is returned is the height of the left and right subtrees plus 1, which is the height of the binary tree.

Our code can also be improved. Our fmax function in C language:The function of this function is to compare two numbers to get the larger number

int TreeHeight(TreeNode* root)
{
    
    
	if (root == NULL)
		return 0;

	return fmax(TreeHeight(root->left), TreeHeight(root->right)) + 1;
}

Find the node with value x in a binary tree:

// 二叉树查找值为x的结点
TreeNode* TreeFind(TreeNode* root, BTDataType x)
{
    
    
	if (root == NULL)
		return NULL;

	if (root->data == x)
		return root;

	TreeNode* ret1 = TreeFind(root->left, x);
	if (ret1)
		return ret1;

	TreeNode* ret2 = TreeFind(root->right, x);
	if (ret2)
		return ret2;

	return NULL;
}

Destroy the binary tree:

void DestroyTree(TreeNode* root)
{
    
    
	if (root == NULL)
		return;

	DestroyTree(root->left);
	DestroyTree(root->right);
	free(root);
}

Find the number of nodes in the kth level of a binary tree:

int TreeLevelK(TreeNode* root, int k)
{
    
    
	assert(k > 0);
	if (root == NULL)
		return 0;

	if (k == 1)
		return 1;

	return TreeLevelK(root->left, k - 1)
		+ TreeLevelK(root->right, k - 1);
}

The nodes on the kth layer are equal to the sum of the number of nodes on the k-1th layer.
Insert image description here
Now we require the number of nodes in the third layer, which is equivalent to us returning its second layer, and the number of our second layer nodes is to return the number of our first layer nodes, our left The subtree returns one node, and the right subtree returns two nodes, so there are three nodes.

If it helps anyone, please support it!

Guess you like

Origin blog.csdn.net/Lehjy/article/details/134818483