1. Two fork tree is a non-linear structure, traverse binary tree almost all by recursion or with the aid of the stack to implement the recursive traversal, with a binary tree as a storage structure, take a node, only the left child and the right child, can not directly get the node of any of the predecessor and successor of the sequence.
2. In order to preserve this information needed in the traversal, we use a null pointer to the left and right subtree in the binary tree to store the precursor and subsequent information of the node.
To create a node:
Enum Pointertag
{
THREAD,
LINK,
};
Template<class t>
struct binarytreethdnode
{
T _data; Data
binarytreethdnode<t>* _LETF; Left child
binarytreethdnode<t>* _right; Right child
Pointertag _letftag; Left child clue sign
Pointertag _righttag; Right child clue flag
Binarytreethdnode (const t& data)
: _data (data)
, _LETF (null)
, _right (null)
{}
};
The threaded process and traversal:
Template<class T> class Binarytreethd {typedef binarytreethdnode<t> Node; Public:typedef BinaryTreeThdIter
Ator<t,t&,t*> Iterator;
BINARYTREETHD ();
BINARYTREETHD (const t* a,size_t size,const t& Invalid): _root (NULL) {size_t index=0;
_root=_greatetree (A,size,index,invalid);
} void Inorderthd ()//in-sequence-threaded {node* prev=null;
_INODERTHD (_root,prev);
} void Prevorderthd ()//pre-order thread {node* prev=null;
_PREVORDERTHD (_root,prev);
} void Postorderthd ()//post-threaded {node* prev=null;
_POSTORDERTHD (_root,prev);
} void Prevorder ()//after the first-order traversal of the _prevorder (_root);
cout<<endl;
} void Inorder ()//After the lead-in sequence traversal {_inorder (_root);
cout<<endl;
} void Postorder ()//After the post-thread traversal of {node* cur=_root;
node* Prev=null;
while (cur) {//Find the leftmost node while (Cur&&cur->_letftag ==link) {cur=cur->_letf; }//Access successor while (Cur&&cur->_righttag ==thread) {Cout<<cur->_datA << "";
Prev=cur;
} cur=cur->_right;
}} protected:void _postorderthd (node* root,node*& prev) {if (root==null) return;
_POSTORDERTHD (ROOT->_LETF, prev);
_POSTORDERTHD (Root->_right, prev);
if (root->_letf ==null) {Root->_letftag =thread;
ROOT->_LETF =prev;
} if (Prev&&prev->_right ==null) {Prev->_righttag =thread;
Prev->_right =root;
} prev=root; } void _prevorder (node* root) {while (root) {while (Root&&root->_letftag ==link) {cout<<
; Root->_data << "";
ROOT=ROOT->_LETF;
} cout<<root->_data << "";
Root=root->_right;
}} void _prevorderthd (node* root,node* &prev) {if (root==null) return;
if (root->_letf ==null) {Root->_letftag =thread;
ROOT->_LETF =prev;
} if (prev&& (Prev->_right ==null)) {Prev->_righttag =thread; Prev->_right =root;
} prev=root;
if (Root->_letftag ==link) _prevorderthd (ROOT->_LETF, prev);
if (Root->_righttag ==link) _prevorderthd (Root->_right, prev);
} void _inorder (node* root) {while (root) {//Find left node while (Root&&root->_letftag==link) {
ROOT=ROOT->_LETF;
} cout<<root->_data << "";
while (Root->_righttag ==thread&&root->_right) {root=root->_right;
Cout<<root->_data << "";
} root=root->_right;
}} void _inoderthd (node* root,node* &prev) {if (root==null) return; _INODERTHD (ROOT->_LETF, prev);
Zuo if (root->_letf ==null) {Root->_letftag =thread;
ROOT->_LETF =prev;
} if (Root->_right ==null) Root->_righttag =thread;
if (Prev&&prev->_righttag ==thread) prev->_right =root;
Prev=root; _INODERTHD (Root->_right, prev); You} node* _greatetree (constT *a,size_t size,size_t& index,const t& Invalid) {node* root=null;
if (a[index]!=invalid&&index<size) {root=new Node (A[index]);
Root->_letftag =link;
Root->_righttag =link;
ROOT->_LETF =_greatetree (a,size,++index,invalid);
Root->_right =_greatetree (a,size,++index,invalid);
} return root;
} private:node* _root;
};
void Testbinarytreethd () {int a[10]={1,2,3, ' # ', ' # ', 4, ' # ', ' # ', 5,6};
Binarytreethd<int> t (a,10, ' # ');
T.INORDERTHD ();
T.inorder ();
T.PREVORDERTHD ();
T.prevorder ();
T.POSTORDERTHD (); }