zoukankan      html  css  js  c++  java
  • 二叉树的实现

    关于二叉树的功能有二叉树的创建和销毁,前序遍历,中序遍历,后续遍历,求二叉树中节点的个数,求二叉树的深度,查找二叉树中的某一个节点

    #include<iostream>
    using namespace std;


    template<class T>
    struct BinaryTreeNode
    {
        T _data;
        BinaryTreeNode<T>* _LeftChild;
        BinaryTreeNode<T>* _RightChild;

        BinaryTreeNode(const T& x)
            :_data(x)
            , _LeftChild(NULL)
            , _RightChild(NULL)
        {}
    };

    template<class T>
    class BinTree
    {
    public:
        BinTree() 
            :_root(NULL)
        {}

        BinTree(const T* a, size_t size)
        {
            size_t index = 0;
            _root=_CreateBinTree(a, size, index);
        }

        BinTree(const BinTree<T> &t)
        {
            _root = Copy(t._root);
        }

        BinTree<T>& operator=(BinTree<T> t)
        {
            swap(t._root, _root);
            return *this;
        }

        ~BinTree()   //销毁二叉树
        {
            _Destroy(_root);
        }
        
        void _PreOrder()    //前序遍历
        {
            PreOrder(_root);
        }

        void _InOrder()     //中序遍历
        {
            InOrder(_root);
        }

        void _PostOrder()    //后续遍历
        {
            PostOrder(_root);
        }

        void _LevelOrder()   //层次遍历
        {
             LevelOrder(_root);
        }

        int _Size()     //求二叉树节点的个数
        {
            return Size(_root);
        }

        int _Depth()     //求二叉树的深度
        {
            return Depth(_root);
        }

        BinaryTreeNode<T>* _Find(const T& x)    //查找二叉树中的某个节点
        {
            return Find(_root, x);
        }

    protected:
        BinaryTreeNode<T>*  _CreateBinTree(const T* a, size_t size, size_t& index)
        {
            BinaryTreeNode<T>* root = NULL;
            if (index < size&&a[index] != '#')
            {
                root = new BinaryTreeNode<T>(a[index]);
                root->_LeftChild=_CreateBinTree(a, size, ++index);
                root->_RightChild=_CreateBinTree(a, size, ++index);
            }
            return root;
        }

        void _Destroy(BinaryTreeNode<T>*& root)
        {
            if (root == NULL)
                return;
            _Destroy(root->_LeftChild);
            _Destroy(root->_RightChild);
            delete root;
        }

        void PreOrder(BinaryTreeNode<T>* root)      //递归
        {
            if (root == NULL)
                return;
            cout << root->_data << "  ";
            PreOrder(root->_LeftChild);
            PreOrder(root->_RightChild);
        }

       

        void InOrder(BinaryTreeNode<T>* root)
        {
            if (root == NULL)
            return;

            InOrder(root->_LeftChild);
            cout << root->_data << "  ";
            InOrder(root->_RightChild);
        }

        void PostOrder(BinaryTreeNode<T>* root)
        {
            if (root == NULL)
                return;

            InOrder(root->_LeftChild);
            InOrder(root->_RightChild);
            cout << root->_data << "  ";
        }

        void LevelOrder(BinaryTreeNode<T>* root)
        {
            if (root == NULL)
                return;
            queue<BinaryTreeNode<T>*> q;

            q.push(root);
            while (!q.empty())
            {
                BinaryTreeNode<T> *cur = q.front();
                cout << cur->_data << "  ";
                q.pop();
                if (cur->_RightChild)
                    q.push(cur->_RightChild);
                if (cur->_LeftChild)
                   q.push(cur->_LeftChild);
            }
        }

        int Size(BinaryTreeNode<T>* root)
        {
            if (root == NULL)
                return 0;

            return Size(root->_LeftChild) + Size(root->_RightChild) + 1;
        }
        
        int Depth(BinaryTreeNode<T>* root)
        {
            if (root == NULL)
                return 0;
            int left = Depth(root->_LeftChild);
            int right = Depth(root->_RightChild);
            return (left > right ? left :right)+1;
        }

        BinaryTreeNode<T>* Find(BinaryTreeNode<T>* root,const T& x)
        {
            if (root == NULL)
                return NULL;

            if (root->_data == x)
                return root;
            else
            {
                BinaryTreeNode<T>* cur = Find(root->_LeftChild, x);
                while (cur != NULL)
                {
                    if (cur-> _data == x)
                        return cur;
                }
                return Find(root->_RightChild, x);
            }
        }

        BinaryTreeNode<T>* Copy(BinaryTreeNode<T>* root)
        {
            BinaryTreeNode<T>* NewRoot = NULL;
            if (root)
            {
                NewRoot = new BinaryTreeNode<T>(root->_data);
                NewRoot->_LeftChild =Copy(root->_LeftChild);
                NewRoot->_RightChild =Copy(root->_RightChild);
            }
            return NewRoot;
        }
    private:
        BinaryTreeNode<T>* _root;
    };

  • 相关阅读:
    容斥原理算法总结(bzoj 2986 2839)
    网络流系列算法总结(bzoj 3438 1061)
    bzoj 2746: [HEOI2012]旅行问题 AC自动机fail树
    bzoj 3283: 运算器 扩展Baby Step Giant Step && 快速阶乘
    计算几何考场绘图技巧
    bzoj 1845: [Cqoi2005] 三角形面积并 扫描线
    bzoj 3784: 树上的路径 堆维护第k大
    BZOJ 1231: [Usaco2008 Nov]mixup2 混乱的奶牛
    BZOJ 1112: [POI2008]砖块Klo
    BZOJ 1003: [ZJOI2006]物流运输trans DP+最短路
  • 原文地址:https://www.cnblogs.com/qingjiaowoxiaoxioashou/p/5915454.html
Copyright © 2011-2022 走看看