網站首頁 編程語言 正文
1.結構
#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 先序創建二叉樹
int j=0; //創建二叉樹的全局變量 //先序創建二叉樹 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); } }
輸出函數:
inline bool visit(int e){ //此處使用內斂函數,提高運行效率 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 中序遍歷
//求中序首結點 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.后序線索化
//后續線索化 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 后序遍歷
//求后序前驅 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); } }
總結
tag最好另起函數進行初始化,并且在遍歷中,牢抓前中后的遍歷次序進行分析。
原文鏈接:https://blog.csdn.net/m0_61395860/article/details/123036493
相關推薦
- 2022-05-20 ElasticSearch 7.X系列之:查詢分析索引磁盤使用空間_disk_usage
- 2024-07-15 RedisTemplate使用
- 2022-05-25 C語言三個函數的模擬實現詳解_C 語言
- 2023-09-12 利用Map結合Supplier消除switch...case
- 2022-10-11 oracle 12c和plsql的詳細安裝和配置過程(超級詳細,小白也能懂)
- 2022-09-05 Linux系統下創建守護進程
- 2023-06-16 GO語言中defer實現原理的示例詳解_Golang
- 2024-03-25 解決idea配置自定義的maven失敗的問題
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支