Binary tree creation, non-recursion and recursion before and after traversal, level traversal, number of nodes, depth, leaf node number, find a node
#include <iostream> #include <queue> #include <stack>using namespace std;template< Class t>struct binarytreenode{binarytreenode (const t&x): _data (x), _left (null), _right (null ) {}t _data; binarytreenode<t>* _left; binarytreenode<t>* _right;}; Template<class t>class binarytree{public:binarytree (): _root (NULL) {}binarytree (T* a,size_t size) {size_t index=0;_root=_createtree (a,index,size);} ~binarytree () {_destory (_root);} Void prevorder () {_prevorder (_root); Cout<<endl;} Void inorder () {_inorder (_root); Cout<<endl;} Void postorder () {_postorder (_root); Cout<<endl;} Hierarchical traversal---at room temperature so//using the team, at all, access to the root, delete root, pressure around the child Void levelorder () {queue<binarytreenode<t>*> q;if (_root! =null) Q.push (_root), while (!q.empty ()) {Binarytreenode<t>* front=q.front (); cout<<front->_ data<< " "; Q.pop (); if (front->_left!=null)//Easy Forget Q.push (Front->_left); if (Front->_riGht!=null) Q.push (front->_right);} Cout<<endl;} First-order non-recursive traversal-----> Use the stack, first, access to the root, delete root, and then press right child and left child, access to the child, delete the child Void prevorder_non_r () {Stack<binarytreenode <t>*> s;if (_root!=null) S.push (_root), while (!s.empty ()) {binarytreenode<t>* top=s.top () ;cout<<top->_data<< " "; S.pop (); if (top->_right!=null)//top is not deleted, the top of this time is empty, you can also use Top->_ Right??? S.push (top->_right); if (top->_left!=null) S.push (top->_left);} Cout<<endl;} In-sequence non-recursive traversal--------> Always press left, take the top of the stack, the pre-sequence traversal top node (then access the leftmost (top) node, delete left, the right node of the current node as the new root cur, repeat the front always press left)//All right access is through the cur//of the subtree Each node is output Void inorder_non_r () as a sub-tree {stack<binarytreenode<t>*> s; Binarytreenode<t>* cur=_root;while (cur| |! S.empty ())//easy to forget------> every time to determine whether Cur is null{while (cur!=null) {s.push (cur); cur=cur->_left;} Binarytreenode<t>* top=s.top ();cout<<top->_data<< " "; S.pop (); cur=top->_right ;} Cout<<endl;} Post-traversal non-recursive---------> Always press left, access to the left, delete left, the right child as a new root cur, always pressure left, repeat ... The key is to judge when the rootNode does not have left and right nodes, is really not, or just left and the child was deleted, that is, the child is in the stack or is going to enter, all introduced another pointer tag Void postorder_non_r () {stack<binarytreenode<t >*> s; binarytreenode<t>* cur=_root; Binarytreenode<t>* prevvisited=null;while (cur| |! S.empty ()) {while (cur!=null) {s.push (cur); cur=cur->_left;} Visited if (!s.empty ()) {binarytreenode<t>* top=s.top ();//Right subtree visited if (top->_right==null| | top->_right==prevvisited) {cout<<top->_data<< " "; S.pop (); Prevvisited=top;//cur=top->_right;} The right subtree is not visited, the current right child as the new root repeats continuously press left to traverse Else{cur=top->_right;}} Cout<<endl;} Void find (const t& x) {binarytreenode<t>* ret=_find (_root,x); if (Ret) cout<< ret->_data<<endl;elsecout<< "The data found does not exist" <<ENDL;} Void size () {cout<< "Size:" <<_size (_root) <<endl;} Void hight () {cout<< "Hight:" <<_hight (_root) <<endl;} Void leafnum () {cout<< "Leafnum:" <<_leafnum (_root) <<endl;} Protected:binarytreenode<t>*&nbSp;_root; Binarytreenode<t>* _createtree (t* _array,size_t &index,size_t size) {if (index< Size) {if (_array[index]!= ' # ') {binarytreenode<t>* root=new binarytreenode<t> (_array[index+ +]); Root->_left=_createtree (_array,index,size); Root->_right=_createtree (_array,index,size);return Root;} Else{index++;return null;}} return null;//Why you can not delete}void _destory (binarytreenode<t>* &root) {if (Root==NULL) return; if (root->_left==null&&root->_right==null) {delete root;root=null;//note return;} _destory (Root->_left); _destory (root->_right);d elete root;} Void _prevorder (binarytreenode<t>* &root) {if (root==null) return;cout<<root->_data; _prevorder (Root->_left); _prevorder (root->_right);} Void _inorder (binarytreenode<t>* &root) {if (root==null) Return;_inorder (root->_left); cout <<root->_data;_inorder (root->_right);} VOid _postorder (binarytreenode<t>* &root) {if (root==null) Return;_postorder (root->_left); _ Postorder (root->_right); cout<<root->_data;} Binarytreenode<t>* _find (binarytreenode<t>* root,const t& x) {if (Root==NULL ) return null;if (root->_data==x) return root; Binarytreenode<t>* lret=_find (root->_left,x); if (Lret) Return lret;return _find (root- >_RIGHT&NBSP;,X);} Int _size (binarytreenode<t>* &root) {if (root==null) return 0;return _size (root- >_left) +_size (root->_right) +1;} Int _hight (binarytreenode<t>* root) {if (root==null) return 0;int lefthight=_hight ( Root->_left) +1;int righthight=_hight (root->_right) +1;return lefthight>righthight?lefthight: Righthight;} Int _leafnum (binarytreenode<t>* root) {if (root==null) return 0;if ((Root->_left==NULL) && (Root->_right==null)) {return 1;} return _leafnum (Root->_left) +_leafnum (root->_right);}; Void test1 () {char array[10]={' a ', ' B ', ' d ', ' # ', ' # ', ' E ', ' # ', ' # ', ' C ', ' F '}; BINARYTREE<CHAR>&NBSP;BT1 (array,10); bt1. Prevorder (); bt1. Prevorder_non_r (); bt1. Inorder (); bt1. Inorder_non_r (); bt1. Postorder (); bt1. Postorder_non_r (); bt1. Find (' d '); bt1. Find (' z '); bt1. Levelorder (); bt1. Size (); bt1. Hight (); bt1. Leafnum ();} Int main () {Test1 (); System ("pause"); return 0;}
This article is from the "sunshine225" blog, make sure to keep this source http://10707460.blog.51cto.com/10697460/1757510
Realization of the basic ability of binary tree