"C + +" Second clue binary tree and its traversal __c++

Source: Internet
Author: User

#include <iostream> using namespace std;
Enum Pointtag {LINK, THREAD,};
	struct Bintreethingnode {char _data;
	Bintreethingnode*_left;
	Bintreethingnode*_right;
	Pointtag _lefttag;
	Pointtag _righttag; Bintreethingnode (const char &data): _data (data), _left (null), _right (null), _lefttag (LINK), _righttag (LI

NK) {}};
	
	Class Bintreethreading {public:bintreethreading (const char *str) {_creatbintreethreading (_ROOT,STR);//recursively Create two fork tree}
		void Postthread ()//subsequent clue {bintreethingnode*prev = NULL;
	_postoderthread (_root, prev);
		}//subsequent threaded traversal of void Postsort () {bintreethingnode*cur = _root;
		Bintreethingnode *lastvisited = NULL; while (cur) {//always looking for the leftmost leaf node of the two-fork tree cur as the root node//cur->_left!= lastvisited: When cur is found through its left child, do not repeat cur to find the leftmost leaf node W
			Hile (Cur&&cur->_lefttag!= thread&& cur->_left!= lastvisited) {cur = cur->_left;
			if (cur && cur->_righttag = THREAD)///When Cur has no right child, cur can be accessed {	cout << cur->_data << "";
			lastvisited = cur;
				} if (cur = = _root&&cur->_right = NULL)//When the root node is accessed, the traversal completes {cout << cur->_data << "";
			Return


			} cur = cur->_right; When Cur's right child is visited, cur can be accessed. And you know cur no successor, so start looking for the root node of cur while (cur && cur->_right = = lastvisited) {cout << Cur->_data <
				< "";
				lastvisited = cur; _parent (_root, &cur);
				Find the root node of cur if (cur = = lastvisited) {return;  }//while processing is: When Cur is found through its right child} protected:///recursively create two-fork tree void _creatbintreethreading (Bintreethingnode*&root,
			const char *&AMP;STR {if (*str!= ' # ' &&*str!= ' ") {root = new Bintreethingnode (*STR);
			_creatbintreethreading (Root->_left, ++STR);
			if (*str!= ' ") {_creatbintreethreading (root->_right, ++STR); Find the root node of the current node void _parent (Bintreethingnode *&root,bintreethingnode **child) {Bintreethingnode *tmp = *ch Ild;
		if (*child = = root) {return;
				} if (root) {if (Root->_left = = *child | | root->_right = = *child) {*child = root;
			Return
			} if (Root->_lefttag = LINK) {_parent (root->_left, child);
			} if (tmp = = *child&&root->_righttag = LINK) {_parent (root->_right, child); implementation void _postoderthread (Bintreethingnode*&cur, Bintreethingnode*&prev) {if (cur) {i
			F (Cur->_lefttag = LINK) {_postoderthread (cur->_left, prev);
			} if (Cur->_righttag = LINK) {_postoderthread (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;
}} Private:bintreethingnode*_root;
 };


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.