当前位置:网站首页>Three traversals of binary tree in C language
Three traversals of binary tree in C language
2022-07-21 02:03:00 【Y6blNU1L】
#include <stdio.h>
#include <stdlib.h>
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
// The first sequence traversal
void PreOrder(BiTree t){
if(t!=NULL){
printf("%c ",t->data);// Access the root node
PreOrder(t->lchild); // Access the left node
PreOrder(t->rchild); // Access the right node
}
}
// In the sequence traversal
void InOrder(BiTree t){
if(t!=NULL){
InOrder(t->lchild); // Access the left node
printf("%c ",t->data);// Access the root node
InOrder(t->rchild); // Access the right node
}
}
// After the sequence traversal
void PostOrder(BiTree t){
if(t!=NULL){
PostOrder(t->lchild); // Access the left node
PostOrder(t->rchild); // Access the right node
printf("%c ",t->data);// Access the root node
}
}
// Count the total number of binary tree nodes
int BiTreeLength(BiTree root)
{
if(root==NULL) return 0;
return 1+BiTreeLength(root->lchild) + BiTreeLength(root->rchild);
}
// Initialize the tree node
void InitBiTree(BiTree &root){
root=(BiTree)malloc(sizeof(BiTNode));
root->data={'A'};
root->lchild=root->rchild=NULL;
BiTNode *p1=(BiTNode *)malloc(sizeof(BiTNode));
p1->data={'B'};
p1->lchild=p1->rchild=NULL;
root->lchild=p1;
BiTNode *p2=(BiTNode *)malloc(sizeof(BiTNode));
p2->data={'C'};
p2->lchild=p2->rchild=NULL;
root->rchild=p2;
BiTNode *p3=(BiTNode *)malloc(sizeof(BiTNode));
p3->data={'D'};
p3->lchild=p3->rchild=NULL;
p1->lchild=p3;
BiTNode *p4=(BiTNode *)malloc(sizeof(BiTNode));
p4->data={'E'};
p4->lchild=p4->rchild=NULL;
p1->rchild=p4;
BiTNode *p5=(BiTNode *)malloc(sizeof(BiTNode));
p5->data={'F'};
p5->lchild=p5->rchild=NULL;
p2->lchild=p5;
BiTNode *p6=(BiTNode *)malloc(sizeof(BiTNode));
p6->data={'G'};
p6->lchild=p6->rchild=NULL;
p2->rchild=p6;
}
int main(){
BiTree root=NULL;
InitBiTree(root);
PreOrder(root);
printf("\n");
InOrder(root);
printf("\n");
PostOrder(root);
printf("\n");
int cnt=BiTreeLength(root);
printf(" The node number of binary tree is :%d\n",cnt);
return 0;
}
边栏推荐
- 从三翼鸟,透视家居品牌的“飞轮效应”
- At32 uses the kernel DWT register to set the delay time
- 2022 Hangzhou future science and technology city digital economy talent programming competition 02 Black and white chess game_____ sliding window
- How to use mitmproxy to get data return in automated testing?
- “蔚来杯“2022牛客暑期多校训练营1 I Chiitoitsu(概率dp)
- 【5GC】5G PDU会话以及会话类型
- [LeetCode]剑指 Offer 52. 两个链表的第一个公共节点
- 95 pages intelligent factory digitalization, intelligent planning, solutions and construction scheme 2022
- A gadget that displays SAP CRM product hierarchy
- 视频25-7章2节VGG 26-NiN 27-GooLeNet
猜你喜欢
Top priority of dry goods: common indicators and terms in data analysis!
服务器的1U、2U、4U是指什么?
Matlab: print the figure into PDF format
95 pages intelligent factory digitalization, intelligent planning, solutions and construction scheme 2022
Highest evaluation: the development career path you want to follow, your determination and action are thorough, sincere and absolutely true
【Pygame 学习笔记】8.精灵
MySQL 索引
Compile + link and preprocess
数据工程系列精讲(第五讲): Data-centric AI之数据集质量
[pygame Learning notes] 8. Elfe.
随机推荐
vben-admin 时间选择器相关配置以及设置不可选择的时间
. Net core rapid development platform, powerful workflow engine, multi system rapid configuration
RedHat 7 network service cannot start. The problem ("device does not see to be present, delaying initialization") is handled
U.S. lawmakers advocate cracking down on encrypted mining, ringing the alarm of encryption? Only by reducing the carbon footprint can we achieve real value
Chromeoptions common configuration and webui practice
JTAG debugging command line debugging of arm bare board debugging
95页智能工厂数字化、智能化规划、解决方案及建设方案2022
At32 MCU f415 OTG new function use
Understanding and applying continuous integration Tekton
C语言实现树遍历找到指定的节点的前驱
To clarify the tax arrears: there is no tax arrears, and will continue to operate in compliance, rooted in China
【webrtc】ImportError: No module named win32file
手动操纵工业机器人
DTOS帝拓思的3D引擎将取代游戏引擎巨兽,实现国产化替代
Calculate the value of any root n
[pygame Learning notes] 8. Elfe.
Manually operated industrial robot
如何安装scons低版本
第一篇:反射框架Reflections
[OBS] text description of QT UI