当前位置:网站首页>C语言实现树遍历找到指定的节点的前驱
C语言实现树遍历找到指定的节点的前驱
2022-07-20 02:58:00 【Y6blNU1L】
#include <stdio.h>
#include <stdlib.h>
/*
* 遍历找到指定的节点的前驱
*/
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
char p='F'; //目标节点值
BiTNode *pre=NULL;//当前访问的前驱
BiTNode *finall=NULL;//最终找到指定节点的前驱
void InitConst(){
BiTNode *pre=NULL;//当前访问的前驱
BiTNode *finall=NULL;//最终找到指定节点的前驱
}
void visit(BiTNode *q){
if(q->data == p) finall=pre;
else pre=q; //更新前驱状态到当前节点
}
//先序遍历
void PreOrder(BiTree t){
if(t!=NULL){
printf("%c ",t->data);//访问根节点
visit(t);
PreOrder(t->lchild); //访问左节点
PreOrder(t->rchild); //访问右节点
}
}
//中序遍历
void InOrder(BiTree t){
if(t!=NULL){
InOrder(t->lchild); //访问左节点
printf("%c ",t->data);//访问根节点
visit(t);
InOrder(t->rchild); //访问右节点
}
}
//后序遍历
void PostOrder(BiTree t){
if(t!=NULL){
PostOrder(t->lchild); //访问左节点
PostOrder(t->rchild); //访问右节点
printf("%c ",t->data);//访问根节点
visit(t);
}
}
//初始化树节点
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【先序遍历】%c前驱为:%c\n",p,finall->data);
InitConst();
InOrder(root);
printf("\n【中序遍历】%c前驱为:%c\n",p,finall->data);
InitConst();
PostOrder(root);
printf("\n【后序遍历】%c前驱为:%c\n",p,finall->data);
return 0;
}
边栏推荐
- 视频25-7章2节VGG 26-NiN 27-GooLeNet
- [R language text mining]: emotion analysis and word cloud mapping
- 10 个用于网络管理员进行高级扫描的端口扫描工具
- 【obs】Transform: fit to screen
- About: Customizing templates in office 2021
- 深度学习1-感知器
- LVS cluster application
- . Net core rapid development platform, powerful workflow engine, multi system rapid configuration
- 如何在自动化测试中使用MitmProxy获取数据返回?
- Use OpenCV to adjust the brightness and contrast of the image
猜你喜欢
【5GC】5G PDU会话以及会话类型
Top priority of dry goods: common indicators and terms in data analysis!
AT32 MCU F415 OTG新功能使用
vben-admin 时间选择器相关配置以及设置不可选择的时间
高通和MTK针对国家wifi channel 客制化修改方法
Understanding and applying continuous integration Tekton
某网站登录接口password参数还原
Record of force deduction and question brushing 3---34 Find the first and last positions of elements in a sorted array
通过例子学C标准库<assert.h>
为什么说巨星传奇(周杰伦)不构成传销?分销和传销有什么区别?
随机推荐
About: Customizing templates in office 2021
【obs】Transform: fit to screen
Matlab summary of differential equation solving methods
干货丨重中之重:数据分析中常用指标及术语!
At32 MCU f415 OTG new function use
如何在自动化测试中使用MitmProxy获取数据返回?
10 个用于网络管理员进行高级扫描的端口扫描工具
It's 2022, and you don't know what automated testing is
371 pages of 200000 words 2021 smart city informatization comprehensive construction plan
[noi simulation] Simen Nong number (number theory, linked list)
Exch2010: rebuild the entire DAG
Exch2010:重建整个 DAG
Fundamentals of machine learning (2): basic knowledge and drawing graphics
Proxyman, a native high-performance packet capturing tool, is for you who love learning
JVM调优方法
为什么说巨星传奇(周杰伦)不构成传销?分销和传销有什么区别?
微信小程序如何实现select二级下拉
数据工程系列精讲(第五讲): Data-centric AI之数据集质量
【R语言文本挖掘】:情感分析与词云图绘制
LeetCode__ 301 weekly games 6112. Minimum total time required to fill the cup___ Greedy two methods