当前位置:网站首页>【C语言】二叉树的实现及三种遍历
【C语言】二叉树的实现及三种遍历
2022-06-28 11:36:00 【贾璞】
实现二叉树并对其进行遍历操作
二叉树概念不做解释了,使用二级指针进行操作,比较方便。
使用时节点值须大于0,否则该节点置为NULL。
环境:Ubuntu18.04 GCC编译通过并使用
Code:
#include <stdio.h>
#include <stdlib.h>
//节点
struct BinTreeNode
{
int value;
struct BinTreeNode *left;
struct BinTreeNode *right;
};
//创建树
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;
}
//前序遍历
int PreorderTraversal(struct BinTreeNode *root)
{
if (!root)
return -1;
printf("%d,", root->value);
PreorderTraversal(root->left);
PreorderTraversal(root->right);
return 0;
}
//后序遍历
int PostorderTraversal(struct BinTreeNode *root)
{
if (!root)
return -1;
PostorderTraversal(root->left);
PostorderTraversal(root->right);
printf("%d,", root->value);
return 0;
}
//中序遍历
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:


边栏推荐
- Simulation of the Saier lottery to seek expectation
- Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed
- day30 js笔记 BOM和DOM 2021.09.24
- Apache2配置对目录拒绝访问,但是可以访问里面文件的设置
- js中this的默认指向及如何修改指向 2021.11.09
- Web3 security serials (3) | in depth disclosure of NFT fishing process and prevention techniques
- What is the main chain system?
- day34 js笔记 正则表达式 2021.09.29
- The development and principle of the metacosmic system
- [sciter]: use of sciter FS module scanning file API and its details
猜你喜欢

js中this的默认指向及如何修改指向 2021.11.09

行业分析| 快对讲,楼宇对讲

New listing of operation light 3.0 - a sincere work of self subversion across the times!

Software test interview classic + 1000 high-frequency real questions, and the hit rate of big companies is 80%

Unity screenshot function

day33 js笔记 事件(下)2021.09.28

多维度监控:智能监控的数据基础

Web3 security serials (3) | in depth disclosure of NFT fishing process and prevention techniques

Fruit FL studio/cubase/studio one music host software comparison

Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration
随机推荐
AcWing 604. Area of circle (implemented in C language)
NFT card chain game system development DAPP construction technical details
4. maximum continuity factor
day24 js笔记 2021.09.15
Bisection (integer bisection and floating point bisection)
使用ssm项目对Mysql8进行访问的时候,出现连接失败和一些错误的解决办法
赛尔号抽奖模拟求期望
day23 js笔记 2021.09.14
Industry analysis - quick intercom, building intercom
Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed
MapReduce项目案例1
Why do many people want to change careers as programmers, while some programmers want to change careers as others?
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
[no title] the virtual machine vmnet0 cannot be found and an error is reported: there is no un bridged host network adapter
For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?
纯纯大怨种!那些年被劝退的考研专业
MySQL cannot query the maximum value using the max function
Setinterval, setTimeout and requestanimationframe
买股票在中金证券经理的开户二维码上开户安全吗?求大神赐教
Array method in JS 2021.09.18