您好,登錄后才能下訂單哦!
這篇“如何用C語(yǔ)言遞歸實(shí)現(xiàn)線索二叉樹”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“如何用C語(yǔ)言遞歸實(shí)現(xiàn)線索二叉樹”文章吧。
描述:將二叉樹中結(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; }
以上就是關(guān)于“如何用C語(yǔ)言遞歸實(shí)現(xiàn)線索二叉樹”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。