线索树
將二叉樹轉(zhuǎn)換成一個雙向鏈表
struct TBTNode {int data;int ltag, rtag;TBTNode *lchild;TBTNode *rchild; }; //中序 void Inthread(TBTNode *p, TBTNode *pre) {if (p != NULL) {Inthread(p->lchild, pre);if (p->lchild == NULL) {p->lchild = pre;p->ltag = 1;}if (pre->rchild == NULL && pre != NULL) {pre->rchild = p;pre->rtag;}pre = p;Inthread(p->rchild, pre);} } void createInthread(TBTNode *root) {TBTNode *pre = NULL;if (root != NULL) {Inthread(root, pre);pre->rchild = NULL;pre->rtag = 1;} } //遍歷 TBTNode *First(TBTNode *p) {while (p->ltag == 0) {p = p->lchild;}return p; } TBTNode *Next(TBTNode *p) {if (p->rtag == 0) {return First(p->rchild);}else {return p->rchild;} } void Inorder(TBTNode *root) {for (TBTNode *p = First(root); p != NULL; p = Next(p)) {cout << p->data << endl;} } //先序,后序和中序差不多?
總結(jié)