# "Data Structure" the recursive and non-recursive notation of sequential cues in binary tree

Source: Internet
Author: User

The two-fork tree is a nonlinear structure, and the traversal of the binary tree is accomplished by recursive or non-recursive traversal with the aid of a stack. 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. So the clue binary tree is introduced. Let's take a look at two implementations of sequential cues in a threaded binary tree:

(1). Recursive implementation of sequence-threaded binary tree

First, let's look at the structure of the threaded binary tree.

`Enum pointertag{THREAD, LINK};template<class t>struct binarytreethdnode{binarytreethdnode<t>* _left;    binarytreethdnode<t>* _right;    Pointertag _lefttag;    Pointertag _righttag;    T _data; Binarytreethdnode (const t& x): _left (null), _right (null), _lefttag (LINK), _righttag (LIN K), _data (x) {}};`

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/52/wKioL1cZ7PPiwAYlAAAKeTZhmII624.png "title=" ll.png "alt=" Wkiol1cz7ppiwaylaaaketzhmii624.png "/>

In order to realize the sequential clue of binary tree with recursion, there are two main places to be noticed.

first of all, let's go to the left. However, consider the condition of recursion, only when the left and right identifiers are thread, we can recursive, otherwise it will be infinite loop, because the left and right nodes will point to the previous node and the latter node, thus triggering infinite recursion. This can be tested on your own.

another point to be aware of is the need to analyze the problem when you encounter, that is, when our right node is empty, we need to connect the right node to the previous layer of recursive node up. Since it is recursive down, then the previous layer is equivalent to the future, we are unpredictable, then how can we connect the right side to the previous layer of the node up?

Hey, since we can not go from now to the future, then to the future, we will do this again! Let's remember this time node and remember him as a prev. When we go to his upper node, we first determine if Prev is null and determine if he needs a thread (a clue). If it is, then the right point of the saved Prev node to the current node is OK.

then recursively to the right, which completes the clue to the binary tree.

`Template<class t>class binarytreethd{    typedef binarytreethdnode &LT;T&GT;&NBSP;NODE;PUBLIC:&NBSP;&NBSP;&NBSP;&NBSP;BINARYTREETHD (int* a, size_t size,  Const t& invalid)     {        size_t &NBSP;INDEX&NBSP;=&NBSP;0;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;_ROOT&NBSP;=&NBSP;_CREATETREETHD (A,  size, invalid,index);     }        node* &NBSP;_CREATETREETHD (int* a, size_t size, const t& invalid, size_t&  index)     {        node* root =  NULL;        if  (index < size &&  a[index] != invalid)         {              root = new node (A[index]);        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ROOT-&GT;_LEFT&NBSP;=&NBSP;_CREATETREETHD (a, size, invalid, ++ index);             root->_right = _ CREATETREETHD (a, size, invalid, ++index);         }         return root;    }     //using recursion to realize the clue of Binary Tree     void inorderthreading ()     {         Node* prev = NULL;         _inorderthreading (_root,prev);    }         protected:    //recursive implementation of a threaded binary tree     void _inorderthreading (Node*  root,node* prev)     {        if  (root == null)              return;         if  (Root->_lefttag == link)         _ Inorderthreading (Root->_left, prev);        if  (root-> _left == null)         {             root->_left = prev;             root->_leftTag = THREAD;         }        if  (root->_right == null)          {             root->_righttag = thread;        }         if  (Prev != null && prev->_righttag == thread)          {             prev->_right = root;        }         prev = root;        if  (root- >_righttag == link)         _inorderthreading (root->_ Right, prev);     }`

(2). Using the stack to realize the sequential clue binary tree

Using the stack to achieve the clue of the binary tree is not recursive so abstract, the idea is similar to recursion, but we no longer need to consider the last layer of access to the problem, because the stack to the top of the stack can know the nodes of the previous layer. In this way, you can understand it by drawing it yourself. I'll just give the code here.

`/* Stack-threaded binary tree */    void inorderthreading ()     {         stack<node*> s;        node*  prev = NULL;        Node* cur = _root;         while  (cur | |  !s.empty ())         {             while  (cur)              {                 S.push (cur);                 cur = cur->_left;            }            &nbsP;cur = s.top ();             s.pop ();             if  (cur->_left ==  NULL)             {                 cur->_left = prev;                 cur->_lefttag =  THREAD;            }             prev = cur;             if  (Cur->_right == null && !s.empty ())             {           &nbsP;     cur->_right = s.top ();                 cur->_rightTag = THREAD;                 cur = NULL;             }             else            {                 cur =  cur->_right;            }         }    }`

Plus two printing methods:

(1). Recursive printing

`Void inorder ()     {        _inorder (_root);         cout << endl;    }           //recursive printing of threaded binary tree     void _inorder ( Node* root)     {        if  (root ==  null)             return;         if  (Root->_lefttag == link)              _inorder (Root->_left);         cout << root->_data <<  " ";         if  (Root->_righttag == link)         {     &nbSp;       _inorder (root->_right);         }        }`

(2). Print with stack

`Void inorder ()     {        if  (_root  == null)             return;         Node* cur = _root;         while  (cur)         {             while  (Cur->_left)              {                 cur = cur->_left;             }            cut <<  cur->_data <<  " ";             if  (cur->_rightTag == thread)             {                 cout <<  cur->_right->_data <<  " ";                 cur = cur->_right;             }             else if  (Cur->_right == link)              {                             }         }    }`

"Data Structure" the recursive and non-recursive notation of sequential cues in binary tree

Related Keywords:

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.