当前位置:网站首页>[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
- Le patron a donné trois ordres: discret, discret, discret
- Youju new material rushes to Shenzhen Stock Exchange: it plans to raise 650million yuan, with an annual revenue of 333million yuan
- Send2vec tutorial
- 基于 Nebula Graph 构建百亿关系知识图谱实践
- 叮!Techo Day 腾讯技术开放日如约而至!
- How does Seata server 1.5.0 support mysql8.0?
- Vscode writes markdown file and generates pdf
- Validate palindrome string
- 解决Unable to create process using ‘D:\Program File
猜你喜欢

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

Q-tester 3.2: applicable to development, production and after-sales diagnostic test software

PMP真的有用吗?

Ding! Techo day Tencent technology open day arrived as scheduled!

3. Caller 服务调用 - dapr

运行近20年,基于Win 98的火星探测器软件迎来首次升级

竞远安全冲刺创业板:拟募资4亿 启元投资与云游软件是股东

dolphinscheduler2. Installation of X (valid for personal test)

openGauss内核:SQL解析过程分析

基于 Nebula Graph 构建百亿关系知识图谱实践
随机推荐
Recommendation letter brain correspondent: if love is just a chemical reaction, can you still believe in love?
推荐四款可视化工具,解决 99% 的可视化大屏项目!
动力电池,是这样被“瓜分”的
Summary of technical difficulties of wearable neural signal and behavior data detection and recording system for birds in flight
Calculator (force buckle)
2022 recurrent training question bank and online simulation examination for safety inspection of metal and nonmetal mines (underground mines)
Le patron a donné trois ordres: discret, discret, discret
Maingene listed on the Hong Kong Stock Exchange: IPO with a market value of HK $4.3 billion was ignored by the market
vector详解+题目
机器人的运动范围(DFS)
Who is the main body of the waiting insurance record? Record in the local network security, right?
JS judge whether the string is empty or not
计算器(力扣)
Technical trendsetter
Ding! Techo day Tencent technology open day arrived as scheduled!
dolphinscheduler2.X的安装(亲测有效)
QQ被盗号后群发黄图,大批用户“社死”
单一职责原则
PMP真的有用吗?
智联招聘基于 Nebula Graph 的推荐实践分享