Binary tree is a common data structure, and here we need to note that the binary tree of the non-recursive traversal.
First order traversal, middle sequence traversal, post-order traversal
These three kinds of traversal, if implemented in a non-recursive way, we need to use the structure of the stack, first we need to traverse the left side of all the left subtree. After the pressure stack, after the completion of the stack, according to different needs, determine whether to continue to access or eject or press into the right subtree of the node.
Sequence traversal
Unlike other traversal methods, the sequence traversal begins with the root node, down in turn, and each layer is accessed from left to right.
Here we need to use the data structure of the queue, layers of the team, layers out of the line, complete the traversal.
The code is as follows:
#pragma once#include<iostream>using namespace std; #include <queue> #include <stack >typedef char datatype;struct binarytreenode//node struct {binarytreenode (DataType data = (DataType) 0): _data (data), _leftchild (null), _rightchild (null) {}datatype _data; binarytreenode* _leftchild; binarytreenode* _rightchild;}; Class binarytree{typedef binarytreenode _node;public:binarytree (CHAR*&NBSP;STR)//Construct two tree by first order string # ' is empty {_root = _creattree (_ROOT,STR);} BinaryTree (const binarytree &t) {_root = _copy (t._root);} Binarytree operator = (binarytree t) {swap (_root, t._root); return *this;} ~binarytree () {_destory (_root);} Size_t size ()//Find the size of the binary tree {return _size (_root);} Size_t depth ()//Seek depth {return _depth (_root);} Void levelorder ()//sequence traversal binary tree {queue<_node*> nodequeue;_node* cur = _root; Nodequeue.push (cur);while (! NodequEue.empty ()) {_node*tmp = nodequeue.front ();//Take Team head cout << tmp->_data << " ";//Access Nodequeue.pop ();if (tmp->_leftchild)//left not empty into left, right not empty into right nodequeue.push (tmp->_ Leftchild);if (tmp->_rightchild) Nodequeue.push (Tmp->_rightchild);}} Void backorder_nonrec ()//subsequent non-recursive traversal {stack<_node*> s;_node*prev = null;_node*cur = _root;while (!s.empty () | | cur)//press the left sub-tree of a tree until the leftmost {while (cur) {s.push (cur); cur = cur->_leftchild;} _node* top = s.top ();if (top->_rightchild==null| | Top->_rightchild==prev)//If the right node of the top node of the stack is empty, or if it is a node that has already been accessed, the top node {visitor (top);p is not popped rev = top;// Save the last visited node S.pop ();} else//otherwise presses into the left subtree of the root with the right node of the top section of the stack, until the leftmost {cur = top->_rightchild;}} Void inorder_nonrec ()//middle order non-recursive traversal {stack<_node*> s;_node*cur = _root;while (! S.empty () | | cur) {while (cur)//press the left node of a tree until the leftmost, if empty then do not press stack {s.push (cur);cur = cur->_leftchild;}_node* top = s.top ();if (!s.empty ())//accesses the top node of the stack, placing another pressed tree, which is the right subtree {visitor (top) of the top node of the stack; s.pop (); cur = top->_rightchild;}}} Void prevorder_nonrec ()//First order non-recursive traversal {Stack<_node*> s;_node* cur = null;s.push (_root );while (!s.empty ()) {cur = s.top ();//first access the current node visitor (cur); S.pop ();if (cur->_rightchild)/ /The current right node is not empty-pressed into S.push (cur->_rightchild);if (cur->_leftchild) s.push (cur->_leftchild);} Cout << endl;} Protected:static void visitor (_node* cur)//Access function, in order to satisfy the test, the console prints the data {cout << cur-> _data << " ";} _node* _copy (_node* root) {_node* newroot = null;if (Root == NULL) Return null;else{newroot = new _node (root->_data); newroot->_leftchild = _ Copy (Root->_leftchild); newroot->_rightchild = _copy (root->_rightchild);} Return newroot;} Size_t _depth (_node* root) {size_t depth = 0;if (root == null) return depth;else{depth = _depth (root->_leftchild) + 1;size_t newdepth = _depth (root->_rightchild) + 1;depth = depth > newdepth ? depth : newdepth;} Return depth;} Size_t _size (_node* root) {if (root==null) return 0;elsereturn _size (root->_ Leftchild) + _size (Root->_rightchild) + 1;} Void _destory (_node* &root) {if (Root) {if (root->_leftchild == null) & & (Root->_rightchild == null)) {delete root;root = null;} Else{_destory (Root->_leftchild); _destory (Root->_rightchild); _destory (root);}} _node*_creattree (_NODE*&NBSP;ROOT,CHAR*&NBSP;&STR) {_node*cur = root;if (*str == ' # ' | | *str == 0) {return null;} Else{cur = new _node (*STR); cur->_leftchild&nbSp;= _creattree (CUR->_LEFTCHILD,&NBSP;++STR); Cur->_rightchild = _creattree (cur->_ RIGHTCHILD,++STR);} Return cur;} protected:_node* _root;};
If there is any deficiency or doubt, I hope to correct.
This article is from the "Pawnsir It Road" blog, so be sure to keep this source http://10743407.blog.51cto.com/10733407/1766937
"Code" C + + implements two fork tree basic operations and test cases