Clue Two fork Tree

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.