Data structure--two-tree (1)

Source: Internet
Author: User

Two-fork Tree

Build: The construction of the two fork tree uses the first order traversal,-> to store the root node and then the left and right nodes, using recursive thought to put all the data in the tree.

Code implementation: 4 Kinds of access methods are implemented, first order, middle order, sequence, and sequence access methods are recursive.

#include <iostream> #include <queue> #include <stack>using namespace std;template< Class t> struct rootnode{t _value;rootnode<t> *_leftnode;rootnode<t>  *_rightnode;rootnode<T> (T value):  _value (value), _leftnode (null), _rightnode (null) {}}; Template <class t>class binarytree{public:binarytree<t> ( T *str) {T * Tmp = str;_root = _binarytree (TMP);} ~binarytree () {_clear (_root);} binarytree<t>  (binarytree &t) {_root=_copy (t._root);} Binarytree<t>& operator= (binarytree t) {if  (*this != t) {swap (_root, t._ root);}} Void fastorder () {_fastorder (_root);} Void inorder () {_inorder (_root);} Void postorder () {_postorder (_root);} Void levelorder () {queue<rootnode<t>*> q;if  (_root == null) {return;} Q.push (_root);while  (!q.empty ()) {Rootnode<t>* root&nbsP;= q.front (); Cout << root->_value;q.pop ();if  (root->_leftnode !=  NULL) {Q.push (root->_leftnode);} if  (root->_rightnode != null) {Q.push (Root->_rightnode);}}} Int leafnum () {int num = 0;num=_leafnum (_root,num); return num;} Int size () {int size = 0;_size (_root,size); return size;} Int depth () {int depth = _depth (_root); return depth;} Void nrfastorder () {stack<rootnode<t>*> s;if  (_root != null) {S.push (_root) ;} while  (!s.empty ()) {rootnode<t>* front=s.top (); Cout<<front->_value;s.pop ();if  ( Front->_rightnode != null) {s.push (front->_rightnode);} if  (front->_leftnode != null) {s.push (Front->_leftnode);}}} Void nrinorder () {stack<rootnode<t>*>s;rootnode<t>*cur = _root;rootnode<t >* top = NULL;while  (cur| |! S.empty ()) {while  (cur) {s.push (cur); cur = cur->_leftnode;} if  (Top != s.top ()->_rightnode) {top = s.top (); cout << top->_ Value;s.pop (); cur = top->_rightnode;} Else{top = s.top (); Cout << top->_value;s.pop ();}}} Void nrpostorder () {rootnode<t>*cur = _root;stack<rootnode<t>*> s; rootnode<t>*top = null;while  (cur | |  !s.empty ()) {while  (cur) {s.push (cur); cur = cur->_leftnode;} if  (S.top ()->_rightnode != null&&top != s.top ()->_rightnode) {top =  s.top (); cur = top->_rightnode;} Else{top = s.top (); S.pop (); cout << top->_value;}}} Protected:rootnode<t>* _binarytree (T&NBSP;*&AMP;STR) {rootnode<t> *root = null; if  (*str !=  ' # &&*str !=  ') {root = new rootnode< T> (*STR); Str++;root->_leftnode = _binarytree (str); str++;root->_rightnode = _ BinaryTree (str);} Return root;} Void _fastorder (Rootnode<t> *&root) {if  (root == null) {return;} Else{cout << root->_value;_fastorder (Root->_leftnode); _fastorder (Root->_rightnode);}} Void _inorder (Rootnode<t> *root) {if  (root == null) {return;} _inorder (Root->_leftnode); Cout << root->_value;_inorder (Root->_rightnode);} Void _postorder (Rootnode<t> *root) {if  (root == null) {return;} _postorder (Root->_leftnode); _postorder (root->_rightnode); cout << root->_value;} Void _clear (Rootnode<t> *root) {if  (root == null) {return;} Rootnode<t> *tmp = root->_leftnode;rootnode<t> *tmp2 = root->_ Rightnode;delete root;_clear (TMP); _clear (TMP2);} Rootnode<t&gT;* _copy (Rootnode<t> *root) {rootnode<t> *newroot = null;if  (root  == null) {return newroot;} Newroot = new rootnode<t> (Root->_value); Newroot->_leftnode = _copy (root- >_leftnode); newroot->_rightnode = _copy (root->_rightnode); return newroot;} Int _size (rootnode<t> *root,int &size) {if  (root == null) {return 0 ;} Size++;_size (root->_leftnode,size); _size (root->_rightnode,size); return size;} Int _depth (Rootnode<t> *root) {if  (root==null) {return 0;} Int hight = 1;int left = 0;int right = 0;left += _depth (Root->_leftnode)  + hight;right += _depth (Root->_rightnode)  + hight;if   (left > right) {return left;} Else{return right;}} Int _leafnum (rootnode<t>* root,int &num) {IF&NBSP; (root == null) {return 0;} if  (root->_leftnode == null&&root->_rightnode == null) {num++;} _leafnum (Root->_leftnode, num); _leafnum (root->_rightnode, num); return num;} private:rootnode<t> *_root;}; Void test1 () {char *str =  "123# #45 # #6 # #78 # # #"; BINARYTREE&LT;CHAR&GT;&NBSP;B1 (str); BINARYTREE&LT;CHAR&GT;&NBSP;B2 (B1); Binarytree<char> b3 = b2;b1. Fastorder (); cout << endl;b1. Inorder (); cout << endl;b1. Postorder (); cout << endl;b2. Fastorder (); cout << endl;b3. Fastorder (); cout << endl;cout << b3. Size () <<endl;cout << b3. Depth ()  << endl;b3. Levelorder (); Cout << endl;cout << b3.leafnum () <<endl;} Int main () {        test1 ();}


This article is from the "Traces" blog, be sure to keep this source http://wpfbcr.blog.51cto.com/10696766/1760648

Data structure--two-tree (1)

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.