Source: Internet
Author: User

Why would there be a clue to a binary tree?

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.

`int a1[10] = {n-A, ' # ', ' # ', 4, ' # ', ' # ', 5,6};`

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7F/B7/wKiom1cp9S_xj-OMAAAbVLPyZo0804.png "style=" float: none; "title=" 1.PNG "alt=" Wkiom1cp9s_xj-omaaabvlpyzo0804.png "/>650) this.width=650; src= http://s1.51cto.com/ Wyfs02/m02/7f/b5/wkiol1cp9gxz7lhoaaabbffb62m491.png "title=" 2.PNG "style=" Float:none; "alt=" Wkiol1cp9gxz7lhoaaabbffb62m491.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7F/B5/wKioL1cp9gWxO1KhAAAaUcpuyJs248.png "style=" float: none; "title=" 3.PNG "alt=" Wkiol1cp9gwxo1khaaaaucpuyjs248.png "/>

Code implementation:

`#pragma  once#include <iostream>using namespace std;enum pointertag{thread,// Threaded link,  //node};template <class t>struct binarytreenode//node {BinaryTreeNode ( Const t& data): _data (data), _left (null), _right (null), _lefttag (link), _righttag (link) {}t _data    //data binarytreenode* _left; //left child binarytreenode* _right;//right child pointertag  _lefttag;//left child clue sign pointertag _righttag;//right child clue sign};template <class t>class  BINARYTREETHD{TYPEDEF&NBSP;BINARYTREENODE&LT;T&GT;&NBSP;NODE;PUBLIC:BINARYTREETHD ()   //parameterless constructor: _root ( NULL) {}BINARYTREETHD (Const t* a,size_t size,const t& invalue)  //with the parameter constructor { &NBSP;SIZE_T&NBSP;INDEX&NBSP;=&NBSP;0;_ROOT&NBSP;=&NBSP;_BINARYTREETHD (A,size,index,invalue);} Void inorderthreading ()//sequence node* prev = null;_inorderthreading (_root,prev);} Void prevorderthreading ()//First order thread {NODE*&NBSP;PREV&NBSP; = null;_prevorderthreading (_root,prev);} Void postorderthreading ()//post-clue {node* prev = null;_postorderthreading (_root,prev);} VOID&NBSP;INORDERTHD ()//Middle sequence traversal {cout<< "in sequence traversal:" <<endl; 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;}} VOID&NBSP;PREVORDERTHD ()//First Order traversal {cout<< "First order traversal:" <<endl; Node* cur = _root;while (cur) {while (Cur->_lefttag == link) {cout<<cur->_ data<< " "; cur = cur->_left;} cout<<cur->_data<< " "; cur = cur->_right;}} VOID&NBSP;POSTORDERTHD ()//post-Traversal//{//cout<< "post-Traversal:" <<endl;//node* cur = _root;////} PUBLIC:NODE*&NBSP;_BINARYTREETHD (const t* a,size_t size,sIze_t& index,const t& invalue)//Create tree {node* root = null;if (index <  size && a[index] != invalue) {Root = new node (A[index]); root- &GT;_LEFT&NBSP;=&NBSP;_BINARYTREETHD (A,size,++index,invalue); ROOT-&GT;_RIGHT&NBSP;=&NBSP;&NBSP;_BINARYTREETHD ( A,size,++index,invalue);} Return root;} Void _inorderthreading (Node* cur,node* &prev)//middle order Thread {if (cur == null) return;_ Inorderthreading (Cur->_left,prev); if (cur->_left == null) {cur->_lefttag = thread; Cur->_left = prev;} if (prev && prev->_right == null) {prev->_righttag = thread;prev- >_right = cur;} Prev = cur;_inorderthreading (Cur->_right,prev);} Void _prevorderthreading (Node* cur,node* &prev)//First order thread {if (cur == null) return;if ( Cur->_left == null)   //Pre-order clue {CUR-&GT;_LEFTTAG&NBSP;=&NBSp Thread;cur->_left = prev;} if (prev && prev->_right == null)//post-clue {prev->_righttag = thread; Prev->_right = cur;} Prev = cur;if (Cur->_lefttag == link) {_prevorderthreading (Cur->_left,prev);} if (Cur->_righttag == link) {_prevorderthreading (Cur->_right,prev);}} Void _postorderthreading (Node* cur,node* &prev)//post-threaded {if (cur == null) return;_ Postorderthreading (Cur->_left,prev); _postorderthreading (Cur->_right,prev); if (cur->_left ==  NULL) {Cur->_lefttag = thread;cur->_left = prev;} if (prev && prev->_right == null) {prev->_righttag = thread;prev- >_right = cur;} Prev = cur;} protected:node* _root;//root node};`

Test code:

`void Test () {int a1[10] = {A. # ', ' # ', 4, ' # ', ' # ', 5,6};int a2[15] = {1, 2, ' # ', 3, ' # ', ' # ', 4, 5, ' # ', 6, ' # ', 7, ' # ', ' # ', 8}; Binarytreethd<int> B (a2,15, ' # ');//b.inorderthreading ();//b.inorderthd (); b.prevorderthreading (); b. PREVORDERTHD ();//b.postorderthreading ();}`

Pre-order test results:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7F/B7/wKiom1cp9c-jPuOjAAALWtCf2x0785.png "title=" 4.PNG " alt= "Wkiom1cp9c-jpuojaaalwtcf2x0785.png"/>

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.