隐藏页面特效

线索化二叉树

/*
  先序线索化
*/
#include<stdio.h> #include<stdlib.h> //建立线索二叉树存储结构 typedef struct ThreadNode{ int data; //数据元素 struct ThreadNode *lchild,*rchild; //左右子树 int ltag,rtag; //左右线索标志 }ThreadNode,*ThreadTree; //先序线索化二叉树 void PreThread(ThreadTree &p,ThreadTree &pre){ if(p!=NULL){ /*visit()*/ if(p->lchild==NULL){ //左子树为空,建立前驱线索 p->lchild=pre; p->ltag=1; } if(pre!=NULL&&pre->rchild==NULL){ pre->rchild=p; //右子树为空,建立后继线索 pre->rtag=1; } pre=p; if(p->ltag==0){ PreThread(p->lchild,pre); //递归线索化左子树 } PreThread(p->rchild,pre); //递归线索化右子树 } } //建立线索化二叉树 void CreatePreThread(ThreadTree T){ ThreadTree pre=NULL; if(T!=NULL){ //非空二叉树——线索化 PreThread(T,pre); //线索化二叉树 pre->rchild=NULL; //处理遍历的最后一个结点 pre->rtag=1; } } int main(){ }

 

/* 中序线索化 */ #include<stdio.h> #include<stdlib.h> //建立线索二叉树存储结构 typedef struct ThreadNode{ int data; //数据元素 struct ThreadNode *lchild,*rchild; //左右子树 int ltag,rtag; //左右线索标志 }ThreadNode,*ThreadTree; //线索化二叉树 void InitThread(ThreadTree &p,ThreadTree &pre){ if(p!=NULL){ InitThread(p->lchild,pre); //递归线索化左子树 if(p->lchild==NULL){ p->lchild=pre; p->ltag=1; } if(pre!=NULL&&pre->rchild==NULL){ pre->rchild=p; pre->rtag=1; } pre=p; InitThread(p->rchild,pre); } } //建立线索化二叉树 void CreateInitThread(ThreadTree T){ ThreadTree pre=NULL; if(T!=NULL){ InitThread(T,pre); pre->rchild=NULL; pre->rtag=1; } } int main(){ }

 

/* 后序线索化 */ #include<stdio.h> #include<stdlib.h> //建立线索二叉树存储结构 typedef struct ThreadNode{ int data; //数据元素 struct ThreadNode *lchild,*rchild; //左右子树 int ltag,rtag; //左右线索标志 }ThreadNode,*ThreadTree; //后序线索化二叉树 void PostThread(ThreadTree &p,ThreadTree &pre){ if(p!=NULL){ PostThread(p->lchild,pre); //递归线索化左子树 PostThread(p->rchild,pre); //递归线索化右子树 if(p->lchild==NULL){ //左子树为空,建立前驱线索 p->lchild=pre; p->ltag=1; } if(pre!=NULL&&pre->rchild==NULL){ pre->rchild=p; //右子树为空,建立后继线索 pre->rtag=1; } pre=p; } } //建立线索化二叉树 void CreatePostThread(ThreadTree T){ ThreadTree pre=NULL; if(T!=NULL){ //非空二叉树——线索化 PostThread(T,pre); //线索化二叉树 pre->rchild=NULL; //处理遍历的最后一个结点 pre->rtag=1; } } int main(){ }

 


__EOF__

本文作者CherriesOvO
本文链接https://www.cnblogs.com/zyj3955/p/16487761.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   CherriesOvO  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2020-07-17 学习Java的第十二天
点击右上角即可分享
微信分享提示