当前位置:网站首页>Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)
Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)
2022-07-03 08:50:00 【Cap07】
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef char DataType;
typedef struct BiTNode {
DataType data;
struct BiTNode* lchild, * rchild;
}BiTNode, * BiTree;
BiTree CreateBiTree() { // Create a binary tree ( Pre order creation )
BiTree T;
char ch;
scanf("%c", &ch);
if (ch == '#') T = NULL; // With ‘#’ Indicates that the location is empty
else {
T = (BiTree)malloc(sizeof(BiTNode));
T->data = ch;
T->lchild = CreateBiTree();
T->rchild = CreateBiTree();
}
return T; // Return root node
}
void PreOrderTraverse(BiTree T) // Traversing a binary tree in order
{
if (!T) return;
printf("%c", T->data);
PreOrderTraverse(T->lchild); // Recursively traverses the left subtree
PreOrderTraverse(T->rchild); // Recursively traverses the right subtree
}
void InOrderTraverse(BiTree T) // Middle order ergodic binary tree
{
if (!T) return;
InOrderTraverse(T->lchild);
printf("%c", T->data);
InOrderTraverse(T->rchild);
}
void RearOrderTraverse(BiTree T) // Post order traversal binary tree
{
if (!T) return;
RearOrderTraverse(T->lchild);
RearOrderTraverse(T->rchild);
printf("%c", T->data);
}
int main()
{
BiTree T;
printf(" Please input the binary tree in the first order , For vacant positions “#” Instead of :\n");
T = CreateBiTree();
PreOrderTraverse(T);
printf("\n");
InOrderTraverse(T);
printf("\n");
RearOrderTraverse(T);
return 0;
}
test result :
边栏推荐
- Unity notes 1
- Monotonic stack -42 Connect rainwater
- Dealing with duplicate data in Excel with xlwings
- Find the intersection of line segments
- 单调栈-503. 下一个更大元素 II
- TP5 order multi condition sort
- Pit & ADB wireless debugging of vivo real machine debugging
- Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
- Deep parsing JVM memory model
- URL backup 1
猜你喜欢
Visual Studio (VS) shortcut keys
Concurrent programming (III) detailed explanation of synchronized keyword
[concurrent programming] explicit lock and AQS
Markdown learning
Animation_ IK overview
了解小程序的笔记 2022/7/3
UE4 source code reading_ Bone model and animation system_ Animation node
VIM learning notes from introduction to silk skating
Monotonic stack -84 The largest rectangle in the histogram
TP5 multi condition sorting
随机推荐
22-05-26 西安 面试题(01)准备
Unity editor expansion - the framework and context of unity imgui
【Rust 笔记】12-闭包
单调栈-42. 接雨水
Osgearth target selection
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
Unity editor expansion - draw lines
Dealing with duplicate data in Excel with xlwings
第一个Servlet
数据库原理期末复习
Really explain the five data structures of redis
XPath实现XML文档的查询
Cesium for unreal quick start - simple scenario configuration
[concurrent programming] synchronization container, concurrent container, blocking queue, double ended queue and work secret
ES6 promise learning notes
【Rust 笔记】13-迭代器(上)
Unity Editor Extension - Outline
【Rust 笔记】07-结构体
Allocation exception Servlet
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis