二叉树的先序,中序,后序遍历

以此二叉树为例
BinaryTree.h

#include<iostream>
#include<queue>
#include<stack>
using namespace std;

template<class T>
struct BinaryTreeNode
{
    BinaryTreeNode(T value = 0)
    :_value(value)
    ,_LeftChild(NULL)
    , _RightChild(NULL)
    {}
    T _value;
    BinaryTreeNode<T>* _LeftChild;
    BinaryTreeNode<T>* _RightChild;
};

template<class T>
class BinaryTree
{
public:
    BinaryTree()
        :_root(NULL)
    {}
    BinaryTree(T* str)
    {
        _CreateTree(_root, str);
    }
    BinaryTree( BinaryTree<T>& bt)
    {
        _root = _CopyBinaryTree(bt._root);
    }
    BinaryTree& operator=(BinaryTree<T>& bt)
    {
        /*if (this != &bt)
        {
            _Destory(_root);
            _root = _CopyBinaryTree(bt._root);
        }*/
        swap(_root, bt._root);
        return *this;
    }
    ~BinaryTree()
    {
        _Destory(_root);
    }
    void preOrder()
    {
        cout << "树的先序遍历:";
        _preOrder(_root);
        cout << endl;
    }
    void InOrder()
    {
        cout << "树的中序遍历:";
        _InOreder(_root);
        cout << endl;
    }
    void PostOrder()
    {
        cout << "树的后序遍历:";
        _PostOrder(_root);
        cout << endl;
    }
    void LevelOrder()
    {
        cout << "树的层序遍历:";
        _LevelOrder(_root);
        cout << endl;
    }
    void preOrder_NonRec() //非递归
    {
        cout << "树的先序遍历:";
        _preOrder_NonRec(_root);
        cout << endl;
    }

    void InOrder_NonRec()
    {
        cout << "树的中序遍历:";
        _InOrder_NonRec(_root);
        cout << endl;
    }
    void PostOrder_NonRec()
    {
        cout << "树的后序遍历:";
        _PostOrder_NonRec(_root);
        cout << endl;
    }
    int Size()
    {
        int size = 0; //在栈上开辟内存,不存在线程安全问题,每一个线程都有属于自己的栈帧
        cout << "此树的节点数:";
        _Size(_root,size);
        return size;
    }
    int LeafSize()
    {
        int size = 0;
        cout << "叶子节点的个数:";
        _LeafSize(_root,size);
        return size;
    }
    int GetKLevelSize(int k)
    {
        cout << "第" << k << "层节点个数:";
        return _GetKLevelSize(_root, k);
    }
    int Depth()
    {
        cout << "树的深度:";
        return _Depth(_root);
    }

protected:
    void _Destory(BinaryTreeNode<T>*& root)
    {
        if (root)
        {
            _Destory(root->_LeftChild);
            _Destory(root->_RightChild);
            delete root;
            root = NULL;
        }
    }
    BinaryTreeNode<T>*& _CopyBinaryTree(BinaryTreeNode<T>*& root)
    {
        BinaryTreeNode<T>* CopyRoot = new BinaryTreeNode<T>(root->_value);
        if (root->_LeftChild)
        {
            CopyRoot->_LeftChild = _CopyBinaryTree(root->_LeftChild);
        }
        if (root->_RightChild)
        {
            CopyRoot->_RightChild = _CopyBinaryTree(root->_RightChild);
        }
        return CopyRoot;
    }
    void _CreateTree(BinaryTreeNode<T>*& root, T*& str) //创建节点
    {
        if (*str != '#' && *str != '\0')
        {
            root = new BinaryTreeNode<T>(*str);
            _CreateTree(root->_LeftChild, ++str);
            _CreateTree(root->_RightChild, ++str);
        }
    }
    void _preOrder(BinaryTreeNode<T>*& root) //先序遍历 跟,左,右
    {
        if (root)
        {
            cout << root->_value << " ";
            _preOrder(root->_LeftChild);
            _preOrder(root->_RightChild);
        }
    }

    void _InOreder(BinaryTreeNode<T>*& root) //中序遍历 左,跟,右
    {
        if (root)
        {
            _InOreder(root->_LeftChild);
            cout << root->_value << " ";
            _InOreder(root->_RightChild);
        }
    }
    void _PostOrder(BinaryTreeNode<T>*& root) //后序遍历 左,右,跟
    {
        if (root)
        {
            _PostOrder(root->_LeftChild);
            _PostOrder(root->_RightChild);
            cout << root->_value<<" ";
        }
    }
    void _LevelOrder(BinaryTreeNode<T>*& root)
    {
        queue<BinaryTreeNode<T>*> q; //利用队列先进先出的特点
        if (root)
        {
            q.push(root);
        }
        //队列中的数据为1 2 5 3 4 6
        while (!q.empty())
        {
            BinaryTreeNode<T>* front = q.front();
            cout << front->_value << " ";
            q.pop();
            if (front->_LeftChild != NULL)
            {
                q.push(front->_LeftChild); //插入左孩子
            }
            if (front->_RightChild != NULL)
            {
                q.push(front->_RightChild); //插入右孩子
            }
        }
    }
    //方法一
    //int _Size(BinaryTreeNode<T>*& root)
    //{
    //  if (root == NULL) //空树
    //  {
    //      return 0;
    //  }
    //  if (root->_LeftChild == NULL && root->_RightChild == NULL) //叶子节点
    //  {
    //      return 1;
    //  }
    //  //左子树节点的个数 + 右子数节点的个数 + 1(根节点)
    //  return 1 + _Size(root->_LeftChild) + _Size(root->_RightChild);
    //}

