二叉树部分相关练习题

二叉树:一颗二叉树是节点的的一个有限集合,此集合可为空可由一个根节点及左子树、右子树组成。
下面为二叉树部分的相关习题:
1.创建二叉树
这里写图片描述

#include<iostream>
#include<queue>
#include<stack>
using namespace std;
//给出结点
template<class T>
struct BinTreeNode
{
    BinTreeNode<T>*_pLeft;//左子树
    BinTreeNode<T>*_pRight;//右子树
    T _data;
    BinTreeNode(const T&data)//构造函数
        :_pLeft(NULL)
        , _pRight(NULL)
        , _data(data)
    {}
};
template<class T>
class BinTree
{
    typedef BinTreeNode<T>*pNode;
    typedef BinTreeNode<T>Node;
public:
    BinTree()
        :_pRoot(NULL)
    {}
    BinTree(const T*array, size_t size, const T&invalid)//构造函数
    {
        size_t index = 0;
        CreateBinTree(_pRoot, array, size, index, invalid);
    }
创建二叉树的相关代码:
private:
    void CreateBinTree(pNode&pRoot, const T*array, size_t size, size_t &index, const T&invalid)//二叉树的建立
    {
        //先进行判断
        if (index < size&&'#' != array[index])
        {
             pRoot = new Node(array[index]);//创建根结点
            //创建左子树
            CreateBinTree(pRoot->_pLeft, array, size, ++index, invalid);
            //创建右子树
            CreateBinTree(pRoot->_pRight, array, size, ++index, invalid);
        }
    }
    pNode _pRoot;
};

2.前/中/后遍历二叉树(递归&非递归)
前序遍历顺序依次为:根节点—》左子树—》右子树
中序遍历顺序依次为:左子树—》根节点—》右子树
后序遍历顺序依次为:左子树—》右子树—》根节点
具体实现如下所示:

(三种遍历方式的递归形式:)
void preorder()
    {
        cout << "前序遍历:";
        _preorder(_pRoot);
        cout << endl;
    }
    void _preorder(pNode pRoot)//前序遍历的实现
    {
        if (pRoot)
        {
            cout << pRoot->_data << " ";
            _preorder(pRoot->_pLeft);
            _preorder(pRoot->_pRight);
        }
    }
    void midorder()
    {
        cout << "中序遍历:";
        _midorder(_pRoot);
        cout << endl;
    }
    void _midorder(pNode pRoot)//中序遍历的实现
    {
        if (pRoot)
        {
            _midorder(pRoot->_pLeft);
            cout << pRoot->_data << " ";
            _midorder(pRoot->_pRight);
        }
    }
    void pNextorder()
    {
        cout << "后序遍历" << " ";
        _pNextorder(_pRoot);
        cout << endl;
    }
    void _pNextorder(pNode pRoot)//后序遍历的实现
    {
        if (pRoot)
        {
            _pNextorder(pRoot->_pLeft);
            _pNextorder(pRoot->_pRight);
            cout << pRoot->_data << " ";
        }
    }

下为三种遍历方式的非递归形式:
前序遍历的非递归应用栈,由于栈的特性是先进的后出,故先将根节点入栈,再将右子树入栈,最后将左子树入栈。

void preorder_nor()
    {
        cout << "前序遍历的非递归:";
        _preorder_nor(_pRoot);
        cout << endl;
    }
    void _preorder_nor(pNode pRoot)//前序遍历非递归的实现
    {
        if (_pRoot == NULL)
        {
            return;
        }
        stack<pNode>s;
        s.push(_pRoot);
        while (!s.empty())
        {
            pNode pTop = s.top();
            cout << pTop->_data << "";
              s.pop();
              if (pTop->_pRight)
              {
                  s.push(pTop->_pRight);
              }
              if (pTop->_pLeft)
              {
                  s.push(pTop->_pLeft);
              }
        }
    }

中序遍历的非递归应用栈,将找到当前节点为根的左侧路径中的节点压入栈中,取到栈顶对其进行访问,再将右子树压入栈中。具体实现如下:

