二叉树的创建以及递归与非递归遍历-创新互联

二叉树先序遍历;(1)先序访问根节点 (2)先序访问左子树 (3)先序访问右子树

创新互联是一家朝气蓬勃的网站建设公司。公司专注于为企业提供信息化建设解决方案。从事网站开发,网站制作,网站设计,网站模板,微信公众号开发,软件开发,小程序制作,十载建站对工商代办等多个领域,拥有多年的网站运维经验。

二叉树中序遍历;(1)中序访问根节点 (2)中序访问左子树 (3)中序访问右子树

二叉树后序遍历;(1)后序访问根节点 (2)后序访问左子树 (3)后序访问右子树

测试用例:int a[10]={'1','2','3','#','#','4','#','#','5','6'}

二叉树的创建以及递归与非递归遍历

代码:

#include
using namespace std;
#include
#include
template
struct BinaryTreeNode
{
    BinaryTreeNode* _left;
    BinaryTreeNode* _right;
    T _data;
    BinaryTreeNode(const T& d)
        :_left(NULL)
        ,_right(NULL)
        ,_data(d)
    {}
};
template
class BinaryTree
{
public:
    BinaryTree()
        :_root(NULL)
    {}
    BinaryTree(const T* a,size_t size,const T& invalid)
    {
        size_t index = 0;
        _root = _Create(a,size,index,invalid);
    }
    //BinaryTree(const BinaryTree& d)
    //{
    //    BinaryTreeNode root = NULL;

    //}
    BinaryTree& operator = (const BinaryTree& d)
    {
        swap(root,d._root );
    }
    void PrevOrder()
    {
       _PrevOrder(_root);
    }
    void InOrder()
    {
       _InOrder(_root);
    }
    size_t Size()
    {
        _Size(_root);
    }
    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();
    }
public:

protected:
   BinaryTreeNode* _Create(const T*a,size_t size,size_t& index,const T& invalid)
   {
       BinaryTreeNode *root = NULL;
       while(index (a[index]);
           root->_left = _Create(a,size,++index,invalid);
           root->_right = _Create(a,size,++index,invalid);
       }
       return root;
   }
   void _PrevOrder(BinaryTreeNode* root)
   {
       if(root == NULL)
       {
            return;
       }
       cout<_data<<" " ;
       _PrevOrder(root->_left );
       _PrevOrder(root->_right);
   }
   void _InOrder(BinaryTreeNode* root)
   {    
        if(root == NULL)
        {
            return;
        }
        _InOrder (root->_left );
        cout<_data<<" " ;
        _InOrder (root->_right );
   }
   size_t _size(BinaryTreeNode* root)
   {
        if(root == NULL)
        {
            return 0;
        }
        return _Size(root->_left )+_Size(root->_right )+1;
   }
   size_t _Depth(BinaryTreeNode* 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* 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*>q;
        if(_root)
        {
            q.push(_root);
        }
        while(!q.empty())
        {
            BinaryTreeNode* front = q.front();
            cout<_left)
            {
                q.push(_root->_left);
            }
            if(_root->_right)
            {
                q.push(_root->_right);
            }
            q.pop();
        }
        cout<*>s;
        BinaryTreeNode* cur = _root;
        while(cur||!s.empty())
        {
            
            while(cur )
            {
                cout<_data <<" "; 
                s.push(cur);
                cur = cur->_left ;
            }
            if(!s.empty())
            {
                BinaryTreeNode* top = s.top();
                cur = top->_right ;
                s.pop();
            }            
        }
    }
    void _InOrer_NonR()
    {
        stack*> s;
        BinaryTreeNode* cur = _root;
        while(cur||!s.empty())
        {
            while(cur)
            {
               s.push(cur);
               cur = cur->_left ;
            }
            BinaryTreeNode* Top = s.top();
            cout<_data<<" ";            
            cur = Top->_right ;
            s.pop();
        }
        cout<* cur = _root;
        stack*>s; 
        BinaryTreeNode* prev = NULL;
        while(cur||!s.empty())
        {
            while(cur)
            {
                s.push(cur);
                cur = cur->_left ;
            }
            BinaryTreeNode* top = s.top();
            if(top->_right == NULL||top->_right == prev)
            {
                cout<_data <<" ";
                s.pop();
                prev = top;
            }
            else
            cur = top->_right ;
            //cout<* _root;
};
int main()
{
    int a1[10] = {1,2,3,'#','#',4,'#','#',5,6};
    BinaryTree  b1(a1,10,'#');
    //b1.InOrder();
    //b1.InOrder_NonR ();
    //b1.Depth();
    //b1.PrevOrder_NonR();
    b1.PostOrder_NonR();
    system("pause");
    return 0;
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站栏目:二叉树的创建以及递归与非递归遍历-创新互联
文章路径:http://myzitong.com/article/dgjphp.html