#include Using namespace Std;typedef enum {link,thread}pointertag;template struct Binarytreenodethd{t _data; Binarytreenodethd * _LEFT; Binarytreenodethd * _right; Pointertag _lefttag; Pointertag _righttag; BINARYTREENODETHD (): _left (null), _right (null), _lefttag (link), _righttag (link) {}BINARYTREENODETHD (const t&data ): _data (data), _left (null), _right (null), _lefttag (link), _righttag (link) {}};template Class BINARYTREETHD{PUBLIC:BINARYTREETHD (): _root (NULL) {}BINARYTREETHD (const t*a, size_t size) {size_t index = 0;_root = _createtree (A, size, index);} BINARYTREETHD (const BINARYTREETHD & T) {_root = _copy (t._root);} void Inorderthreading () {BINARYTREENODETHD * prev = null;_inorderthreading (_root, prev);} void Prevorderthreading () {BINARYTREENODETHD * prev = null;_prevorderthreading (_root, prev);} void Postorderthreading () {BINARYTREENODETHD * prev = null;_postorderthreading (_root, prev);} Public:void Inorderthd () {BINARYTREENODETHD * Cur=_root;while (cur) {while (Cur->_lefttag = = LINK) {cur = cur->_left;} cout< _data<< ""; while (Cur&&cur->_righttag = = THREAD) {cur = cur->_right;cout << cur->_data;} cur = cur->_right;} cout << Endl;} void Prevorderthd () {BINARYTREENODETHD *cur =_root;while (cur) {while (Cur&&cur->_lefttag = = LINK) {cout << cur->_data << ""; cur = cur-& Gt;_left;} cout << cur->_data << ""; while (Cur&&cur->_righttag = = THREAD) {cur = cur->_right;cout <& Lt Cur->_data << "";} if (Cur->_lefttag = = LINK) cur = cur->_left;elsecur = cur->_right;}} Protected:binarytreenodethd * _createtree (const t*a, size_t size, size_t &index) {BINARYTREENODETHD * Root = null;if (Index < Size&&a[index]! = ' # ') {root = new BINARYTREENODETHD (A[index]); root->_left = _createtree (A, size, ++index); root->_right = _createtree (A, size, ++index);} return root;} void _inorderthreading (BINARYTREENODETHD * cur, BINARYTREENODETHD *& prev) {if (cur = = null) return;_inorderthreading (Cur->_left, prev); if (cur->_left = = null) {Cur->_lefttag = Thread;cur->_left = prev;} if (Prev&&prev->_righttag = = NULL) {Prev->_righttag = Thread;prev->_right = cur;} Prev = cur;_inorderthreading (cur->_right, prev);} void _prevorderthreading (BINARYTREENODETHD *cur, BINARYTREENODETHD *& prev) {if (cur = = NULL) {return;} if (Cur->_left = = NULL) {Cur->_lefttag = Thread;cur->_left = prev;} if (prev&&prev->_right== NULL) {prev->_righttag = Thread;prev->_right = cur;} Prev = cur;if (Cur->_lefttag = = LINK) {_prevorderthreading (cur->_left, prev);} if (Cur->_righttag = = LINK) {_prevorderthreading (cur->_right, prev);}} void _postorderthreading (BINARYTREENODETHD *cur, BINARYTREENODETHD *& prev) {if (cur = = NULL) {return;} _postorderthreading (Cur->_left, prev); _postorderthreading (Cur->_right, prev); if (cur->_left = = NULL) {cur- >_lefttag = Thread;cur->_left = prev;} if (prev&&prev->_right = = NULL) {Prev->_righttag = Thread;prev->_right = cur;} prev = cur;} Binarytreenodethd * _copy (BINARYTREENODETHD * Root) {if (root = null) return null; BINARYTREENODETHD * newroot = new BINARYTREENODETHD (root->_data); newroot->_left = _copy (root->_l EFT); newroot->_right = _copy (root->_right); return newroot;} /*BINARYTREETHD & operator= (binarytreethd *& t) {if (This! = &t) {this->_destory (_roo t); _root = _copy (t._root);} return *this;} *///BINARYTREENODETHD & operator= (BINARYTREENODETHD t)//{//swap (_root, t._root);//ret Urn *this;//}protected:binarytreenodethd *_root;}; int main () {int a[] = {1, 2, 3, ' # ', ' # ', 4, ' # ', ' # ', 5, 6}; BINARYTREETHD T (A, 10); BINARYTREETHD TEMP;T.INORDERTHD (); t.inorderthreading ();//t.prevorderthreading ();//t.postorderthr Eading (); cout << endl;system ("pause"); return 0;}
Two fork tree thread