void midorder_nor()
    {
        cout << "二叉树的中序遍历非递归:";
        _midorder_nor(_pRoot);
        cout << endl;
    }
    void _midorder_nor(pNode pRoot)//中序非递归的实现
    {
        if (pRoot == NULL)
        {
            return;
        }
        stack<pNode>s;
        pNode cur = _pRoot;
        //1.找到当前节点为根的左侧路径中的节点
        while (cur || !s.empty())
        {
            while (cur)
            {
                s.push(cur);
                cur = cur->_pLeft;//将其左侧的节点中的元素压入栈中
            }
            //取出栈顶元素并进行访问
            cur = s.top();
            cout << cur->_data << " ";
            s.pop();
            //将右子树压入栈中
            cur = cur->_pRight;
        }
    }

后序遍历的非递归应用栈,将找到当前节点为根的左侧路径中的节点压入栈中,取到栈顶对其进行访问,再将右子树压入栈中。但是与中序不同的是要给其初始化一个prev指针,记录已经访问过的节点具体实现如下:

void pNextorder_nor()
    {
        cout << "后序遍历非递归:";
        _pNextorder_nor(_pRoot);
        cout << endl;
    }
    void _pNextorder_nor(pNode pRoot)//后序遍历非递归
    {
        if (_pRoot == NULL)
        {
            return;
        }
        stack<pNode>s;
        pNode pcur = _pRoot;
        pNode prev = NULL;
        while (pcur || !s.empty())
        {
            while (pcur)
            {
                s.push(pcur);
                pcur = pcur->_pLeft;
            }
            if (!s.empty())
            {
                pcur = s.top();
            }
            if (pcur->_pRight == NULL || pcur->_pRight == prev)
            {
                cout << pcur->_data << " ";
                prev = pcur;//将当前结点地址赋值pre作为下一次判断标志,防止重复访问  
                s.pop();
                pcur = NULL;
            }
            else
                pcur = pcur->_pRight;
        }
    }

3.层序遍历二叉树
层序遍历的基本思想为:因为要进行一层一层遍历,故应用队列,队列的特点是先进先出,所以先将根节点如队列,进行访问,之后是左子树。右子树。具体实现如下:

void Levelorder()//层序遍历
    {
        cout << "层序遍历" << " ";
        _Levelorder();
        cout << endl;
    }
    void _Levelorder()//层序遍历的实现(应用了队列,先进先出)
    {
        if (_pRoot == NULL)
        {
            return;
        }
        queue<pNode>q;//定义一个队列
        q.push(_pRoot);//根节点入队
        while (!q.empty())
        {
            pNode cur = q.front(); //将队头取出
            cout << cur->_data << " ";
            if (cur->_pLeft)
            {
                q.push(cur->_pLeft);//左子树入队
            }
            if (cur->_pRight)
            {
                q.push(cur->_pRight);//右子树入队
            }
            q.pop();
        }
        cout << endl;
    }

4.求二叉树高度
二叉树的高度为:左子树的高度右子树高度的最大值+1。
具体实现如下:

    size_t Height()
    {
        cout << "二叉树的高度为:";
        return _Height(_pRoot);
    }
   size_t _Height(pNode pRoot)//二叉树的高度
    {
        if (pRoot == NULL)
        {
            return 0;
        }
        if (pRoot->_pLeft == NULL&&pRoot->_pRight == NULL)
        {
            return 1;
        }
        size_t leftHeight = _Height(pRoot->_pLeft);
        size_t rightHeight = _Height(pRoot->_pRight);
        return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
    }

5.求二叉树中节点的个数
二叉树中节点个数为左子树节点个数+右子树节点个数+根节点
具体实现如下:

    size_t size()
    {
        cout << "二叉树节点个数为:";
        return Size(_pRoot);
    }
    size_t Size(pNode pRoot)//二叉树节点个数
    {
        if (NULL == pRoot)
            return 0;
        return Size(pRoot->_pLeft) + Size(pRoot->_pRight) + 1;
    }

