C語(yǔ)言遞歸實(shí)現(xiàn)線索二叉樹(shù)
本文實(shí)例為大家分享了C語(yǔ)言遞歸實(shí)現(xiàn)線索二叉樹(shù)的具體代碼,供大家參考,具體內(nèi)容如下
描述:將二叉樹(shù)中結(jié)點(diǎn)的空左孩子指針域指向前驅(qū)結(jié)點(diǎn),將空的右孩子指針域指向后繼結(jié)點(diǎn)。
code:
#pragma warning(disable:4996) #include<stdio.h> #include<stdlib.h> typedef struct TreeNode { char data; struct TreeNode *lchild, *rchild; int ltag, rtag; }Tree,*BTree; BTree Build_Tree(void) { BTree T; char ch; scanf("%c", &ch); if (ch == '#') { T = NULL; } else { T = (BTree)malloc(sizeof(Tree)); T->data = ch; T->ltag = 0; T->rtag = 0; T->lchild = Build_Tree(); T->rchild = Build_Tree(); } return T; } //先序線索化 void Pre_Thread(BTree cur, BTree *pre) { if (cur && cur->ltag==0) { printf("%c ", cur->data); if (cur->lchild == NULL) { cur->lchild = *pre; (*pre)->ltag = 1; cur->ltag = 1; } if (cur->rchild == NULL) { cur->rtag = 1; } if (*pre && (*pre)->rtag == 1) { (*pre)->rchild = cur; } *pre = cur; Pre_Thread(cur->lchild, pre); Pre_Thread(cur->rchild, pre); } } //中序線索化 void In_Thread(BTree cur, BTree *pre) { if (cur) { In_Thread(cur->lchild, pre); printf("%c ", cur->data); if (cur->lchild==NULL) { cur->lchild = *pre; cur->ltag = 1; } if (cur->rtag == NULL) { cur->rtag = 1; } if (*pre && (*pre)->rtag == 1) { (*pre)->rchild = cur; } *pre = cur; In_Thread(cur->rchild, pre); } } //后序線索化 void Post_Thread(BTree cur, BTree *pre) { if (cur) { Post_Thread(cur->lchild, pre); Post_Thread(cur->rchild, pre); printf("%c ", cur->data); if (cur->lchild == NULL) { cur->lchild = *pre; cur->ltag = 1; } if (cur->rchild == NULL) { cur->rtag = 1; } if (*pre && (*pre)->rtag == 1) { (*pre)->rchild = cur; } *pre = cur; } } int main(void) { BTree T,p=NULL; T = Build_Tree(); Pre_Thread(T, &p); //In_Thread(T, &p); //Post_Thread(T, &p); return 0; }
跑時(shí)分別運(yùn)行前序、中序、后序線索化。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Cocos2d-x中CCEditBox文本輸入框的使用實(shí)例
這篇文章主要介紹了Cocos2d-x中CCEditBox文本輸入框的使用實(shí)例,本文在代碼中用大量注釋講解了CCEditBox的使用方法,需要的朋友可以參考下2014-09-09C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表的簡(jiǎn)單實(shí)例
這篇文章主要介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表的簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06C語(yǔ)言?超詳細(xì)順序表的模擬實(shí)現(xiàn)實(shí)例建議收藏
程序中經(jīng)常需要將一組數(shù)據(jù)元素作為整體管理和使用,需要?jiǎng)?chuàng)建這種元素組,用變量記錄它們,傳進(jìn)傳出函數(shù)等。一組數(shù)據(jù)中包含的元素個(gè)數(shù)可能發(fā)生變化,順序表則是將元素順序地存放在一塊連續(xù)的存儲(chǔ)區(qū)里,元素間的順序關(guān)系由它們的存儲(chǔ)順序自然表示2022-03-03C++實(shí)現(xiàn)單例模式的自動(dòng)釋放
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)單例模式的自動(dòng)釋放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06C語(yǔ)言實(shí)現(xiàn)頁(yè)面置換算法(FIFO、LRU)
這篇文章主要介紹了通過(guò)C語(yǔ)言實(shí)現(xiàn)的兩種頁(yè)面置換算法:先進(jìn)先出(FIFO)頁(yè)面置換算法和最近最久未使用(LRU)頁(yè)面置換算法。文中的代碼具有一定的學(xué)習(xí)或工作價(jià)值,快來(lái)跟隨小編學(xué)習(xí)一下吧2021-12-12C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單掃雷源碼
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單掃雷源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04C語(yǔ)言實(shí)現(xiàn)萬(wàn)年歷源碼
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)萬(wàn)年歷源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10