C++实现二叉树的基本操作

BinTree.h文件

#pragma once
#pragma once
#include<iostream>
using namespace std;
#include<string.h>
#include<queue>
#include<stack>

template<class T>
struct BinTreeNode
{
    BinTreeNode(const T& data)
    :_data(data)
    , _left(NULL)
    , _right(NULL)
    {}

    BinTreeNode<T>* _left;
    BinTreeNode<T>* _right;
    T _data;
};

template<class T>
class BinTree
{
    typedef BinTreeNode<T> Node;
    typedef Node* pNode;
public:
    BinTree()
        :_pRoot(NULL)
    {}

    BinTree(const T* array, size_t size, const T& invalid)
    {
        size_t index = 0;
        _CreateBinTree(_pRoot, array, size, invalid, index);
    }

    BinTree(const BinTree<T>& bt)
        :_pRoot(NULL)
    {
        _pRoot = _CopyBinTree(bt._pRoot);
    }

    BinTree<T>& operator=(const BinTree<T>& bt)
    {
        if (this != &bt)
        {
            _DestoryBinTree(_pRoot);
            _pRoot = _CopyBinTree(bt._pRoot);
        }
        return *this;
    }

    ~BinTree()
    {
        if (_pRoot)
        {
            _DestoryBinTree(_pRoot);
        }
    }


    void PreOrder()//先序遍历
    {
        cout << "PreOrder:";
        _PreOrder(_pRoot);
        cout << endl;
        cout << "PreOrder1:";
        _PreOrder1(_pRoot);
        cout << endl;

        cout << "PreOrder2:";
        _PreOrder2(_pRoot);
        cout << endl;
    }


    void InOrder()//中序遍历
    {
        cout << "InOrder:";
        _InOrder(_pRoot);
        cout << endl;
        cout << "InOrder1:";
        _InOrder1(_pRoot);
        cout << endl;
    }


    void PostOrder()//后序遍历
    {
        cout << "PostOrder:";
        _PostOrder(_pRoot);
        cout << endl;
        cout << "PostOrder1:";
        _PostOrder1(_pRoot);
        cout << endl;
    }

    void Size()//统计结点的个数
    {
        cout << "结点的个数为:";
        size_t count1 = 0;
        _CountNode(_pRoot, count1);
        cout << count1 << endl;

        cout << "CountNode1:";
        cout << _CountNode1(_pRoot) << endl;
    }

    void LeftSize()//统计叶子节点的个数
    {
        cout << "叶子结点的个数为:";
        size_t count = 0;
        _CountLeft(_pRoot, count);
        cout << count << endl;

        cout << "_CountLeft1:";
        cout << _CountLeft1(_pRoot) << endl;
    }

    void Height()//树的高度
    {
        cout << "树的高度为:";
        cout << _Height1(_pRoot)<< endl;
    }

    void LevelOrder()//层序遍历
    {
        if (_pRoot == NULL)
            return;
        cout << "层次遍历:";
        queue<pNode> s;
        s.push(_pRoot);
        while (!s.empty())
        {
            pNode pCur = s.front();
            cout << pCur->_data << " ";

            if (pCur->_left)
                s.push(pCur->_left);

            if (pCur->_right)
                s.push(pCur->_right);

            s.pop();
        }
        cout << endl;
    }

    void _PreOrder(pNode pRoot)//先序遍历递归实现
    {
        if (pRoot != NULL)
        {
            cout << pRoot->_data << " ";
            _PreOrder(pRoot->_left);
            _PreOrder(pRoot->_right);
        }
    }

    void _PreOrder1(pNode pRoot)//先序遍历非递归实现
    {
        stack<pNode> s;
        pNode pCur = pRoot;
        while (!s.empty()||pCur)
        {
            while (pCur)
            {
                cout << pCur->_data << " ";
                s.push(pCur);
                pCur = pCur->_left;
            }

            //左子树遍历完了
            pNode tmp = s.top();
            s.pop();
            pCur = tmp->_right;
        }
    }

    void _PreOrder2(pNode pRoot)//先序遍历非递归实现
    {
        if (pRoot == NULL)
            return;
        stack<pNode> s;
        s.push(pRoot);
        while (!s.empty())
        {
            pNode pCur = s.top();
            cout << pCur->_data << " ";
            s.pop();

            if (pCur->_right)
                s.push(pCur->_right);
            if (pCur->_left)
                s.push(pCur->_left);
        }
    }