6.求叶子节点的个数
叶子节点为没有左右子树的节点,具体分为三种情况:
(1)、空树——-返回0
(2)、只有一个根节点—-返回1
(3)、含有多个节点—–则为左右子树叶子节点之和
具体实现如下:

    size_t GetLeefCount()
    {
        cout << "二叉树中叶子节点个数:";
        return _GetLeefCount(_pRoot);
    }
   size_t _GetLeefCount(pNode pRoot)//二叉树叶子节点的个数
    {
        if (pRoot == NULL)
        {
            return 0;
        }
        if (pRoot->_pLeft == NULL&&pRoot->_pRight == NULL)
        {
            return 1;
        }
        return _GetLeefCount(pRoot->_pLeft) + _GetLeefCount(pRoot->_pRight);
    }

7.求二叉树中第K层节点的个数
求第K层的节点个数也分为三种情况:
(1)、空树或者K<=0——-返回0
(2)、只有一个根节点—-返回1
(3)、含有多个节点—–则为第K-1层的左右子树节点相加
具体实现如下所示:

    size_t GetKcount(size_t k)
    {
        cout << "二叉树第K层节点个数";
        return _GetKcount(_pRoot, k);
    }
    size_t  _GetKcount(pNode pRoot, size_t k)//二叉树中第K层的节点个数
    {
        if (pRoot == NULL || k <= 0)
        {
            return 0;
        }
        if (k == 1)
        {
            return 1;
        }
        return _GetKcount(pRoot->_pLeft, k - 1) + _GetKcount(pRoot->_pRight, k - 1);//上一层的左右节点之和
    }

8.在此二叉树中找一个节点
具体实现如下:

pNode Find(const T&data)
    {
        //cout << "找的节点为:";
        return _Find(_pRoot, data);
    }
    pNode _Find(pNode pRoot, const T&data)//找一个节点
    {
        if (pRoot == NULL)
        {
            return NULL;
        }
        /*if (pRoot->_data == data)
        {
            return pRoot;
        }
        pNode ret = NULL;
        if (ret == (_Find(pRoot->_pLeft, data)))
        {
            return ret;
        }
        return _Find(_pRoot->_pRight, data);*/
        if (data == pRoot->_data)
            return pRoot;
        if (pRoot->_pLeft)
        {
            _Find(pRoot->_pLeft, data);
        }
        if (pRoot->_pRight)
        {
            _Find(pRoot->_pRight, data);
        }
    }

9.找寻双亲节点与节点的左右孩子

pNode parent(pNode pNode)
    {
        cout << "该节点的双亲节点为:";
        return _parent(_pRoot, pNode);
    }
    pNode _parent(pNode pRoot, pNode pNode)
    {
        if (pRoot == pNode || pRoot == NULL || pNode == NULL)
        {
             return NULL;
        }
        if (pRoot == pNode || pRoot == NULL || pNode == NULL)
         {
           return NULL;
         }
        if (pNode == pRoot->_pLeft || pNode == pRoot->_pRight)
            return pRoot;
        if (pRoot->_pLeft)
        {
            _parent(pRoot->_pLeft, pNode);
        }
        if (pRoot->_pRight)
        {
            _parent(pRoot->_pRight, pNode);
        }
        return pRoot;
    }
    pNode Leftchild(pNode pNode)
    {
        cout << "该节点的左孩子为:";
        return (NULL == pNode) ? NULL : pNode->_pLeft;
    }

    pNode Rightchild(pNode pNode)
    {
        cout << "该节点的右孩子为:";
        return (NULL == pNode) ? NULL : pNode->_pRight;
    }

10.求二叉树的镜像
所谓镜像即为在镜子中的像,具体的实现方式如同前序遍历
这里写图片描述
故将左右子树进行交换。
具体实现如下所示:

void mirrorBinTree()
    {
        cout << "二叉树的镜像:";
        _mirrorBinTree(_pRoot);
        cout << endl;
    }
    void _mirrorBinTree(pNode pRoot)
    {
        if (pRoot == NULL)
            return;
        if (pRoot)
        {
            swap(pRoot->_pLeft, pRoot->_pRight);
            cout << pRoot->_data << "";
        }
        _mirrorBinTree(pRoot->_pLeft);
        _mirrorBinTree(pRoot->_pRight);
    }

