二叉树的简单实现(递归算法)

在计算机科学中,二叉树是每个节点最多有两个子树的树结构。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树有几个常用操作,这里只写了构造二叉树,前序遍历,中序遍历,后序遍历,层序遍历,求树的总结点,叶节点,树的深度,树中第k层的节点数和在树中查找一个节点。主要用递归算法实现。
前序遍历:根,左,右;
中序遍历:左,根,右;
后序遍历:左,右,根;

#pragma once
#include <queue>
#include<iostream>
using namespace std;

template <class T>                  
struct BinaryTreeNode     //定义二叉树的节点
{
    BinaryTreeNode<T>* _left;
    BinaryTreeNode<T>* _right;
    T _data;
    BinaryTreeNode(const T& data)
        :_data(data)
        , _left(NULL)
        , _right(NULL)
    {}
};
template<class T>
class BinaryTree
{
    typedef BinaryTreeNode<T> Node;
public:
    BinaryTree()                 //构造二叉树 invalid为非法值
        :_root(NULL)
    {}
    BinaryTree(T* a, size_t n, const T& invalid)
    {
        size_t index = 0;
        _root = CreateTree(a, n, invalid, index);
    }
    BinaryTree(const BinaryTree<T>& t)   //拷贝构造函数 
    {
        _root = _CopyBinaryTree(t._root);
    }
    BinaryTree<T>& operator=(const BinaryTree<T>& t)//赋值运算符的重载
    {
        _root = _CopyBinaryTree(t._root);
        return *this;
    }
    //前序遍历
    void PrevOrder()
    {
        _PrevOrder(_root);
        cout << endl;
    }
    //中序遍历
    void InOrder()
    {
        _InOrder(_root);
        cout << endl;
    }
    //后序遍历
    void PostOrder()
    {
        _PostOrder(_root);
        cout << endl;
    }
    //层序遍历
    void LevelOrder()
    {
        _LevelOrder(_root);

    }
    //节点数
    size_t _Size()
    {
        return _Size(_root);
    }
    //叶子节点数
    size_t LeafSize()
    {
        return LeafSize(_root);
    }
    //深度
    size_t Depth()
    {
        return _Depth(_root);
    }
    //第K层的节点个数
    size_t GetKLevel(size_t k)
    {
        return _GetKLevel(_root, k);
    }
    Node* find(const T& x)
    {
        return _find(_root,x);
    }
protected:
    Node* CreateTree(T* a, size_t n, const T& invalid, size_t& index)
    { 
        //index要引用,否则上层递归的++index对上层没有任何影响
        Node* root = NULL;
        if (index < n&&a[index] != invalid)
        {
            root = new Node(a[index]);
            root->_left = CreateTree(a, n, invalid, ++index);
            root->_right = CreateTree(a, n, invalid, ++index);
        }
        return root;
    }
    Node* _CopyBinaryTree(Node* root)
    {
        if (NULL == root)
        {
            return NULL;
        }
        Node* newRoot = new Node(root->_data);
        newRoot->_left = _CopyBinaryTree(root->_left);
        newRoot->_right = _CopyBinaryTree(root->_right);
        return newRoot;
    }
    void Destroy(Node* root)
    {
        if (NULL == root)
            return;
        Destroy(root->_left);
        Destroy(root->_right);
        delete root;
    }
    //前序遍历
    void _PrevOrder(Node* root)
    {
        if (NULL == root)
            return;
        cout << root->_data << " ";
        _PrevOrder(root->_left);
        _PrevOrder( root->_right);
    }
    //中序遍历
    void _InOrder(Node* root)
    {
        if (NULL == root)
            return;
        _InOrder(root->_left);
        cout << root->_data << " ";
        _InOrder(root->_right);

    }
    //后序遍历
    void _PostOrder(Node* root)
    {
        if (NULL == root)
            return;
        _PostOrder(root->_left);
        _PostOrder(root->_right);
        cout << root->_data << " ";
    }
    //层序遍历(利用队列实现,先进先出)
    void _LevelOrder(Node* root)
    {
        queue<Node*>q;
        if (NULL != root)
            q.push(root);
        while (!q.empty())
        {
            Node* front = q.front();
            cout << front->_data << " ";
            q.pop();
            if (NULL != front->_left)
                q.push(front->_left);
            if (NULL != front->_right)
                q.push(front->_right);
        }
        cout << endl;
    }
    //查找
    Node* _find(Node* root, const T& x)
    {
        if (NULL == root)
            return 0;
        if (x == root->_data)
            return root;
        Node* ret = _find(root->_left,x)
        if (ret)
            return ret;
        return _find(root->_right,x);
    }
    //节点数
    size_t _Size(Node* root)
    {
        if (NULL == root)
            return 0;
        return _Size(root->_left) + _Size(root->_right) + 1;

    }
    //叶子节点数
    void _LeafSize(Node* root)
    {
        if (NULL == root)
            return 0;
        if ((NULL == root->_left) && (NULL == root->_right))
        {
            return 1;
        }
        return _LeafSize(root->_left) + _LeafSize(root->_right);

    }
    //深度
    size_t _Depth(Node* root)
    {
        if (NULL == root)
            return 0;
        if ((NULL == root->_left) && (NULL == root->_right))
            return 1;
        size_t left = _Depth(root->_left);
        size_t right = _Depth(root->_right);
        return left > right ? left + 1 : right + 1;
    }
    //第K层节点数
    size_t _GetKLevel(Node* root, size_t k)
    {
        if (NULL == root)
            return 0;
        if (1 == k)
            return 1;
        return _GetKLevel(root->_left, k - 1) + _GetKLevel(root->_right, k - 1);
    }

    protected:
        Node* _root;
};
int main()
{
int a[] = { 5,3,2,0,0,4,0,0,7,0,8,0,1 };
    BinaryTree<int> t(a,13,0),tree;
    tree = t;
    cout<<tree.Depth()<<endl;
    cout << tree._Size() << endl;
    cout << tree.LeafSize() << endl;
    tree.LevelOrder();
    tree.InOrder();
    tree.PostOrder();
    tree.PrevOrder();
    cout << tree.GetKLevel(2) << endl;
    cout << tree.find(5) << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qiting00/article/details/72628909