Binary tree is a nonlinear structure, and traversing binary tree requires recursive or non-recursive traversal by means of a stack assist.
When a binary tree is used as a compressed storage structure, it takes a node to get the left child and the right child, and can not directly get the precursor or successor of any of the nodes ' traversal sequences. In order to achieve this kind of traversal, we use the null pointer in the binary tree to the left and right subtree to store the precursor and successor of the node.
Clue Binary Tree Idea:
When NULL is present at the left or right point of a node, the null sub-node of the node needs to be threaded.
In the lead, the node's precursor points to the previous visited node, so define a pointer prev to save the previous node; the successor of the node does not know, then we can in the future to the future of the next clue, so that the successor of Prev point to cur.
Pre-sequence traversal binary tree------root-left-to-right (1,2,3,4,5,6)
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7F/66/wKiom1cdiX7ScBUcAAA8DRIBJ08387.png "title=" Untitled. png "alt=" Wkiom1cdix7scbucaaa8dribj08387.png "/>
Template<class t>void binarytreethd<t>::P Revordertag ()//pre-order threaded binary Tree {node* prev = Null;_prevordertag (_root, prev);} Template<class t>void Binarytreethd<t>::_prevordertag (node* cur, node*& prev)//Pre-order threaded binary Tree {if (cur = = NULL) {return;} if (Cur->_left = = NULL) {Cur->_lefttag = Thread;cur->_left = prev;} if (prev && prev->_right = = NULL)//To future nodes to follow the previous node of the thread {Prev->_righttag = Thread;prev->_right = cur;} Prev = cur;if (Cur->_lefttag = = LINK)//Threaded left node {_prevordertag (Cur->_left, prev);} if (Cur->_righttag = = LINK)//Threaded right Node {_prevordertag (cur->_right, prev);}}
Middle sequence traversal binary tree------left-to-root right (3,2,4,1,6,5)
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7F/67/wKiom1cdkbmzS2-ZAAAwN3Yncp0317.png "title=" Untitled. png "alt=" wkiom1cdkbmzs2-zaaawn3yncp0317.png "/> Recursive implementation:
Template<class t>void Binarytreethd<t>::inordertag ()//middle sequence threaded binary tree {//recursive implementation of sequential thread node* prev = null;//The previous node of the thread _inoredertag (_root, prev);} Template<class t>void Binarytreethd<t>::_inoredertag (node* cur,node*& prev)//sequence-threaded binary tree {if (cur = = NULL ) {return;} _inoredertag (Cur->_left, prev);//recursion Cur->_left is empty if (Cur->_left = = null) {Cur->_lefttag = Thread;cur->_ left = prev;//The predecessor of the current node points to the previous node}//to lead to the predecessor of the node, and at the point where the cur points to the next junction, the successor of the previous nodes to cur//to the future, and the subsequent cue if of the previous junction (Prev && Prev->_right = = NULL) {Cur->_righttag = Thread;prev->_right = cur;} Prev = Cur;_inoredertag (cur->_right, prev);//recursion out Cur->_right is empty}
Non-recursive implementations (leveraging stacks):
Template<class t>void Binarytreethd<t>::inordertag_nonr ()//sequential cue binary tree--non-recursive {//stack implementation of sequence-clue stack<node* > s; node* prev = NULL; node* cur = _root;if (cur = = NULL) {return;} while (cur | |.!s.empty ()) {while (cur)//Find the leftmost node, enter the stack {s.push (cur); cur = cur->_left;} Cur is not an empty stack, cur is empty description cur has been threaded cur = s.top ();//loop into the top element of the stack and determine if it needs to be threaded if (cur = = NULL) {Cur->_left = Thread;cur->_left = prev;} S.pop ();//pop up the top of the stack, so that the stack top saves the subsequent prev of the cur that need to be threaded = cur;if (cur->_right = = NULL &&!s.empty ()) {Cur->_righttag = Thread;cur->_right = S.top (); cur = null;//set cur to null to prevent a dead loop (2)}else{cur = cur->_right;//cue jump to right subtree}}}
Sequential traverse binary tree------left-to-right root (3,4,2,6,5,1)
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7F/64/wKioL1cdjKezJY6cAAAwVprYE2c086.png "title=" Untitled. png "alt=" Wkiol1cdjkezjy6caaawvprye2c086.png "/>
Template<class t>void binarytreethd<t>::P Astordertag ()//post-threaded binary tree {node* prev = Null;_pastordertag (_root, prev);} Template<class t>void Binarytreethd<t>::_pastordertag (node* cur, node*& prev)//post-threaded binary tree {if (cur = = NULL) {return;} _pastordertag (Cur->_left, prev);//The leftmost node _pastordertag (cur->_right, prev);//Right node if (Cur->_left = = NULL)// Lead precursor {Cur->_lefttag = Thread;cur->_left = prev;} if (prev && prev->_right = = NULL)//threaded successor {Prev->_righttag = Thread;prev->_right = cur;} prev = cur;}
This article is from the "Materfer" blog, make sure to keep this source http://10741357.blog.51cto.com/10731357/1767450
The pre-order, middle order and post-sequence clue of binary tree