11.判断一颗二叉树是否为完全二叉树
具体分析:
1.空树—-是
2.1个节点—–是
3.多个节点(先将根节点记录,如pNode pCur = _pRoot;)
(1)、左右子树均存在
(2)、只有左子树没有右子树
(3)、只有右子树没有左子树
(4)、没有左右子树

bool IscompleteBinTree()
    {
        return _IscompleteBinTree(_pRoot);
    }
    bool _IscompleteBinTree(pNode pRoot)//判断是否为完全二叉树
    {
        if (pRoot == NULL || (pRoot->_pLeft == NULL&&pRoot->_pRight == NULL))
            return true;
        //讨论多个节点的情况下:
        queue<pNode>q;
        pNode pCur = _pRoot;
        bool result = false;//最终的返回结果
        bool flag = false;//给出一个临界点
        while (!q.empty())
        {
            pCur = q.front();
            if (flag)//只有存在临界点时,对其进行讨论
            {
                if (pCur->_pLeft || pCur->_pRight)
                    return false;                                   
            }
            else
            {
                //以下的四种情况在讨论临界点是否存在(与层序遍历相似)
                if (pCur->_pLeft&&pCur->_pRight)
                {
                    q.push(pCur->_pLeft);
                    q.push(pCur->_pRight);
                }
                else if (pCur->_pLeft)
                {
                    q.push(pCur->_pLeft);
                    flag = true;
                }
                else if (pCur->_pRight)
                {
                  result=false;//说明这棵树不是完全二叉树
                }
                else
                    flag= true;
            }
            q.pop();
        }
        return  result;
    }

另外,后序遍历在销毁空间时使用

    void DestoryBinTree(pNode pRoot)//销毁空间
    {
        if (pRoot)
        {
            DestoryBinTree(pRoot->_pLeft);
            DestoryBinTree(pRoot->_pRight);
            delete pRoot;
            pRoot = NULL;
        }
    }

具体的代码如下:

