[Data structure] Detailed explanation of binary tree (3)

⭐️ Preface

✨Previous links: [Data structure] Detailed explanation of binary tree (1)
In the first article on binary tree, we discussed the definition and implementation of the chain structure of binary tree. Binary traversal includes ( preorder/inorder/postorder traversal ) and detailed explanation of code implementation and recursive flow chart. There are also some other interface definitions and implementations of binary trees, including BinaryTreeSize( calculate the number of binary tree nodes ), BinaryTreeLeafSize( calculate the number of binary tree leaf nodes ), BinaryTreeKLevelSize( calculate the kkth binary treeThe number of k- level nodes),BinaryTreeFind(find an element in the binary tree).
insert image description here


insert image description here


✨Previous link: [Data Structure] Detailed Explanation of Binary Tree (2)
In the second article on binary tree, an interface implementation is added on the basis of the first article BinaryTreeDepth( calculating the depth of binary tree )
insert image description here


In this issue, on the basis of the previous two articles, we will continue to add some other interface implementations about binary trees.

⭐️ Other interfaces of binary tree

// 二叉树的销毁
void BinaryTreeDestroy(BinaryTreeNode* root);
// 二叉树层序遍历(需要队列)
void LevelOrder(BinaryTreeNode* root);
// 判断二叉树是否是完全二叉树
bool BinaryTreeComplete(BinaryTreeNode* root);

BinaryTreeDestroyaccomplish:

// 二叉树的销毁
// 后序遍历最优
void BinaryTreeDestroy(BinaryTreeNode* root) {
    
    
	if (root == NULL) {
    
    
		return;
	}

	BinaryTreeDestroy(root->left);
	BinaryTreeDestroy(root->right);

	free(root);
}

解析:The destruction of the binary tree is optimal by post-order traversal . The left subtree is destroyed first, the right subtree is destroyed, and the root node is finally destroyed.


LevelOrderaccomplish:

// 二叉树层序遍历(需要队列)
void LevelOrder(BinaryTreeNode* root) {
    
    
	Queue q;
	QueueInit(&q);

	// 把根节点入队列
	if (root) {
    
    
		QueuePush(&q , root);
	}

	// 依次遍历队列
	while (!QueueIsEmpty(&q)) {
    
    
		// 取出队头节点
		QueueDataType front = QueueFront(&q);
		// 出队头节点
		QueuePop(&q);
		// 打印
		printf("%d " , front->value);
		// 左节点不为空 入队列
		if (front->left) {
    
    
			QueuePush(&q , front->left);
		}
		// 右节点不为空 入队列
		if (front->right) {
    
    
			QueuePush(&q , front->right);
		}
	}

	QueueDestroy(&q);
}

解析:Hierarchical traversal needs to be realized by queues. The idea is to put the root node into the queue and loop to judge that the queue is not empty. If the queue is not empty, take out the current queue head node and enqueue the left and right children of the queue head node in turn, and then delete the queue head data. The code and implementation of the queue: ✨Previous articles: [Data Structure] Stack and Queue Implementation

insert image description here


BinaryTreeCompleteaccomplish:

// 判断二叉树是否是完全二叉树
// 用到层序遍历的思想
bool BinaryTreeComplete(BinaryTreeNode* root) {
    
    
	Queue q;
	QueueInit(&q);
	// 根节点不为空入队列
	if (root) {
    
    
		QueuePush(&q , root);
	}
	// 检查队列
	while (!QueueIsEmpty(&q)) {
    
    
		// 取出队头数据
		QueueDataType front = QueueFront(&q);
		// 删除队头数据
		QueuePop(&q);
		// 如果当前对头节点存在则让当前节点左右节点入队
		if (front) {
    
    
			QueuePush(&q , front->left);
			QueuePush(&q, front->right);
		}
		else {
    
    
			break;
		}
	}
	// 继续检查队列
	// 如果全为空 则是完全二叉树
	// 如果有一个不为空 则不是完全二叉树
	while (!QueueIsEmpty(&q)) {
    
    
		QueueDataType front = QueueFront(&q);
		QueuePop(&q);
		if (front) {
    
    
			QueueDestroy(&q);
			return false;
		}
	}

	QueueDestroy(&q);
	return true;
}

解析:To judge whether the current binary tree is a complete binary tree, the idea of ​​layer order traversal is used . Hierarchical traversal is to put the root node into the queue, and then check whether the queue is empty in turn. If it is not empty, take out the data at the head of the queue, and then judge whether the child exists. When the current root node is dequeued, then put the child node into the queue until the queue is empty. A complete binary tree satisfies k − 1 k - 1kThe first layer of nodes is full, and the last layer of nodes is at least one, and at most it is full (this situation is also called a full binary tree). Of course, the last layer nodes of the complete binary tree need to be continuous.
insert image description here


It can be seen from the above figure: If it is not a complete binary tree, if there are non-empty nodes behind the queue when the first one is encountered, then it is not a complete binary tree. NULLBecause the level order traversal of a complete binary tree is to make the existing nodes all on the left and NULLall the nodes on the right . So we put the empty nodes into the queue sequentially on the basis of layer order traversal . When the first NULLnode is encountered , the loop ends, and then check in turn whether there are any nodes that are not in the remaining nodes of the queue . NULLIf they are all , then the current NULLbinary NULLtree is a complete binary tree.

Guess you like

Origin blog.csdn.net/cccyi7/article/details/131838394