網(wǎng)站首頁 編程語言 正文
1.結(jié)構(gòu)
#include#include #define false 0 #define true 1 using namespace std; typedef struct BTnode{ int data; struct BTnode *lchild,*rchild; int ltag,rtag; }*BTree,BTnode;
1.1初始化tag
#include#include #define false 0 #define true 1 using namespace std; typedef struct BTnode{ int data; struct BTnode *lchild,*rchild; int ltag,rtag; }*BTree,BTnode;
2.基本操作
2.1 先序創(chuàng)建二叉樹
int j=0; //創(chuàng)建二叉樹的全局變量 //先序創(chuàng)建二叉樹 int CreateBTree(BTree &T){ int str[]={1,2,3,NULL,4,NULL,NULL,NULL,5,6,NULL,7,NULL,NULL,8,NULL,NULL}; if(str[j]=='#') return false; if(str[j]==NULL){ T=NULL; j++; }else{ T=(BTnode *)malloc(sizeof(BTnode)); T->data=str[j]; j++; CreateBTree(T->lchild); CreateBTree(T->rchild); } }
輸出函數(shù):
inline bool visit(int e){ //此處使用內(nèi)斂函數(shù),提高運行效率 printf("%d",e); return true; }
2.2.先序線索化
//先序線索化. void prehread(BTree &root){ if(root!=NULL){ if(root->lchild==NULL){ root->ltag=1; root->lchild=pre; }else{ root->ltag=0; } if(pre){ if(pre->rchild==NULL){ pre->rtag=1; pre->rchild=root; }else{ pre->rtag=0; } } pre=root; if(root->ltag==0){ prehread(root->lchild); } if(root->rtag==0){ prehread(root->rchild); } } }
2.2.1.先序遍歷
//尋找先序后繼 BTree preNext(BTree T){ if(T->rtag==1){ return T->rchild; }else{ if(T->ltag==0){ return T->lchild; }else{ return T->rchild; } } } //先序線索二叉樹的遍歷 void prebianli(BTree T){ BTree p; p=T; while(p){ visit(p->data); p=preNext(p); } }
2.3.中序線索化
//中序線索化 BTree pre=NULL ; //中序線索化的全局變量 void Inthread(BTree &root){ if(root!=NULL){ Inthread(root->lchild); if(root->lchild==NULL){ root->ltag=1; root->lchild=pre; }else{ root->ltag=0; } if(pre){ if(pre->rchild==NULL){ pre->rtag=1; pre->rchild=root; }else{ pre->rtag=0; } } pre=root; Inthread(root->rchild); } }
2.3.1 中序遍歷
//求中序首結(jié)點 BTree InFirst(BTree T){ BTree p=T; if(p==NULL) return NULL; while(p->ltag==0){ p=p->lchild; } return p; } //求中序后繼 BTree InNext(BTree T) { BTree next=NULL; if(T->rtag==1){ next=T->rchild; }else { T = T->rchild; while (T->ltag==0 ) { T = T->lchild; } next=T; } return next; } //中序線索二叉樹的遍歷 void Inbianli(BTree T){ BTree p; p=InFirst(T); while(p){ visit(p->data); p=InNext(p); } }
2.4.后序線索化
//后續(xù)線索化 void Postthread(BTree &root){ BTree pre=NULL; if(root){ Postthread(root->lchild); Postthread(root->rchild); if(root->lchild==NULL){ root->ltag=1; root->lchild=pre; } if(pre&&pre->rchild==NULL){ pre->rtag=1; pre->rchild=root; } pre=root; } }
2.4.1 后序遍歷
//求后序前驅(qū) BTree postnext(BTree T){ if(T->ltag==0){ if(T->rtag==0){ return T->rchild; }else{ return T->lchild; } }else { return T->lchild; } } //后序遍歷 void postbianli(BTree T){ BTree p; p=T; while(p){ p=postnext(p); visit(p->data); } }
總結(jié)
tag最好另起函數(shù)進(jìn)行初始化,并且在遍歷中,牢抓前中后的遍歷次序進(jìn)行分析。
原文鏈接:https://blog.csdn.net/m0_61395860/article/details/123036493
相關(guān)推薦
- 2022-06-14 Docker?配置容器固定IP的方法_docker
- 2022-10-28 Go語言包和包管理詳解_Golang
- 2022-08-06 Python中if?__name__==‘__main__‘用法詳情_python
- 2022-04-09 C++實現(xiàn)加減乘除計算器_C 語言
- 2022-04-04 iview在Table表格中渲染title文字提示,使用render實現(xiàn)
- 2022-10-04 Android系統(tǒng)優(yōu)化Ninja加快編譯_Android
- 2022-12-05 Python實現(xiàn)字符串模糊匹配方式_python
- 2022-12-12 C語言中組成不重復(fù)的三位數(shù)問題_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支