Image of a binary tree

Source: Internet
Author: User

Mirror: The right tree is created from the left tree, as seen from the mirror.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/8A/wKioL1ch_xzTdUzAAAAURc2HQwA925.png "title=" QQ picture 20160428201042.png "alt=" Wkiol1ch_xztduzaaaaurc2hqwa925.png "/>

#include <iostream> #include <stack>using namespace std;struct binarytreenode{int  data; binarytreenode* left; binarytreenode* right; Binarytreenode (int x):d ata (x),  left (null),  right (null) {}};class binarytree{protected: binarytreenode* _root; Binarytreenode* _createbinarytree (int* arr, int& index, int size) { binarytreenode* root = null;if  (index<size&&arr[index] !=  ' # ') {root  = new binarytreenode (Arr[index]); Root->left = _createbinarytree (Arr, ++index ,  size); Root->right = _createbinarytree (arr, ++index, size);} Return root;} Binarytreenode* _copymirrorint (Binarytreenode* root) {if  (root == null) return  NULL; Binarytreenode* tmp = new binarytreenode (root->data); tmp->left = _ Copymirrorint (root->right); Tmp->right= _copymirroRint (root->left); return tmp;} Public:binarytree (): _root (NULL) {}binarytree (int *arr, int size) {int index = 0;_ Root = _createbinarytree (arr, index, size);} Void preorder_non () {if  (_root == null) return; Binarytreenode* cur = _root;stack<binarytreenode*> s;s.push (_root);while  (! S.empty ()) {cur= s.top ();p rintf ("%d ",  cur->data); S.pop ();if  (cur->right) S.push (cur- >right);if  (cur->left) s.push (cur->left);} Cout << endl;} Void inorder_non () {if  (_root == null) return;stack<binarytreenode*> s; binarytreenode* cur = _root;while  (cur | |  !s.empty ()) {while  (cur) {s.push (cur); cur = cur->left;} if  (!s.empty ()) {cout << s.top ()->data <<  " ";cur =  S.top ()->right;s.pop ();}} Cout << endl;} Void postordeR_non () {if  (_root == null) return;stack<binarytreenode*> s; binarytreenode* cur = _root; binarytreenode* prev = null; binarytreenode* top = null;while  (cur | |  !s.empty ()) {while  (cur) {s.push (cur); cur = cur->left;} Top = s.top ();if  (top->right == null | |  top->right == prev) {cout << top->data <<  " "; S.pop ( );p Rev = top;} Else{cur = top->right;}} Cout << endl;} Void  copymirroring (binarytree& b) {_root = _copymirrorint (b._root);}}; Void test1 () {int arr[] = { 1, 2, 4,  ' # ',  ' # ', 5,  ' # ',   ' # ', 3, 6,  ' # ',  ' # ', 7 }; BINARYTREE&NBSP;BT1 (arr, sizeof (arr)  / sizeof (arr[0])); Binarytree bt2;bt1. Inorder_non (); bt1. Postorder_non (); bt2. Copymirroring (bT1); Bt2. Postorder_non ();}


This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1768768

Image of a 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.