When a binary tree is used as a storage structure, it takes a node to get the left child and the right child of the node, and can not directly get the precursor or successor of any of the nodes ' traversal sequences. But often we want to be more intuitive to know the predecessor of the node. The Clue two fork tree appears particularly important.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7F/52/wKioL1cZ4PHAQI1dAAATb_tS_YE250.png "title=" Capture 3. PNG "alt=" Wkiol1cz4phaqi1daaatb_ts_ye250.png "/>
The key to the clue two fork tree is to define a global variable to hold the last visited node.
node* prev;
(i) Pre-order clue two fork Tree
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7F/52/wKioL1cZ4Rnh5EcpAAAalaSqfkY164.png "title=" Capture 2. PNG "alt=" Wkiol1cz4rnh5ecpaaaalasqfky164.png "/>
void Prevordertag () {_prevordertag (_root);} void _prevordertag (node* root)//pre-order thread two fork tree {if (root = NULL) return;if (!root->_left) {Root->_lefttag = thread;root- >_left = prev;} if (prev &&!prev->_right) {Prev->_righttag = Thread;prev->_right = root;} Prev = root;if (Root->_lefttag = = link) _prevordertag (root->_left); if (Root->_righttag = = link) _prevordertag ( Root->_right);}
(ii) Middle sequence clue two fork tree
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7F/52/wKioL1cZ4rqD83EoAAAdvvhjOEE594.png "title=" Capture 4. PNG "alt=" Wkiol1cz4rqd83eoaaadvvhjoee594.png "/>
void Midordertag () {_midordertag (_root);} void _midordertag (node* root)//middle order thread two fork tree {if (root = NULL) {return;} _midordertag (Root->_left); if (!root->_left) {Root->_lefttag = Thread;root->_left = prev;} if (prev&&!prev->_right) {Prev->_righttag = Thread;prev->_right = root;} Prev = Root;_midordertag (root->_right);}
(iii) Post-trail Clue two fork Tree
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7F/52/wKioL1cZ48nB07OgAAAb2FHsZJM967.png "title=" Capture 5. PNG "alt=" Wkiol1cz48nb07ogaaab2fhszjm967.png "/>
void Rearordertag () {_rearordertag (_root);} void _rearordertag (node* root)//post-thread two fork tree {if (root = NULL) {return;} _rearordertag (Root->_left); _rearordertag (root->_right); if (!root->_left) {root->_lefttag = THREAD; Root->_left = prev;} if (prev&&!prev->_right) {Prev->_righttag = Thread;prev->_right = root;} prev = root;}
Clue Two fork Tree