欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

数据结构后序线索二叉树C语言

时间:2023-05-26

#include using namespace std;#define Status int #define OK 1#define ERROR 0#define Elemtype chartypedef enum{link,Thread}PointerTag;typedef struct BiThrNode{Elemtype data;struct BiThrNode *lchild, *rchild; struct BiThrNode *parent;PointerTag LTag , RTag; }BiThrNode, *BiThrTree;BiThrTree pre; Status visit(Elemtype e){cout << e << endl;return OK;}char Vexch[26]={'A','B','D','H','$','$','I','$','$','E','J','$','$','$','C','F','$','$','G','$','$'}; int i=0; Status CreatBiThrTree(BiThrTree &T, BiThrTree &p) { if(Vexch[i++]=='$') T=NULL; else { T= new BiThrNode; if(!T) return 0; T->data=Vexch[i-1]; T->parent = p;T->LTag=link; CreatBiThrTree(T->lchild, T);T->RTag=link; CreatBiThrTree(T->rchild, T); } return 1; } void PostThreading(BiThrTree p) {if(p) {PostThreading(p->lchild); PostThreading(p->rchild); if(!p->lchild) { p->LTag = Thread; p->lchild = pre; }if(pre && !pre->rchild) {pre->RTag = Thread; pre->rchild = p ; }pre = p;}}Status PostOrderTraverse_Thr(BiThrTree T) {BiThrTree p;p = T; pre=NULL;while(p != NULL){ while(p->LTag == link) p = p->lchild; while(p->RTag == Thread) { visit(p->data);pre = p;p = p->rchild ; }if(p == T){ visit(p->data); break;}while(p && p->rchild == pre) { visit(p->data);pre = p;p = p->parent;}if(p && p->RTag == link)p = p->rchild;}return OK;}int main() {BiThrTree PostT;pre = NULL;CreatBiThrTree(PostT,pre);PostThreading(PostT);PostOrderTraverse_Thr(PostT);printf("n");return 0;}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。