当前位置:网站首页>【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:


边栏推荐
- Why do many people want to change careers as programmers, while some programmers want to change careers as others?
- On the output representation of bidirectional LSTM in pytoch
- Remote login sshd service
- 水果FL Studio/Cubase/Studio one音乐宿主软件对比
- Day33 JS note event (Part 2) September 28, 2021
- 买股票在中金证券经理的开户二维码上开户安全吗?求大神赐教
- Simulation of the Saier lottery to seek expectation
- day36 js笔记 ECMA6语法 2021.10.09
- Self use demo of basic component integration of fluent
- day37 js笔记 运动函数 2021.10.11
猜你喜欢

Jetpack Compose Desktop 桌面版本的打包和发布应用

Packaging and publishing application of jetpack compose desktop version

2022中国信通院首届业务与应用安全发展论坛成功召开!

day33 js笔记 事件(下)2021.09.28

Array method in JS 2021.09.18

Ali three sides: what is the difference between using on or where in the left join associated table and the condition

day31 js笔记 DOM下 2021.09.26

Day36 JS notes ecma6 syntax 2021.10.09

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

Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration
随机推荐
On the output representation of bidirectional LSTM in pytoch
Apache2 configuration denies access to the directory, but can access the settings of the files inside
Day33 JS note event (Part 2) September 28, 2021
Bisection (integer bisection and floating point bisection)
Prefix and (one dimension)
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
Prefix and (2D)
Remote login sshd service
Packaging and publishing application of jetpack compose desktop version
FTP protocol for Wireshark packet capture analysis
fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
Redis 原理 - List
[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
Recommended practice sharing of Zhilian recruitment based on Nebula graph
【无标题】虚拟机vmnet0找不到且报错:没有未桥接的主机网络适配器
Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration
Oracle date format exception: invalid number
Contract quantification system development (construction explanation) - contract quantification system development (source code analysis and ready-made cases)
Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method
Graduated