C語言實(shí)現(xiàn)線索二叉樹的定義與遍歷示例
更新時(shí)間:2017年06月06日 08:29:23 作者:PHP開發(fā)學(xué)習(xí)門戶
這篇文章主要介紹了C語言實(shí)現(xiàn)線索二叉樹的定義與遍歷,結(jié)合具體實(shí)例形式分析了基于C語言的線索二叉樹定義及遍歷操作相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下
本文實(shí)例講述了C語言實(shí)現(xiàn)線索二叉樹的定義與遍歷。分享給大家供大家參考,具體如下:
#include <stdio.h> #include <malloc.h> typedef char TElemType; // 二叉樹的二叉線索存儲(chǔ)表示 typedef enum{ Link, Thread }PointerTag; // Link(0):指針,Thread(1):線索 typedef struct BiThrNode { TElemType data; struct BiThrNode *lchild,*rchild; // 左右孩子指針 PointerTag LTag,RTag; // 左右標(biāo)志 }BiThrNode,*BiThrTree; TElemType Nil = ' '; // 字符型以空格符為空 BiThrTree pre; // 全局變量,始終指向剛剛訪問過的結(jié)點(diǎn) // 按先序輸入二叉線索樹中結(jié)點(diǎn)的值,構(gòu)造二叉線索樹T // 空格(字符型)表示空結(jié)點(diǎn) int CreateBiThrTree(BiThrTree *T) { TElemType h; scanf("%c",&h); if(h==Nil) *T=NULL; else { *T=(BiThrTree)malloc(sizeof(BiThrNode)); if(!*T) exit(0); (*T)->data=h; // 生成根結(jié)點(diǎn)(先序) CreateBiThrTree(&(*T)->lchild); // 遞歸構(gòu)造左子樹 if((*T)->lchild) // 有左孩子 (*T)->LTag=Link; CreateBiThrTree(&(*T)->rchild); // 遞歸構(gòu)造右子樹 if((*T)->rchild) // 有右孩子 (*T)->RTag=Link; } return 1; } // 算法6.7 P135 // 中序遍歷進(jìn)行中序線索化。 void InThreading(BiThrTree p) { if(p) { InThreading(p->lchild); // 遞歸左子樹線索化 if(!p->lchild) // 沒有左孩子 { p->LTag=Thread; // 前驅(qū)線索 p->lchild=pre; // 左孩子指針指向前驅(qū) } if(!pre->rchild) // 前驅(qū)沒有右孩子 { pre->RTag=Thread; // 后繼線索 pre->rchild=p; // 前驅(qū)右孩子指針指向后繼(當(dāng)前結(jié)點(diǎn)p) } pre=p; // 保持pre指向p的前驅(qū) InThreading(p->rchild); // 遞歸右子樹線索化 } } // 算法6.6 P134 // 中序遍歷二叉樹T,并將其中序線索化,Thrt指向頭結(jié)點(diǎn)。 int InOrderThreading(BiThrTree *Thrt,BiThrTree T) { *Thrt=(BiThrTree)malloc(sizeof(BiThrNode)); // 建頭結(jié)點(diǎn) if(!*Thrt) exit(0); (*Thrt)->LTag=Link; //標(biāo)志左孩子為指針 (*Thrt)->RTag=Thread; //標(biāo)志右孩子為線索 (*Thrt)->rchild=*Thrt; // 右指針回指 if(!T) // 若二叉樹空,則左指針回指 (*Thrt)->lchild=*Thrt; else { (*Thrt)->lchild=T; //頭結(jié)點(diǎn)左指針指向樹的根 pre = *Thrt; InThreading(T); // 中序遍歷進(jìn)行中序線索化 pre->RTag=Thread; // 最后一個(gè)結(jié)點(diǎn)線索化 pre->rchild=*Thrt; (*Thrt)->rchild=pre; } return 1; } // 算法6.5 P134 // 中序遍歷二叉線索樹T(頭結(jié)點(diǎn))的非遞歸算法。 int InOrderTraverse_Thr(BiThrTree T,int(*Visit)(TElemType)) { BiThrTree p; p=T->lchild; // p指向根結(jié)點(diǎn) while(p!=T) { // 空樹或遍歷結(jié)束時(shí),p==T while(p->LTag==Link) p=p->lchild; if(!Visit(p->data)) // 訪問其左子樹為空的結(jié)點(diǎn) return 0; while(p->RTag==Thread&&p->rchild!=T) { p=p->rchild; Visit(p->data); // 訪問后繼結(jié)點(diǎn) } p=p->rchild; } return 1; } int vi(TElemType c) { printf("%c ",c); return 1; } int main() { BiThrTree H,T; printf("請(qǐng)按先序輸入二叉樹(如:ab三個(gè)空格,表示a為根結(jié)點(diǎn)," "b為左子樹的二叉樹)\n"); CreateBiThrTree(&T); // 按先序產(chǎn)生二叉樹 InOrderThreading(&H,T); // 中序遍歷,并中序線索化二叉樹 printf("中序遍歷(輸出)二叉線索樹:\n"); InOrderTraverse_Thr(H,vi); // 中序遍歷(輸出)二叉線索樹 printf("\n"); system("pause"); return 0; }
運(yùn)行結(jié)果:
希望本文所述對(duì)大家C語言程序設(shè)計(jì)有所幫助。
相關(guān)文章
Cocos2d-x UI開發(fā)之CCControlButton控件類實(shí)例
這篇文章主要介紹了Cocos2d-x UI開發(fā)之CCControlButton控件類實(shí)例,本文代碼中包含大量注釋來講解CCControlButton控件類的使用,需要的朋友可以參考下2014-09-09C++中的多態(tài)與多重繼承實(shí)現(xiàn)與Java的區(qū)別
這篇文章主要介紹了C++中的多態(tài)與多重繼承實(shí)現(xiàn)與Java的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03使用MinGW使Windows通過gcc實(shí)現(xiàn)C或C++程序本地編譯執(zhí)行的方法
這篇文章主要介紹了使用MinGW使Windows通過gcc實(shí)現(xiàn)C或C++程序本地編譯執(zhí)行的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11C++ leetcode之刪除并獲得點(diǎn)數(shù)的示例代碼
這篇文章主要介紹了C++ leetcode之刪除并獲得點(diǎn)數(shù)的示例代碼,本文給大家分享問題解析及解決方案,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05