Binary tree creation and recursive and non-recursive traversal

Source: Internet
Author: User

Binary tree First order traversal; (1) First Order access root node (2) First order access Zuozi (3) First order access right subtree

binary tree sequence traversal; (1) Middle sequence access root node (2) Sequence access Zuozi (3) middle order access right subtree

Binary Tree post-post traversal; (1) post-secondary Access root node (2) post-visit Zuozi (3) post-visit right sub-tree

Test case: Int a[10]={' 1 ', ' 2 ', ' 3 ', ' # ', ' # ', ' 4 ', ' # ', ' # ', ' 5 ', ' 6 '}

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7F/FC/wKiom1czMXCx_nz5AAB0jqGXn5E099.jpg "title=" Yypdf2016511211832.jpg "alt=" Wkiom1czmxcx_nz5aab0jqgxn5e099.jpg "/>

Code:

#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& d)         :_left (NULL)         ,_right (NULL)          _data (d)     {}};template<class T>class BinaryTree{public:     binarytree ()         :_root (NULL)     {}     binarytree (const t* a,size_t size,const t& invalid)      {        size_t index = 0;      &nbSp;  _root = _create (a,size,index,invalid);    }     //binarytree (const binarytree<t>& d)     //{     //    binarytreenode<t> root = null;    //}     BinaryTree<T>& operator =  (const binarytree<t> &AMP;&NBSP;D)     {        swap (root,d._root ) ;     }    void prevorder ()     {        _prevorder (_root);    }    void  Inorder ()     {       _inorder (_root);     }    size_t size ()     {         _size (_root), &Nbsp;   }    size_t depth ()     {         return _depth (_root);     }    size_ T leafsize ()     {        return _ Leafsize ( _root);     }    void levelorder ()     {        _leavelorder ();   }     void prevorder_nonr ()     {        _ Prevorder_nonr ();     }    void inorder_nonr ()      {        _inorer_nonr ();    }     void postorder_nonr ()     {         _postorder_nonr ();   &nbSp; }public:protected:   binarytreenode<t>* _create (const T*a,size_t  Size,size_t& index,const t& invalid)    {        BinaryTreeNode<T> *root = NULL;        while (Index<size && a[index] != invalid)         {           root = new  binarytreenode<t>  (A[index]);            Root->_left = _create (a,size,++index,invalid);            root->_right = _create (a,size,++index,invalid);        }       return 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 );    }   size_t _size ( Binarytreenode<t>* root)    {        if (root  == null)         {             return 0;        }         return _size (root->_left ) +_size (root->_right ) +1;    }   size_t _depth (binarytreenode<t>* root)    {       if (root == null)       {         return 0;      }       int left = _depth (root->_left ) +1;      int right = _ depth  (root->_right ) +1;      return  (left>right?left:right );    }   size_t _leafsize (Binarytreenode<t>* root)     {        if (root == null)          {            return 0 ;         }        if (root-> _left == null &&  (root->_right == null))          {            return 1 ;        }        return  _leafsize (Root->_left) +_leafsize  (root->_right);    }    void _leavelorder ()      {        queue<BinaryTreeNode<T>*>q;         if (_root)         {             q.push (_root);         }        while (!q.empty ())          {             Binarytreenode<t>* front = q.front ();             cout<<front._data<< " ";             if (_root->_left)              {    &Nbsp;           q.push (_root->_left);             }             if (_root->_right)              {                q.push ( _root->_right);            }             q.pop ();         }        cout<<endl;    }     void _prevorder_nonr ()     {         Stack<binarytreenode<t>*>s;        binarytreenode<t> * cur =&nbSp;_root;        while (cur| |! S.empty ())         {                         while (cur  )             {                 cout<<cur->_data << "   ";                  S.push (cur);                 cur = cur->_left ;            }             if (!s.empty ())              {                binarytreenode<t>*  Top = s.top ();                 cur = top->_right ;                 s.pop ();             }                     }    }    void _inorer_nonr ()      {        stack<BinaryTreeNode<T>*> s;         BinaryTreeNode<T>* cur = _root;         while (cur| |! S.empty ()) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;       while (cur)              {                S.push (cur);                cur  = cur->_left ;            }             binarytreenode<t>* top  = s.top ();             cout<<top- >_data<< " ";                         cur = Top->_right ;             s.pop ();         }   &nBsp;    cout<<endl;    }    void _ Postorder_nonr ()     {        binarytreenode<t >* cur = _root;        stack<binarytreenode<t >*>s;         binarytreenode<t>* prev =  null;        while (cur| |! S.empty ())         {             while (cur)             {                 s.push (cur);                 cur =  cur->_left ;            }             Binarytreenode<t>* top = s.top ();             if (top->_right == null| | Top->_right == prev)             {                 cout<< top->_data << " ";                 s.pop ();                 prev = top;             }            else             cur = top->_right ;            //cout<<endl;         }    }protected:    binarytreenode<t>*  _root;}; Int main () {    int a1[10] = {1,2,3, ' # ', ' # ', 4, ' # ', ' # ',5,6};   &NBSP;&NBSP;BINARYTREE&LT;INT&GT;&NBSP;&NBSP;B1 (a1,10, ' # ');     //b1. Inorder ();     //b1. inorder_nonr  ();     //b1. Depth ();     //b1. Prevorder_nonr ();     b1. Postorder_nonr ();     system ("pause");     return 0;}


Binary tree creation and recursive and non-recursive traversal

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.