C語言實現(xiàn)線索二叉樹的前中后創(chuàng)建和遍歷詳解
更新時間:2022年02月21日 10:32:43 作者:犀牛超人
這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)線索二叉樹的前中后創(chuàng)建和遍歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
1.結(jié)構(gòu)
#include<stdio.h>
#include<stdlib.h>
#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<stdio.h>
#include<stdlib.h>
#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)行分析。
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
C++ 二維數(shù)組參數(shù)傳遞的實現(xiàn)方法
這篇文章主要介紹了C++ 二維數(shù)組參數(shù)傳遞的實現(xiàn)方法的相關(guān)資料,這里提供三種方法幫助大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08
淺談內(nèi)聯(lián)函數(shù)與宏定義的區(qū)別詳解
本篇文章是對內(nèi)聯(lián)函數(shù)與宏定義的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++實現(xiàn)LeetCode(58.求末尾單詞的長度)
這篇文章主要介紹了C++實現(xiàn)LeetCode(58.求末尾單詞的長度),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07