    void _InOrder(pNode pRoot)//中序遍历递归实现
    {
        if (pRoot != NULL)
        {
            _InOrder(pRoot->_left);
            cout << pRoot->_data << " ";
            _InOrder(pRoot->_right);
        }
    }

    void _InOrder1(pNode pRoot)//中序遍历非递归实现
    {
        stack<pNode> s;
        pNode pCur = pRoot;
        while (!s.empty() || pCur)
        {
            while (pCur)
            {
                s.push(pCur);
                pCur = pCur->_left;
            }

            //左子树遍历完了
            pNode tmp = s.top();
            cout << tmp->_data << " ";
            s.pop();
            pCur = tmp->_right;
        }
    }

    void _PostOrder(pNode pRoot)//后序遍历递归实现
    {
        if (pRoot != NULL)
        {
            _PostOrder(pRoot->_left);
            _PostOrder(pRoot->_right);
            cout << pRoot->_data << " ";
        }
    }

    void _PostOrder1(pNode pRoot)//后序遍历非递归实现
    {
        stack<pNode> s;
        pNode pCur = pRoot;
        pNode Prev = NULL;
        while (!s.empty() || pCur)
        {
            while (pCur)
            {
                s.push(pCur);
                pCur = pCur->_left;
            }
            //左子树遍历完了
            pNode tmp = s.top();
            if (tmp->_right && tmp->_right != Prev)
            {
                pCur = tmp->_right;
            }
            else
            {
                cout << tmp->_data << " ";
                Prev = tmp;
                s.pop();
            }
        }
    }

    void GetKlevelNode(size_t K)//求第K层结点的个数
    {
        cout << "第K层的结点个数是:";

        if (K>_Height1(_pRoot))
            return;

        cout << _GetKlevelNode(_pRoot,K) << endl;
    }

    bool IsNodeInBinTree(pNode pNode)//判断结点是否存在二叉树中
    {
        return _IsNodeInBinTree(_pRoot, pNode);
    }

    pNode Find(const T& data)//查找结点
    {
        return _Find(_pRoot,data);
    }

    void Mirror()//树的镜像
    {
        _Mirror(_pRoot);
    }

    bool IsCompletelyBinTree()//判断树是否为完全二叉树
    {
        return _IsCompletelyBinTree(_pRoot);
    }

protected:

    bool _IsCompletelyBinTree(pNode pRoot)//判断树是否为完全二叉树
    {
        if (pRoot==NULL)
            return true;

        queue<pNode> q;
        q.push(pRoot);
        bool IsFlag = false;
        while (!q.empty())
        {
            pNode tmp = q.front();

            if (IsFlag)
            {
                if (pRoot->_left || pRoot->_right)
                    return false;
            }
            else
            {
                if (tmp->_left&&tmp->_right)
                {
                    q.push(tmp->_left);
                    q.push(tmp->_right);
                }
                else if (tmp->_left)
                {
                    q.push(tmp->_left);
                    IsFlag = true;
                }
                else if (tmp->_right)
                    return false;
                else
                    IsFlag = true;
            }   
            q.pop();
        }
        return true;
    }

    void _Mirror(pNode pRoot)//树的镜像递归实现
    {
        if (pRoot)
        {
            swap(pRoot->_left, pRoot->_right);
            _Mirror(pRoot->_left);
            _Mirror(pRoot->_right);
        }
    }

    void _Mirror1(pNode pRoot)//树的镜像非递归实现
    {
        if (pRoot == NULL)
            return;
        queue<pNode> s;
        s.push(pRoot);
        while (!s.empty())
        {
            pNode tmp = s.front();
            if (tmp->_left || tmp->_right)
                swap(tmp->_left, tmp->_right);

            if (tmp->_left)
                s.push(tmp->_left);

            if (tmp->_right)
                s.push(tmp->_right);

            s.pop();
        }
        cout << endl;
    }

    pNode _Find(pNode pRoot, const T& data)//查找结点
    {
        if (NULL == pRoot)
            return NULL;
        if (pRoot->_data == data)
            return pRoot;

        pNode tmp = _Find(pRoot->_left, data);
        if (tmp)
            return tmp;
        return _Find(pRoot->_right, data);
    }

    bool _IsNodeInBinTree(pNode pRoot, pNode pNode)//判断结点是否存在二叉树中
    {
        if (pRoot == NULL || pNode == NULL)
            return false;

        if (pRoot == pNode)
            return true;

        bool IsIn = _IsNodeInBinTree(pRoot->_left, pNode);
        if (IsIn)
            return true;

        return _IsNodeInBinTree(pRoot->_right, pNode);
    }

