当前位置:网站首页>[C language] implementation of binary tree and three Traversals
[C language] implementation of binary tree and three Traversals
2022-06-28 14:49:00 【Jia Pu】
Implement the binary tree and traverse it
The concept of binary tree will not be explained , Use the secondary pointer to operate , It's more convenient .
The node value must be greater than 0, Otherwise, the node is set to NULL.
Environmental Science :Ubuntu18.04 GCC Compile and use
Code:
#include <stdio.h>
#include <stdlib.h>
// node
struct BinTreeNode
{
int value;
struct BinTreeNode *left;
struct BinTreeNode *right;
};
// Create trees
int CreatBinTree(struct BinTreeNode **root)
{
int val;
scanf("%d", &val);
if (val <= 0)
{
*root = NULL;
return 0;
}
*root = (struct BinTreeNode*)malloc(sizeof(struct BinTreeNode));
if (!(*root))
{
perror("Failed to creat:");
return -1;
}
else
{
(*root)->value = val;
printf("%d Left sibling:", val);
CreatBinTree(&((*root)->left));
printf("%d Right sibling:", val);
CreatBinTree(&((*root)->right));
}
return 0;
}
// The former sequence traversal
int PreorderTraversal(struct BinTreeNode *root)
{
if (!root)
return -1;
printf("%d,", root->value);
PreorderTraversal(root->left);
PreorderTraversal(root->right);
return 0;
}
// After the sequence traversal
int PostorderTraversal(struct BinTreeNode *root)
{
if (!root)
return -1;
PostorderTraversal(root->left);
PostorderTraversal(root->right);
printf("%d,", root->value);
return 0;
}
// In the sequence traversal
int InorderTraversal(struct BinTreeNode *root)
{
if (!root)
return -1;
InorderTraversal(root->left);
printf("%d,", root->value);
InorderTraversal(root->right);
return 0;
}
int main(int argc, char const *argv[])
{
struct BinTreeNode *root = (struct BinTreeNode*)malloc(sizeof(struct BinTreeNode*));
CreatBinTree(&root);
printf("Preorder Traversal:\n");
PreorderTraversal(root);
printf("\n");
printf("Inorder Traversal:\n");
InorderTraversal(root);
printf("\n");
printf("Postorder Traversal:\n");
PostorderTraversal(root);
printf("\n");
return 0;
}
Picture:


边栏推荐
- 2022 Chinese cook (Advanced) test questions and online simulation test
- Differences between ram ROM flash
- Who is the main body of the waiting insurance record? Record in the local network security, right?
- 基于 Nebula Graph 构建百亿关系知识图谱实践
- How to solve the following problems in the Seata database?
- seata-server 1.5.0 如何支持mysql8.0?
- 从小小线虫谈起——溯源神经系统进化,开启生命模拟
- Is PMP really useful?
- 2022金属非金属矿山(地下矿山)主要负责人考试模拟100题模拟考试平台操作
- Does Frankfurt currently support SQL?
猜你喜欢

dolphinscheduler2.X的安装(亲测有效)

3. caller service call - dapr

Introduction to common components of IOT low code platform

Mingchuangyou products passed the listing hearing: seeking dual main listing with an annual revenue of 9.1 billion

Recommended practice sharing of Zhilian recruitment based on Nebula graph

After nearly 20 years of operation, the Mars probe software based on win 98 has been upgraded for the first time

【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境

Le patron a donné trois ordres: discret, discret, discret

Recommended practice sharing of Zhilian recruitment based on Nebula graph

Youju new material rushes to Shenzhen Stock Exchange: it plans to raise 650million yuan, with an annual revenue of 333million yuan
随机推荐
Foreign trade SEO Webmaster Tools
Do not use short circuit logic to write STL sorter multi condition comparison
【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
What does VPS do? What are the famous brands? What is the difference with ECS?
Is PMP really useful?
2022 welder (technician) examination question bank simulated examination platform operation
代码片段
张同学还没学会当主播
Vector explanation + topic
2022中式烹調師(高級)試題及在線模擬考試
Seata数据库中出现以下问题要怎么解决呀?
法兰克福地区目前支持sql了吗?
How does Seata server 1.5.0 support mysql8.0?
2022年最新PyCharm激活破解码永久_详细安装教程(适用多版本)
Quantum frontier hero spectrum - "light quantum Explorer" McMahon: turning any physical system into a neural network
Opening and closing principle
Recommended practice sharing of Zhilian recruitment based on Nebula graph
[MySQL learning notes 24] index design principles
坐拥1200亿,她又要IPO敲钟了
机器人的运动范围(DFS)