C + + threaded binary tree

Source: Internet
Author: User

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://s3.51cto.com/wyfs02/M01/82/0E/wKioL1dJaYvy4CzKAADO5L3WcMo754.png "title=" Binarytreethreading.png "alt=" Wkiol1djayvy4czkaado5l3wcmo754.png "/>


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/82/0F/wKiom1dJaKyCjicSAAB_o7MVcQQ125.png "title=" Inorderthreading.png "alt=" Wkiom1djakycjicsaab_o7mvcqq125.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/82/0E/wKioL1dJabTR4wZdAACIOhdRSBI843.png "title=" Preorderthreading.png "alt=" Wkiol1djabtr4wzdaaciohdrsbi843.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/82/0E/wKioL1dJacPQFETTAACCmjZ2nd4431.png "title=" Postorderthreading.png "alt=" Wkiol1djacpqfettaaccmjz2nd4431.png "/>

#include  <iostream>using namespace std;enum PointerTag {THREAD, LINK}; template<class t>struct binarytreenodethd{t _data;                       // Data binarytreenodethd<t>* _left;  //left child binarytreenodethd<t>* _right; // Right child pointertag _lefttag;          //left child clue sign    pointertag _righttag;         //right child clue sign    BINARYTREENODETHD (Const t& data): _data (data), _left (null), _right (null), _lefttag (LINK), _righttag ( LINK) {}};TEMPLATE&LT;CLASS&NBSP;T&GT;CLASS&NBSP;BINARYTREETHD{PUBLIC:BINARYTREETHD (Const T* array,  size_t size, const t& invalid) {size_t index = 0;_root =  _createtree (Array, size, index,&nbsP;invalid);} ~BINARYTREETHD () {_destroytree (_root); _root = null;} Void inorderthreading () {binarytreenodethd<t>* prev = null;_inorderthreading (_root,  prev);} Void preorderthreading () {binarytreenodethd<t>* prev = null;_preorderthreading (_root,  prev);} Void postorderthreading () {binarytreenodethd<t>* prev = null;_postorderthreading (_ Root, prev);} VOID&NBSP;PREORDERTHD () {binarytreenodethd<t>* cur = _root;while  (cur) {while  ( Cur && link == cur->_lefttag) {cout<<cur->_data<< " "; cur  = cur->_left;} cout<<cur->_data<< " "; cur = cur->_right;} Cout<<endl;} VOID&NBSP;INORDERTHD () {binarytreenodethd<t>* cur = _root;while  (cur) {while  (cur  && link == cur->_lefttag) {cur = cur->_left;} Cout<<cur->_data<< " ";while  (Thread == cur->_righttag) {cur = cur->_right;cout< <cur->_data<< " ";} Cur = cur->_right;} Cout<<endl;} Protected:binarytreenodethd<t>* _createtree (const t* array, size_t size,  Size_t& index, const t& invalid) {binarytreenodethd<t>* root =  NULL;if  (index < size && array[index] != invalid) {root  = new BinaryTreeNodeThd<T> (Array[index]); Root->_left = _createtree (Array,  size, ++index, invalid); Root->_right = _createtree (Array, size, ++index ,  invalid);} Return root;} Void _destroytree (Binarytreenodethd<t>* root) {if  (null == root) return;if  ( Link == root->_lefttag) _destroytree (root->_left);if  (Link == root->_righttag ) _destroytree (Root->_right);d Elete root;} Void _preorderthreading (binarytreenodethd<t>* cur, binarytreenodethd<t>*&  prev) {if  (null == cur) return;if  (null == cur->_left) {cur->_leftTag =  thread;cur->_left = prev;} if  (prev && null == prev->_right) {prev->_righttag = thread; Prev->_right = cur;} prev = cur;if  (Cur->_lefttag == link) _preorderthreading (Cur->_left, prev); if  (Cur->_righttag == link) _preorderthreading (Cur->_right, prev);} Void _inorderthreading (binarytreenodethd<t>* cur, binarytreenodethd<t>*&  prev) {if  (null == cur) return;_inorderthreading (Cur->_left, prev);if  (NULL ==  cur->_left) {Cur->_lefttag = thread;cur->_left = prev;} if  (prev && null == p rev->_right) {prev->_righttag = thread;prev->_right = cur;} Prev = cur;_inorderthreading (Cur->_right, prev);} Void _postorderthreading (binarytreenodethd<t>* cur, binarytreenodethd<t>*&  prev) {if  (null == cur) return;_postorderthreading (Cur->_left, prev); _ Postorderthreading (Cur->_right, prev);if  (cur->_left == null) {cur->_leftTag  = thread;cur->_left = prev;} if  (prev && null == prev->_right) {prev->_righttag = thread; Prev->_right = cur;} Prev = cur;} protected:binarytreenodethd<t>* _root;}; Void test () {int a[] = {1, 2, 3,  ' # ',  ' # ', 4,  ' # ',  ' # ',  5, 6}; Binarytreethd<int> t1 (A, sizeof (a)/sizeof (a[0]),  ' # '); t1. Preorderthreading (); T1. PREORDERTHD (); Binarytreethd<int> t2 (A, sizeof (a)/sizeof (a[0]),  ' # '); T2. Inorderthreading (); T2. INORDERTHD ();int a1[] = {1, 2,  ' # ', 3,  ' # ',  ' # ', 4, 5,  ' # ', 6,  ' # ', 7,  ' # ',  ' # ', 8}; Binarytreethd<int> t3 (a1, sizeof (A1)/sizeof (A1[0]),  ' # '); T3. Preorderthreading (); T3. PREORDERTHD ();} Int main () {Test (); return 0;}


This article is from the "zgw285763054" blog, make sure to keep this source http://zgw285763054.blog.51cto.com/11591804/1784096

C + + threaded binary tree

Contact Us

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.