当前位置:网站首页>Basic operation of chain binary tree (implemented in C language)
Basic operation of chain binary tree (implemented in C language)
2022-07-07 18:55:00 【Brant_ zero】
Catalog
One 、 Creation of chain binary tree
Two 、 Depth traversal of trees
1. Before the order 、 Middle preface 、 After the sequence traversal
1.2 Traversal sequence diagram of three ways
3、 ... and 、 Sequence traversal of trees
3.2 Identification of complete binary tree
Four 、 The basic functions of trees
4.1 The number of nodes in the tree
4.2 The number of leaf nodes in the tree
4.3 The first k Number of layer nodes
This blog is about the most common chain binary tree , The function of this structure in practical application is not great , But we still have to learn this structure . The purpose of learning this structure is to lay a foundation for us to study more complex tree structures in the future . I don't say much nonsense , Just start .
Catalog
One 、 Creation of chain binary tree
Two 、 Depth traversal of trees
2.1 Before the order 、 Middle preface 、 After the sequence traversal
2.1.1 Traversal sequence diagram of three ways
3、 ... and 、 The basic functions of trees
3.1 The number of nodes in the tree
3.2 The number of leaf nodes in the tree
3.3 The first k Number of layer nodes
Four 、 Sequence traversal of trees
4.2 Identification of complete binary tree
One 、 Creation of chain binary tree
First, let's create a tree , There are three steps to create a tree :① Define the structure of nodes in the tree ② Create a new node ③ Link nodes ; In this way, a tree can be built .
1.1 Define node structure
// Node creation
typedef int BTDataType;
typedef struct BinaryTreeNode
{
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
BTDataType data;
}BTNode;
1.2 Node creation
BTNode* BuyNode(BTDataType x)
{
BTNode* node = (BTNode*)malloc(sizeof(BTNode));
assert(node);
node->data = x;
node->left = NULL;
node->right = NULL;
return node;
}
1.3 Links to nodes
BTNode* CreatBinaryTree()
{
BTNode* node1 = BuyNode(1);
BTNode* node2 = BuyNode(2);
BTNode* node3 = BuyNode(3);
BTNode* node4 = BuyNode(4);
BTNode* node5 = BuyNode(5);
BTNode* node6 = BuyNode(6);
// link
node1->left = node2; // 1
node1->right = node4;// 2 4
node2->left = node3;// 3 # 5 6
node4->left = node5;// # # # # # #
node4->right = node6;
return node1;
}
Two 、 Depth traversal of trees
We have created the tree , Next, let's take a look at the links of our tree , At this point, we need to traverse the tree . But the structure of trees is special , Different from the array we learned before , Traversal is very convenient . Therefore, the following traversal methods are extended .
1. Before the order 、 Middle preface 、 After the sequence traversal
The access order of the three ways
Before the order ——( root --> The left subtree --> Right subtree )
Middle preface ——( The left subtree --> root --> Right subtree )
In the following order ——( The left subtree --> Right subtree --> root )
1.2 Traversal sequence diagram of three ways
2. Code implementation
This ergodic regularity is strong , We can easily write it using recursion , And divide each into NULL Print a # As a substitute for .
The former sequence traversal
// The former sequence traversal
void PreOrder(BTNode* root)
{
if (root == NULL)
{
printf("# ");
return;
}
printf("%d ", root->data);
PreOrder(root->left);
PreOrder(root->right);
}
In the sequence traversal
// In the sequence traversal
void InOrder(BTNode* root)
{
if (root == NULL)
{
printf("# ");
return;
}
InOrder(root->left);
printf("%d ", root->data);
InOrder(root->right);
}
After the sequence traversal
// After the sequence traversal
void PostOrder(BTNode* root)
{
if (root == NULL)
{
printf("# ");
return;
}
PostOrder(root->left);
PostOrder(root->right);
printf("%d ", root->data);
}
3. Traversal detection
3、 ... and 、 Sequence traversal of trees
After completing the above three depth traversals , Someone must want to ask , If I want to traverse layer by layer, how can I achieve it ?
The implementation of sequence traversal can only be realized with the help of queues , How to realize it? Let's look down .
3.1 Sequence traversal
thought :
① Put the root node in the queue .
② Pop up the header element and print , Then put the left node and the right node of the queue header element into the queue in turn
③ Repeat until the queue is empty , You can traverse the entire tree layer by layer .
because , If the left subtree or right subtree is empty , You won't put it in the queue , In this way, the front nodes will be out of the queue in turn according to the law of forward first out , It can achieve the purpose of our sequence traversal .
// Sequence traversal
// thought Put a root node Pop up a root node Connect and store the left node and the right node ,
// When the queue is empty, it will not be put
void LevelOrder (BTNode* root)
{
Queue q;
QueueInit(&q);
if (root)
{
QueuePush(&q, root);
}
// If the queue is not empty, continue the above operation
while (!QueueEmpty(&q))
{
// The right data
BTNode* front = QueueFront(&q);
printf("%d ", front->data);
QueuePop(&q);
if (front->left)
{
QueuePush(&q, front->left);
}
if (front->right)
{
QueuePush(&q, front->right);
}
}
QueueDestroy (&q);
}
3.2 Identification of complete binary tree
Perfect binary tree :n-1 Layer is full , The last layer of discontent , Full binary tree is a special kind of complete binary tree .
thought :
① According to the rules of sequence traversal , Set the root node 、 The left subtree 、 Put the right subtree into the queue
② If the header data is NULL, Then stop inserting into the queue .
③ Check the queue , If NULL There are non NULL node , Then it means that the tree is not a complete binary tree , Until the queue is empty ( That is, after the last node of the complete binary tree, it is all NULL node ).
Look at this first GIF chart , Then you can combine the code to see and understand .
// Judge whether the current tree is a complete binary tree
// If there is non empty after empty , It's not a complete binary tree
bool TreeComplete(BTNode* root)
{
Queue q;
QueueInit(&q);
if (root)
{
QueuePush(&q, root);
}
while (!QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);
QueuePop(&q);
if (front)
{
QueuePush(&q, front->left);
QueuePush(&q, front->right);
}
else
{
// If it is empty, it will jump out of sequence traversal
break;
}
//1. If there are all complete binary trees behind
//2. If there is non empty after empty , Is not a complete binary tree
while (!QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);
QueuePop(&q);
if (front)
{
return false;
}
}
}
QueueDestroy(&q);
return true;
}
Four 、 The basic functions of trees
It's not enough to traverse the tree , Let's implement more common functions .
4.1 The number of nodes in the tree
// Find the number of nodes
int TreeSize(BTNode* root)
{
return root == NULL ? 0 : TreeSize(root->left) + TreeSize(root->right)+1;
}
4.2 The number of leaf nodes in the tree
// Two ways to find the number of leaf nodes
int TreeLeafSize1(BTNode* root)
{
if (root == NULL)
return 0;
return ((root->left == NULL) && (root->right == NULL)) ? 1 :
TreeLeafSize1(root->left) + TreeLeafSize1(root->right) ;
}
4.3 The first k Number of layer nodes
// Please k Number of layer nodes
//1. Transform to find the left subtree k-1 layer , Right subtree k-1 layer
int KLeveSize(BTNode* root, int k)
{
if (root == NULL)
return 0;
if (k == 1)
{
return 1;
}
return KLeveSize(root->left, k - 1) + KLeveSize(root->right, k - 1);
}
4.4 Find the node of the tree
// Find the node of the tree
BTNode* TreeFind(BTNode* root, BTDataType x)
{
if (root == NULL)
return NULL;
if (root->data == x)
return root;
/*if (TreeFind(root->left, x) )
return TreeFind(root->left, x);
if (TreeFind(root->right, x))
return TreeFind(root->right, x);*/
// The above method will traverse deep twice Not recommended
BTNode* LeftNode = TreeFind(root->left, x);
if (LeftNode)
return LeftNode;
BTNode* RightNode = TreeFind(root->right, x);
if (RightNode)
return RightNode;
return NULL;
}
4.5 Depth of tree
// Find the depth of the binary tree
int TreeDepth(BTNode* root)
{
if (root == NULL)
return 0;
int left = TreeDepth(root->left);
int right = TreeDepth(root->right);
return left > right ? left+1 : right+1;
}
4.6 Destruction of trees
// Destruction of binary tree
// Post order traversal destruction
void TreeDestory(BTNode* root)
{
if (root == NULL)
return;
TreeDestory(root->left);
TreeDestory(root->right);
// Should not be in Check the use of
//free Print the release before
printf("\n%p : %d ", root,root->data);
free(root);
}
Compared with the previous stack and queue, the binary tree is more difficult , Be familiar with recursion , You can slowly digest it in combination with drawing . The code of recursive classes is not easy to debug , You can observe by printing in combination with depth traversal , Or read the code on the computer drawing board to understand .
This blog is over , Binary tree is still very difficult , Next, I'll do some exercises and start learning pile , I hope you will continue to pay attention .
边栏推荐
- Redis集群与扩展
- Industry case | digital operation base helps the transformation of life insurance industry
- 小程序中实现付款功能
- Will domestic software testing be biased
- Comparison and selection of kubernetes Devops CD Tools
- How to clean when win11 C disk is full? Win11 method of cleaning C disk
- SQLite SQL exception near "with": syntax error
- Thread pool and singleton mode and file operation
- Reinforcement learning - learning notes 8 | Q-learning
- 直播预约通道开启!解锁音视频应用快速上线的秘诀
猜你喜欢
【Unity Shader】插入Pass实现模型遮挡X光透视效果
磁盘存储链式的B树与B+树
[PaddleSeg源码阅读] PaddleSeg Validation 中添加 Boundary IoU的计算(1)——val.py文件细节提示
[unity shader] insert pass to realize the X-ray perspective effect of model occlusion
你真的理解粘包与半包吗?3分钟搞懂它
Antisamy: a solution against XSS attack tutorial
Tear the Nacos source code by hand (tear the client source code first)
Differences between rip and OSPF and configuration commands
Backup Alibaba cloud instance OSS browser
单臂路由和三层交换的简单配置
随机推荐
直播预约通道开启!解锁音视频应用快速上线的秘诀
2022年理财产品的一般收益率是多少?
财富证券证券怎么开户?通过链接办理股票开户安全吗
[paper sharing] where's crypto?
debian10系统问题总结
Idea completely uninstalls installation and configuration notes
unity2d的Rigidbody2D的MovePosition函数移动时人物或屏幕抖动问题解决
Thread pool and singleton mode and file operation
Differences between rip and OSPF and configuration commands
nest.js入门之 database
DataSimba推出微信小程序,DataNuza接受全场景考验? | StartDT Hackathon
Kirk Borne的本周学习资源精选【点击标题直接下载】
【Unity Shader】插入Pass实现模型遮挡X光透视效果
Recommend free online SMS receiving platform in 2022 (domestic and foreign)
C语言中匿名的最高境界
Learn to make dynamic line chart in 3 minutes!
More than 10000 units were offline within ten days of listing, and the strength of Auchan Z6 products was highly praised
【塔望方法论】塔望3W消费战略 - U&A研究法
Redis cluster and expansion
能同时做三个分割任务的模型,性能和效率优于MaskFormer!Meta&UIUC提出通用分割模型,性能优于任务特定模型!开源!...