Binary tree is a nonlinear structure, and traversal of binary tree is accomplished by recursive or non-recursive traversal by means of a stack assist. 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.
In order to save the information needed in the traversal, we use the null pointer in the binary tree to the left and right subtree to hold the node's precursor and subsequent information.
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/80/05/wKiom1c0TMahtW-lAAANAVyYRG0326.png "title=" Image.png "alt=" Wkiom1c0tmahtw-laaanavyyrg0326.png "/>
The code structure is as follows:
Enum Pointertag{thread,link};template<class t>struct binarytreenode{t _data; binarytreenode<t>* _left; binarytreenode<t>* _right; Pointertag _lefttag;//left child clue flag Pointertag _righttag;//right child clue flag Binarytreenode (const t& D): _data (d), _left (NULL), _ Right (NULL) {_lefttag = Link;_righttag = LINK;}};
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/80/02/wKioL1c0TieivAASAAApdH6AISE596.png "style=" float: none; "Title=" Image [1].png "alt=" Wkiol1c0tieivaasaaapdh6aise596.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/80/05/wKiom1c0TUaRqvI1AAAmuN8_Knc531.png "style=" float: none; "Title=" Image [2].png "alt=" Wkiom1c0tuarqvi1aaamun8_knc531.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/80/02/wKioL1c0TiiSHvvWAAAlnnPxkH8632.png "style=" float: none; "Title=" Image [3].png "alt=" Wkiol1c0tiishvvwaaalnnpxkh8632.png "/>
The code is implemented as follows :
Void inorderthreading () //in-sequence clue {node* prev = null;_inorderthreading (_root, prev);} Void prevorderthreading () //pre-order Thread {node* prev = null;_prevorderthreading (_ Root, prev);} Void postorderthreading () //post-//{//node* prev = null;//_ of clues Postorderthreading (_root, prev);//}VOID&NBSP;INORDERTHD () //sequence traversal {node* cur = _root;while (cur) {while (cur->_lefttag == link) {cur = cur- >_left;} cout << cur->_data << " ";while (cur->_righttag == THREAD) {cur = cur->_right;cout << cur->_data << " ";} Cur = cur->_right;} Cout << endl;} VOID&NBSP;PREVORDERTHD () //Pre-order traversal {node* cur = _root;while (cur) {cout << cur->_data << " ";while (Cur->_lefttag == link) {cur = cur-> _left;cout << cur->_data << " ";} Cur = cur->_right;} Cout << endl;} Void _inorderthreading (Node* root, node*& prev) //{if (root == null) {return;} _inorderthreading (Root->_left, prev);if (root->_left == null) {Root->_leftTag = thread;root->_left = prev;} if (prev&& (prev->_right == null)) {prev->_righttag = thread;prev->_ Right = root;} Prev = root;_inorderthreading (Root->_right, prev);} Void _prevorderthreading (Node* root, node*& prev) {node* cur = root;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);}}
Because the post-post is more complex, so the dew is not very capable of writing it out. After the dew will be good to improve themselves, and then the next stage to achieve a haha 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0003.gif "alt=" j_0003.gif "/ >
Binary tree (ii)---clue binary tree