[Data structure] Implementation of chained binary tree (below) (c language)

prologue

We have already mentioned the concept of binary tree, the concept of binary tree can jump to the concept of binary tree and the implementation of sequential storage

Create a simple binary tree

To create a simple binary tree, first we need to define a structure, data represents the data field, left represents the left child node, and right represents the right child node

 Define an interface for creating nodes

 Then you can create a simple binary tree

 The above steps are not the real steps to create a binary tree, but just to lay the foundation for us to learn the basic operations of the binary tree, so that we can check whether the subsequent interface is accurate

 The above picture is a schematic diagram of the binary tree we created

Related interfaces of binary tree

First, the traversal of the binary tree

Binary tree traversal is divided into pre -order traversal , in-order traversal , post-order traversal and layer order

Now we mainly talk about the first three traversals. After the basic interface of the binary tree is almost implemented, we will talk about the layer order traversal of the binary tree.

1.1 Preorder traversal of binary tree

Preorder traversal: first visit the root node, then visit the left subtree, and finally visit the right subtree

 1.2 Inorder traversal of a binary tree

In-order traversal: first visit the left subtree of the binary tree, then visit the root, and finally visit the right subtree

 1.3 Postorder traversal of binary tree

Post-order traversal: first visit the left subtree, then visit the right subtree, and finally visit the root

 Now let's check that the implemented interface is correct

 From the above figure, we can find that there is no problem with the currently implemented interface, then we can continue to implement the following interface

1.4 Number of Binary Tree Nodes

You can use the recursive method. This method uploads the data from the back to the front one time, and adds itself

 1.5 The number of leaf nodes of the binary tree

 1.6 Depth of a binary tree

Using the divide and conquer algorithm, return from bottom to top, idea: when the height of the left node is greater than the height of the right node, add one to the height of the left node and return, otherwise add one to the height of the right node and return

 1.7 The number of K layer nodes

When the number of layers in the binary tree is less than k, it means that there is no node in the kth layer, and it returns zero. When it recurses to the kth layer, it returns 1. If it does not reach the kth layer, it continues to recurse until the kth layer is known.

 1.8 Find the node whose value is x in the binary tree

return if found

 1.9 Destruction of binary tree

The purpose of passing the secondary pointer is to empty the outer pointer in the function

 

 2. Construct a binary tree by means of preorder

 

 Through the inspection in the figure below, it is found that the code is ok

 2.2 Level order traversal of binary tree

Assuming that the root node of the binary tree is at 1 layer, the layer order traversal is to start from the root node of the binary tree where it is located, first visit the root node of the first layer, then visit the nodes on the second layer from left to right, and then the third The nodes of the layer, and so on, the process of visiting the nodes of the tree layer by layer from top to bottom and from left to right is layer order traversal.

Layer order traversal idea: first store the head node of the binary tree into the queue, then remove the top element of the queue, insert the left and right child nodes of the top element into the queue at the same time, iterate in turn

 The result in the figure below

 2.3 Determine whether the binary tree is a complete binary tree

By understanding the concept of a complete binary tree, we can know that the nodes of a complete binary tree are all left-aligned, so the method of layer order can be used

Put the data in and out of the queue one by one. When the top element of the queue is empty, stop entering the data, and then check the data in the queue to see if there is any data that is not empty. If there is a data that is not empty, then the binary tree is not complete binary tree, otherwise complete binary tree

 That's all for today's sharing, if there are any shortcomings, please point out, thank you for your support!

Guess you like

Origin blog.csdn.net/m0_72532428/article/details/129955767