    //方法二
    int _Size(BinaryTreeNode<T>*& root, int& size)
    {
        //static int size = 0;  存在线程安全问题
        if (root == NULL)
            return 0;
        ++size; // 节点不为空size就++
        _Size(root->_LeftChild,size);
        _Size(root->_RightChild,size);
    }

    //int _LeafSize(BinaryTreeNode<T>*& root) //叶子节点的个数
    //{
    //  if (root == NULL)
    //      return 0;
    //  if (root->_LeftChild == NULL && root->_RightChild == NULL)
    //      return 1;
    //  int left = _LeafSize(root->_LeftChild);
    //  int right = _LeafSize(root->_RightChild);
    //  return left + right;
    //}

    void _LeafSize(BinaryTreeNode<T>*& root,int& size) //叶子节点的个数
    {
        if (root == NULL)
            return;
        if (root->_LeftChild == NULL && root->_RightChild == NULL)
            ++size;
        _LeafSize(root->_LeftChild,size);
        _LeafSize(root->_RightChild,size);
    }

    int _GetKLevelSize(BinaryTreeNode<T>*& root, int k) //根节点是第一层
    {
        if (root == NULL || k == 0)
            return 0;
        if (k == 1)
            return 1;
        int left = _GetKLevelSize(root->_LeftChild, k - 1);
        int right = _GetKLevelSize(root->_RightChild, k - 1);
        return left + right;
    }

    int _Depth(BinaryTreeNode<T>*& root)
    {
        if (root == NULL)
        {
            return 0;
        }
        size_t LeftDepth = _Depth(root->_LeftChild);
        size_t RightDepth = _Depth(root->_RightChild);
        return 1 + (LeftDepth > RightDepth ? LeftDepth : RightDepth);
    }

    void _preOrder_NonRec(BinaryTreeNode<T>*& root)
    {
        stack<BinaryTreeNode<T>*> s;
        if (root)
        {
            s.push(root);
        }
        while (!s.empty())
        {
            BinaryTreeNode<T>* top = s.top();
            cout << top->_value << " ";
            s.pop();//节点访问之后就要删除
            if (top->_RightChild) //先插入右节点,先进后出
            {
                s.push(top->_RightChild);
            }
            if (top->_LeftChild)
            {
                s.push(top->_LeftChild);
            }
        }
    }
    void _InOrder_NonRec(BinaryTreeNode<T>*& root)
    {
        stack<BinaryTreeNode<T>*> s;
        BinaryTreeNode<T>* cur = root;
        while (cur || !s.empty())
        {
            while (cur) //将所有左节点存入栈中
            {
                s.push(cur);
                cur = cur->_LeftChild;
            }
            if(!s.empty())
            {
                BinaryTreeNode<T>* top = s.top();
                cout << top->_value<<" ";
                s.pop();
                cur = top->_RightChild; 
            }
        }
    }
    void _PostOrder_NonRec(BinaryTreeNode<T>*& root)
    {
        stack<BinaryTreeNode<T>*> s;
        BinaryTreeNode<T>* cur = root;
        BinaryTreeNode<T>* prev = NULL;
        while (cur || !s.empty())
        {
            while (cur)
            {
                s.push(cur);
                cur = cur->_LeftChild;
            }
            if (!s.empty())
            {
                BinaryTreeNode<T>* top = s.top();

                //当右子树为空 或 右子树已经访问过了 才能访问根节点
                if (top->_RightChild == NULL || top->_RightChild == prev)
                {
                    cout << top->_value << " ";
                    prev = top;
                    s.pop();
                }
                else //右子数还没有访问
                {
                    cur = top->_RightChild;
                }
            }
        }
    }

private:
    BinaryTreeNode<T>* _root;
}; 

main.cpp

#include"BinaryTree.h"

void Test()
{
    char* str = "123##4##56";
    BinaryTree<char> bt(str);
    bt.preOrder();
    bt.InOrder();
    bt.PostOrder();
    bt.LevelOrder();
    int size = bt.Size();
    cout << size << endl;
    int depth = bt.Depth();
    cout << depth << endl;
    bt.preOrder_NonRec();
    bt.InOrder_NonRec();
    bt.PostOrder_NonRec();
}
void Test1()
{
    char* str = "123##4##56";
    BinaryTree<char> bt(str);
    bt.preOrder_NonRec();
    BinaryTree<char> bt1(bt);
    bt1.preOrder_NonRec();
    bt1 = bt;
    bt1.preOrder_NonRec();
}
int main()
{
    Test1();
    getchar();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值