#include<iostream>
#include<queue>
#include<stack>
using namespace std;
//给出结点
template<class T>
struct BinTreeNode
{
    BinTreeNode<T>*_pLeft;
    BinTreeNode<T>*_pRight;
    T _data;
    BinTreeNode(const T&data)//构造函数
        :_pLeft(NULL)
        , _pRight(NULL)
        , _data(data)
    {}
};
template<class T>
class BinTree
{
    typedef BinTreeNode<T>*pNode;
    typedef BinTreeNode<T>Node;
public:
    BinTree()
        :_pRoot(NULL)
    {}
    BinTree(const T*array, size_t size, const T&invalid)//构造函数
    {
        size_t index = 0;
        CreateBinTree(_pRoot, array, size, index, invalid);
    }
    BinTree(const BinTree<T>&bt)//拷贝构造函数
    {
        _pRoot = copyBinTree(bt._pRoot);
    }
    BinTree<T>&operator=(const BinTree<T>&bt)//赋值运算符重载
    {
        if (this != &bt)
        {
            DestoryBinTree(_pRoot);
            _pRoot = copyBinTree(bt._pRoot);
        }
        return *this;
    }
    ~BinTree()//析构函数
    {
        DestoryBinTree(_pRoot);
    }
    void preorder()
    {
        cout << "前序遍历:";
        _preorder(_pRoot);
        cout << endl;
    }
    void _preorder(pNode pRoot)//前序遍历的实现
    {
        if (pRoot)
        {
            cout << pRoot->_data << " ";
            _preorder(pRoot->_pLeft);
            _preorder(pRoot->_pRight);
        }
    }
    void preorder_nor()
    {
        cout << "前序遍历的非递归:";
        _preorder_nor(_pRoot);
        cout << endl;
    }
    void _preorder_nor(pNode pRoot)//前序遍历非递归的实现
    {
        if (_pRoot == NULL)
        {
            return;
        }
        stack<pNode>s;
        s.push(_pRoot);
        while (!s.empty())
        {
            pNode pTop = s.top();
            cout << pTop->_data << "";
              s.pop();
              if (pTop->_pRight)
              {
                  s.push(pTop->_pRight);
              }
              if (pTop->_pLeft)
              {
                  s.push(pTop->_pLeft);
              }
        }
    }
    void midorder()
    {
        cout << "中序遍历:";
        _midorder(_pRoot);
        cout << endl;
    }
    void _midorder(pNode pRoot)//中序遍历的实现
    {
        if (pRoot)
        {
            _midorder(pRoot->_pLeft);
            cout << pRoot->_data << " ";
            _midorder(pRoot->_pRight);
        }
    }
    void midorder_nor()
    {
        cout << "二叉树的中序遍历非递归:";
        _midorder_nor(_pRoot);
        cout << endl;
    }
    void _midorder_nor(pNode pRoot)//中序非递归的实现
    {
        if (pRoot == NULL)
        {
            return;
        }
        stack<pNode>s;
        pNode cur = _pRoot;
        //1.找到当前节点为根的左侧路径中的节点
        while (cur || !s.empty())
        {
            while (cur)
            {
                s.push(cur);
                cur = cur->_pLeft;//将其左侧的节点中的元素压入栈中
            }
            //取出栈顶元素并进行访问
            cur = s.top();
            cout << cur->_data << " ";
            s.pop();
            //将右子树压入栈中
            cur = cur->_pRight;
        }
    }
    void pNextorder()
    {
        cout << "后序遍历" << " ";
        _pNextorder(_pRoot);
        cout << endl;
    }
    void _pNextorder(pNode pRoot)//后序遍历的实现
    {
        if (pRoot)
        {
            _pNextorder(pRoot->_pLeft);
            _pNextorder(pRoot->_pRight);
            cout << pRoot->_data << " ";
        }
    }
    void pNextorder_nor()
    {
        cout << "后序遍历非递归:";
        _pNextorder_nor(_pRoot);
        cout << endl;
    }
    void _pNextorder_nor(pNode pRoot)//后序遍历非递归
    {
        if (_pRoot == NULL)
        {
            return;
        }
        stack<pNode>s;
        pNode pcur = _pRoot;
        pNode prev = NULL;
        while (pcur || !s.empty())
        {
            while (pcur)
            {
                s.push(pcur);
                pcur = pcur->_pLeft;
            }
            if (!s.empty())
            {
                pcur = s.top();
            }
            if (pcur->_pRight == NULL || pcur->_pRight == prev)
            {
                cout << pcur->_data << " ";
                prev = pcur;//将当前结点地址赋值pre作为下一次判断标志,防止重复访问  
                s.pop();
                pcur = NULL;
            }
            else
                pcur = pcur->_pRight;
        }
    }
    void Levelorder()//层序遍历
    {
        cout << "层序遍历" << " ";
        _Levelorder();
        cout << endl;
    }
    void _Levelorder()//层序遍历的实现(应用了队列,先进先出)
    {
        if (_pRoot == NULL)
        {
            return;
        }
        queue<pNode>q;//定义一个队列
        q.push(_pRoot);//根节点入队
        while (!q.empty())
        {
            pNode cur = q.front(); //将队头取出
            cout << cur->_data << " ";
            if (cur->_pLeft)
            {
                q.push(cur->_pLeft);//左子树入队
            }
            if (cur->_pRight)
            {
                q.push(cur->_pRight);//右子树入队
            }
            q.pop();
        }
        cout << endl;
    }
    void mirrorBinTree()
    {
        cout << "二叉树的镜像:";
        _mirrorBinTree(_pRoot);
        cout << endl;
    }
    void _mirrorBinTree(pNode pRoot)
    {
        if (pRoot == NULL)
            return;
        if (pRoot)
        {
            swap(pRoot->_pLeft, pRoot->_pRight);
            cout << pRoot->_data << "";
        }
        _mirrorBinTree(pRoot->_pLeft);
        _mirrorBinTree(pRoot->_pRight);
    }
    bool IscompleteBinTree()
    {
        return _IscompleteBinTree(_pRoot);
    }
    bool _IscompleteBinTree(pNode pRoot)//判断是否为完全二叉树
    {
        if (pRoot == NULL || (pRoot->_pLeft == NULL&&pRoot->_pRight == NULL))
            return true;
        //讨论多个节点的情况下:
        queue<pNode>q;
        pNode pCur = _pRoot;
        bool result = false;//最终的返回结果
        bool flag = false;//给出一个临界点
        while (!q.empty())
        {
            pCur = q.front();
            if (flag)//只有存在临界点时,对其进行讨论
            {
                if (pCur->_pLeft || pCur->_pRight)
                    return false;                                   
            }
            else
            {
                //以下的四种情况在讨论临界点是否存在(与层序遍历相似)
                if (pCur->_pLeft&&pCur->_pRight)
                {
                    q.push(pCur->_pLeft);
                    q.push(pCur->_pRight);
                }
                else if (pCur->_pLeft)
                {
                    q.push(pCur->_pLeft);
                    flag = true;
                }
                else if (pCur->_pRight)
                {
                  result=false;//说明这棵树不是完全二叉树
                }
                else
                    flag= true;
            }
            q.pop();
        }
        return  result;
    }
    size_t size()
    {
        cout << "二叉树节点个数为:";
        return Size(_pRoot);
    }
    size_t GetLeefCount()
    {
        cout << "二叉树中叶子节点个数:";
        return _GetLeefCount(_pRoot);
    }
    size_t GetKcount(size_t k)
    {
        cout << "二叉树第K层节点个数";
        return _GetKcount(_pRoot, k);
    }
    size_t Height()
    {
        cout << "二叉树的高度为:";
        return _Height(_pRoot);
    }
    pNode Find(const T&data)
    {
        //cout << "找的节点为:";
        return _Find(_pRoot, data);
    }
    pNode parent(pNode pNode)
    {
        cout << "该节点的双亲节点为:";
        return _parent(_pRoot, pNode);
    }
    pNode Leftchild(pNode pNode)
    {
        cout << "该节点的左孩子为:";
        return (NULL == pNode) ? NULL : pNode->_pLeft;
    }
    pNode Rightchild(pNode pNode)
    {
        cout << "该节点的右孩子为:";
        return (NULL == pNode) ? NULL : pNode->_pRight;
    }
    size_t Size(pNode pRoot)//二叉树节点个数
    {
        if (NULL == pRoot)
            return 0;
        return Size(pRoot->_pLeft) + Size(pRoot->_pRight) + 1;
    }
    size_t _GetLeefCount(pNode pRoot)//二叉树叶子节点的个数
    {
        if (pRoot == NULL)
        {
            return 0;
        }
        if (pRoot->_pLeft == NULL&&pRoot->_pRight == NULL)
        {
            return 1;
        }
        return _GetLeefCount(pRoot->_pLeft) + _GetLeefCount(pRoot->_pRight);
    }
    size_t  _GetKcount(pNode pRoot, size_t k)//二叉树中第K层的节点个数
    {
        if (pRoot == NULL || k <= 0)
        {
            return 0;
        }
        if (k == 1)
        {
            return 1;
        }
        return _GetKcount(pRoot->_pLeft, k - 1) + _GetKcount(pRoot->_pRight, k - 1);//上一层的左右节点之和
    }
    size_t _Height(pNode pRoot)//二叉树的高度
    {
        if (pRoot == NULL)
        {
            return 0;
        }
        if (pRoot->_pLeft == NULL&&pRoot->_pRight == NULL)
        {
            return 1;
        }
        size_t leftHeight = _Height(pRoot->_pLeft);
        size_t rightHeight = _Height(pRoot->_pRight);
        return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
    }
    pNode _Find(pNode pRoot, const T&data)//找一个节点
    {
        if (pRoot == NULL)
        {
            return NULL;
        }
        /*if (pRoot->_data == data)
        {
            return pRoot;
        }
        pNode ret = NULL;
        if (ret == (_Find(pRoot->_pLeft, data)))
        {
            return ret;
        }
        return _Find(_pRoot->_pRight, data);*/
        if (data == pRoot->_data)
            return pRoot;
        if (pRoot->_pLeft)
        {
            _Find(pRoot->_pLeft, data);
        }
        if (pRoot->_pRight)
        {
            _Find(pRoot->_pRight, data);
        }
    }
    //pNode _parent(pNode pRoot, pNode pNode)//找寻pNode的双亲节点
    //{
    //  if (pRoot == pNode || pRoot == NULL || pNode == NULL)
    //  {
    //      return NULL;
    //  }
    //  if (pRoot->_pLeft == pNode || pRoot->_pRight == pNode)
    //  {
    //      return pRoot;
    //  }
    //  pNode parent=NULL;
    //  if (_parent(pRoot->_pLeft, pNode) == parent)
    //  {
    //      return parent;
    //  }
    //  if (_parent(pRoot->_pRight, pNode) == parent)
    //  {
    //      return parent;
    //  }
    //}
    pNode _parent(pNode pRoot, pNode pNode)
    {
        if (pRoot == pNode || pRoot == NULL || pNode == NULL)
        {
             return NULL;
        }
        if (pRoot == pNode || pRoot == NULL || pNode == NULL)
         {
           return NULL;
         }
        if (pNode == pRoot->_pLeft || pNode == pRoot->_pRight)
            return pRoot;
        if (pRoot->_pLeft)
        {
            _parent(pRoot->_pLeft, pNode);
        }
        if (pRoot->_pRight)
        {
            _parent(pRoot->_pRight, pNode);
        }
        return pRoot;
    }