    size_t _GetKlevelNode(pNode pRoot,size_t k)//求第K层结点的个数
    {
        if (pRoot == NULL||k==0)
            return 0;
        else if (k == 1)
              return 1;
        else return _GetKlevelNode(pRoot->_left, k - 1) + _GetKlevelNode(pRoot->_right, k - 1);
    }

    size_t _Height(pNode pRoot)//树的高度
    {
        int hl = 0;
        int hr = 0;
        int max = 0;
        if (pRoot)
        {
            hl = _Height(pRoot->_left);
            hr = _Height(pRoot->_right);
            max  = hl > hr ? hl : hr;
            return max+1;
        }
        else
        {
            return 0;
        }
    }

    size_t _Height1(pNode pRoot)//树的高度
    {
        if (pRoot == NULL)
            return 0;
        if (pRoot->_left == NULL&&pRoot->_right == NULL)
            return 1;

        size_t hl = _Height1(pRoot->_left);
        size_t hr = _Height1(pRoot->_right);

        return hl > hr ? ++hl : ++hr;
    }

    size_t _CountNode(pNode pRoot, size_t& count1) //统计结点的个数
    {
        if (pRoot)
        {
            count1++;
            _CountNode(pRoot->_left, count1);
            _CountNode(pRoot->_right, count1);
        }
        return count1;
    }

    size_t _CountNode1(pNode pRoot)  //统计结点的个数
    {
        if (pRoot == NULL)
            return 0;
        else
        {
            return _CountNode1(pRoot->_left) + _CountNode1(pRoot->_right) + 1;
        }
    }

    size_t _CountLeft(pNode pRoot, size_t& count)//叶子结点的个数
    {
        if (pRoot)
        {
            _CountLeft(pRoot->_left, count);
            _CountLeft(pRoot->_right, count);
            if (pRoot->_left == NULL&&pRoot->_right == NULL)
            {
                count++;
            }
        }
        return count;
    }

    size_t _CountLeft1(pNode pRoot)//叶子结点的个数
    {
        if (pRoot == NULL)
            return 0;
        if (pRoot->_left == NULL&&pRoot->_right == NULL)
            return 1;
        return _CountLeft1(pRoot->_left) + _CountLeft1(pRoot->_right);
    }

    void _CreateBinTree(pNode& pRoot, const T* array, size_t size, const T& invalid, size_t& index)//创建二叉树
    {
        if (index < size && array[index] != invalid)
        {
            //创建根节点
            pRoot = new Node(array[index]);

            //创建根节点的左子树
            _CreateBinTree(pRoot->_left, array, size, invalid, ++index);

            //创建根节点的右子树
            _CreateBinTree(pRoot->_right, array, size, invalid, ++index);
        }
    }

    pNode _CopyBinTree(pNode pRoot)//拷贝二叉树
    {
        pNode newRoot = NULL;
        if (pRoot != NULL)
        {
            //拷贝根节点
            newRoot = new Node(pRoot->_data);

            //拷贝根节点左子树
            if (pRoot->_left)
                newRoot->_left = _CopyBinTree(pRoot->_left);

            //拷贝根节点右子树
            if (pRoot->_right)
                newRoot->_right = _CopyBinTree(pRoot->_right);
        }
        return newRoot;
    }

    void _DestoryBinTree(pNode& pRoot)//销毁二叉树
    {
        if (pRoot)
        {
            _DestoryBinTree(pRoot->_left);
            _DestoryBinTree(pRoot->_right);
            delete pRoot;
        }
    }
private:
    pNode _pRoot;
};

test.c文件

#include"BinTree.h"

void TestBinTree()
{
    char* pStr = "ABD###CE##F";
    BinTree<char> bt1(pStr, strlen(pStr), '#');
    BinTree<char> bt2(bt1);
    BinTree<char> bt3;
    bt3 = bt2;
    bt1.PreOrder();
    bt1.InOrder();
    bt1.PostOrder();
    bt1.LeftSize();
    bt1.Size();
    bt1.Height();
    bt1.LevelOrder();
    bt1.GetKlevelNode(3);
    /*bt2.Mirror();
    bt2.PreOrder();
    bt2.Mirror();
    bt2.PreOrder();*/

    /*if (bt1.IsCompletelyBinTree())
        cout << "是完全二叉树"<<endl;
    else
        cout << "不是完全二叉树" << endl;*/
}


int main()
{
    TestBinTree();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值