C + + implementation two fork tree

Source: Internet
Author: User

#include  <iostream>using namespace std; #include  <queue> #include  <stack >template<class T>struct BinaryTreeNode{BinaryTreeNode<T>* _left; binarytreenode<t>* _right; t _data; Binarytreenode (const t& x): _left (null), _right (null), _data (x) {}};template<class t> Class binarytree{public:binarytree (): _root (NULL) {}binarytree (const t* a, size_t size,  const t& invalid) {Size_t index = 0;_root = _createtree (a,  Size, index, invalid);} ~binarytree () {_destroy (_root); _root = null;} BinaryTree (const binarytree<t>& t) {_root = _copy (t._root);} Assignment operator Overload binarytree<t>& operator= (const binarytree& t) {if  (this !=  &t) {_destroy (_root); _root = _copy (t._root);} Return *this;} Recursive pre-order traversal Void preordertraverse () {_preordertraverse (_root); Cout<<endl;} Recursive middle order Traversal void inordertraverse () {_inordertraverse (_root); Cout<<endl;} Recursive sequential traversal void postordertraverse () {_postordertraverse (_root); Cout<<endl;} Sequence Traversal void levelordertraverse () {queue<binarytreenode<t>*> q;if  (_root) {Q.push (_root );} while  (!q.empty ()) {Binarytreenode<t>* front = q.front (); Q.pop (); cout<<front- >_data<< " ";if  (front->_left) {Q.push (front->_left);} if  (front->_right) {Q.push (front->_right);}} Cout<<endl;} Non-recursive pre-order traversal Void preordertraverse_nonr () {stack<binarytreenode<t>*> s;if  (_root) {S.push (_root);} while  (!s.empty ()) {binarytreenode<t>* top = s.top (); S.pop (); cout<<top->_ data<< " ";if  (top->_right) {s.push (top->_right);} if  (top->_left) {s.push (top->_left);}} Cout<<endl;} Non-recursive mid-order traversal VOID&NBSP;INORDERTRAVERSE_NONR () {stack<binarytreenode<t>*> S binarytreenode<t>* cur = _root;while  (cur | |  !s.empty ()) {while  (cur) {s.push (cur); cur = cur->_left;} if  (!s.empty ()) {binarytreenode<t>* top = s.top ();cout<<top->_data<< "   "; S.pop (); cur = top->_right;}} Cout<<endl;} Non-recursive post-traversal void postordertraverse_nonr () {stack<binarytreenode<t>*> s; binarytreenode<t>* cur = _root; binarytreenode<t>* previsted = null;while  (cur | |  !s.empty ()) {while  (cur) {s.push (cur); cur = cur->_left;} Binarytreenode<t>* top = s.top ();if  (null == top->_right| |  previsted == top->_right) {cout<<top->_data<< " ";p revisted =  Top;s.pop ();} Else{cur = top->_right;}} Cout<<endl;} Node Count Size_t size () {return _size (_root);} Depth size_t depth () {return _depth (_rOOT);} Leaf knot Point Size_t leafsize () {size_t size = 0;_getleafsize (_root, size); return size;} PROTECTED://Structure two fork tree Binarytreenode<t>* _createtree (const t* a, size_t size,  Size_t& index, const t& invalid) {binarytreenode<t>* root =  null;if  (a[index] != invalid && index < size) {root =  New binarytreenode<t> (A[index]); Root->_left = _createtree (A, size, ++index,  invalid); Root->_right = _createtree (a, size, ++index, invalid);} Return root;} Destroy Void _destroy (binarytreenode<t>*& root) {if  (null == root) {return;} if  (null == root->_left && null == root->_right) {delete  Root;root = null;return;} _destroy (Root->_left); _destroy (root->_right);d elete root;} Copy BinarytreeNode<t>* _copy (Binarytreenode<t>* root) {if  (null == root) {return  NULL;} Binarytreenode<t>* newroot = new binarytreenode<t> (root->_data); newRoot- >_left = _copy (Root->_left); Newroot->_right = _copy (root->_right);return  Newroot;} Recursive pre-order traversal Void _preordertraverse (binarytreenode<t>* root) {if  (null == root) { return;} cout<<root->_data<< " "; _preordertraverse (Root->_left); _preordertraverse (root->_right );} Recursive middle order Traversal void _inordertraverse (binarytreenode<t>* root) {if  (null == root) {return ;} _inordertraverse (Root->_left);cout<<root->_data<< " "; _inordertraverse (root->_right);} Recursive post-post traversal void _postordertraverse (binarytreenode<t>* root) {if  (null == root) { return;} _postordertraverse (Root->_left); _postordertraverse (root->_right); cout<<root->_data<< " ";} Node Size_t _size (binarytreenode<t>* root) {if  (root == null) {return 0;} Return _size (Root->_left)  + _size (root->_right)  + 1;} Depth size_t _depth (binarytreenode<t>* root) {if  (root == null) {return 0;} Size_t leftdepth = _depth (Root->_left); Size_t rightdepth = _depth (root->_ right); return leftdepth < rightdepth ? leftdepth+1 : rightdepth+1;} Leaf knot point Void _getleafsize (binarytreenode<t>* root, size_t& size) {if  (NULL  == root) {return;} if  (null == root->_left && null == root->_right) {++size; return;} _getleafsize (root->_left, size); _getleafsize (root->_right, size);} protected:binarytreenode<t>* _root;}; Void test () {int a[] = {1, 2, 3,  ' # ',  ' # ',  4,&nbsP; ' # ',  ' # ', 5, 6}; Binarytree<int> t (A, sizeof (a)/sizeof (a[0]),  ' # ');cout<< "pre-sequence Traversal:"; T.preordertraverse ( ;cout<< "sequence traversal:"; T.inordertraverse ();cout<< "post-order traversal:"; T.postordertraverse ();cout<< "sequence traversal:"; t. Levelordertraverse ();cout<< "non-recursive pre-order traversal:"; T.preordertraverse_nonr ();cout<< "non-recursive sequential traversal:"; t.inordertraverse _nonr ();cout<< "non-recursive post-traversal:"; T.postordertraverse_nonr ();cout<< "size = " <<t.size () < <endl;cout<< "depth = " <<t.depth () <<endl;cout<< "leafsize = " < <t.leafsize () <<endl; Binarytree<int> t2 (t);cout<< "T2:"; T2. Preordertraverse (); binarytree<int> t3;t3 = t2;cout<< "T3:"; T3. Preordertraverse ();} Int main () {Test (); return 0;}

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/80/C0/wKiom1dEB8DDLlRqAABiA0kUnyM412.png "title=" Qq20160524154720.png "alt=" Wkiom1deb8ddllrqaabia0kunym412.png "/>

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

C + + implementation two fork 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.