    pNode copyBinTree(pNode pRoot)//拷贝函数
    {
        pNode pnewroot = NULL;
        if (pRoot)
        {
            pnewroot = new Node(pRoot->_data);//拷贝根节点
        }
            if (pRoot->_pLeft)
            {
                pnewroot->_pLeft = copyBinTree(pRoot->_pLeft);//拷贝左子树
            }
            if (pRoot->_pRight)
            {
                pnewroot->_pRight = copyBinTree(pRoot->_pRight);//拷贝右子树
            }
        return pnewroot;
    }
    void DestoryBinTree(pNode pRoot)//销毁空间
    {
        if (pRoot)
        {
            DestoryBinTree(pRoot->_pLeft);
            DestoryBinTree(pRoot->_pRight);
            delete pRoot;
            pRoot = NULL;
        }
    }
private:
    void CreateBinTree(pNode&pRoot, const T*array, size_t size, size_t &index, const T&invalid)//二叉树的建立
    {
        //先进行判断
        if (index < size&&'#' != array[index])
        {
             pRoot = new Node(array[index]);//创建根结点
            //创建左子树
            CreateBinTree(pRoot->_pLeft, array, size, ++index, invalid);
            //创建右子树
            CreateBinTree(pRoot->_pRight, array, size, ++index, invalid);
        }
    }
    pNode _pRoot;
};
void test()
{
    char array[] = "ABD###CE##F";
    BinTree<char>bt(array,strlen(array),'#');
    bt.preorder_nor();
    //bt.midorder();
    //bt.midorder_nor();
    //bt.pNextorder();
    //bt.pNextorder_nor();
    bt.mirrorBinTree();//
    bt.mirrorBinTree();//两次镜像显示就看它是否正确
    cout << boolalpha << bt.IscompleteBinTree() << endl;
    cout << bt.size() << endl;
    cout << bt.GetLeefCount() << endl;
    cout << bt.GetKcount(3) << endl;
    cout << bt.Height() << endl;
    cout << bt.parent(bt.Find('C'))->_data << endl;
    //cout << bt.Leftchild(bt.Find('A'))->_data << endl;
    cout << bt.Rightchild(bt.Find('A'))->_data << endl;
    BinTree<char>bt1(bt);//拷贝构造函数
    bt1.preorder();
    bt1.pNextorder();
    bt1.Levelorder();
    BinTree<char>bt2;//赋值
    bt2 = bt;
    bt2.preorder();
}
int main()
{
    test();
    system("pause");
    return 0;
}

这里写图片描述
希望各位能给与意见及建议。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值