简单实现二叉树

简单的实现一个二叉树所需要的基本要求,后续进行其他的更新。

二叉树最重要的是递归的思想,在这里我想请你去参考我上一篇的广义表,那个思想比起二叉树来说更加简单一些。

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<cstdlib>
#include<cassert>
#include<queue>
using namespace std;

template<typename  T>
struct BinaryTreeNode
{
    BinaryTreeNode(const T& d)
    :_value(d)
    , _left(NULL)
    , _right(NULL)
    {

    }
    T _value;
    BinaryTreeNode *_left;
    BinaryTreeNode *_right;
};

template<typename T>
class BinaryTree
{
public: 
    typedef BinaryTreeNode<T> Node;

public:
    BinaryTree() = default;
    BinaryTree(T *a, size_t size,const T& invaild)
    {
        size_t index = 0;
        _root = _CreateBinaryTree(a,  size,  index,  invaild);
    }
    ~BinaryTree()
    {
        _root=_Destory(_root);
    }
    BinaryTree(const BinaryTree<T> &b)
    {
        _root = _Copy(b._root);
    }

    传统写法
    BinaryTree<T > operator=(const BinaryTree<T> &b)
    {
        if (this != &b)
        {
            Node* tmp = _Copy(b._root);
            _root=_Destory(_root);
            _root = tmp;
        }
        return *this;
    }

    //现代写法
    /*BinaryTree<T >operator =(const BinaryTree<T>& d)
    {
        if (this != &d)
        {
            BinaryTree<T > tmp(d);
            std::swap(tmp._root,_root);
        }
        return *this;

    }*/

    void Preorderprint()
    {
        _PreOrder(_root);
        cout << endl;

    }
    void InfixPrint()
    {
        _InfixOrder(_root);
        cout << endl;

    }
    void PostPrint()
    {
        _PostOrder(_root);
        cout << endl;

    }
    void LevelPrint()
    {
        _LevelPrint(_root);
        cout << endl;
    }
    size_t Size()
    {

        return _size(_root);
    }
    size_t Depth()
    {
        return _Depth(_root);
    }
    size_t GetKLevel(const size_t & k)
    {
        return _GetKLevel(k);
    }
    size_t Leafsize()
    {
        return _Leafsize(_root);
    }
protected:
    size_t _Depth(Node *root)
    {
        size_t maxdepth = 0;
        if (root != NULL)
        {
            size_t depth = 1;
            if (root->_left != NULL)
            {
                depth += _Depth(root->_left) ;
            }
            if (depth > maxdepth)
            {
                maxdepth = depth;
            }
            if (root->_right != NULL)
            {
                depth = _Depth(root->_right) + 1;
            }
            if (depth > maxdepth)
            {
                maxdepth = depth;
            }
        }
        return maxdepth;
    }
    size_t _Leafsize(Node * root)
    {
        int size = 0;
        if (root != NULL)
        {
            if (root->_left == NULL&&root->_right == NULL)
            {
                size += 1;
            }
                size += _Leafsize(root->_left);
                size += _Leafsize(root->_right);


        }
        return size;
    }

    void _LevelPrint(Node* root)
    {
        queue<Node *> q;
        q.push(root);
        while (!q.empty())
        {
            Node* tmp = q.front();
            cout << tmp->_value << " ";
            q.pop();
            if (tmp->_left != NULL)
            {
                q.push(tmp->_left);
            }
            if (tmp->_right != NULL)
            {
                q.push(tmp->_right);
            }

        }
    }

    size_t _GetKLevel(k)
    {

    }

    Node* _Copy(Node *root)
    {
        Node * NewNode = NULL;
        if (root != NULL)
        {
            NewNode = new Node(root->_value);
            NewNode->_left = _Copy(root->_left);
            NewNode->_right = _Copy(root->_right);
        }
        return NewNode;
    }

    size_t _size(Node *root)
    {
        size_t size = 0;
        if (root != NULL)
        {
            size += 1;
            size += _size(root->_left);
            size += _size(root->_right);
        }
        return size;
    }

    Node * _CreateBinaryTree(T *a, size_t size, size_t &index,const T& invaild)
    {
        Node *newnode = NULL;
        if (index < size&&a[index] != invaild)
        {
            newnode = new Node(a[index]);
            newnode->_left = _CreateBinaryTree(a, size, ++index, invaild);
            newnode->_right = _CreateBinaryTree(a, size, ++index, invaild);
        }

        return newnode;
    }

    Node* _Destory(Node *root)
    {
        if (root != NULL)
        {
            root->_left=_Destory(root->_left);
            root->_right=_Destory(root->_right);
            delete root;
            root = NULL;
        }
        return root;
    }

    void _PreOrder(Node* root)
    {
        if (root != NULL)
        {
            cout << root->_value << " ";
            _PreOrder(root->_left);
            _PreOrder(root->_right);
        }
    }
    void _InfixOrder(Node* root)
    {
        if (root != NULL)
        {
            _InfixOrder(root->_left);
            cout << root->_value << " ";
            _InfixOrder(root->_right);
        }

    }
    void _PostOrder(Node* root)
    {
        if (root != NULL)
        {
            _PostOrder(root->_left);
            _PostOrder(root->_right);
            cout << root->_value << " ";
        }
    }

protected:
    Node* _root;

};

void test1()
{
    int a[10] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6 };
    int *ar = NULL;
    int arr[15] = { 1, 2, '#', 3, '#', '#', 4, 5, '#', 6, '#', 7, '#', '#', 8 };
    BinaryTree<int > b(a, 10, (int )'#');
    BinaryTree<int > bt(arr, 15, (int )'#');
    BinaryTree<int > c(ar, 0, (int)'#');
    BinaryTree<int > d;


    cout<<d.Depth()<<endl;
    //bt.LevelPrint();
    //cout<<bt.Leafsize() << endl;
    //cout << bt.Depth() << endl;
    //cout << b.Leafsize() << endl;
    //
    //cout <<c.Leafsize()<< endl;

    //cout << b.Size() << endl;
    //b.Preorderprint();
    //b.InfixPrint();
    //b.PostPrint();
    //cout << endl;
    //cout << d.Size() << endl;
    //d.Preorderprint();
    //d.InfixPrint();
    //d.PostPrint();
    //cout<<c.size() << endl;
    //c.InfixPrint();
    //c.PostPrint();
    //c.Preorderprint();

    /*cout << d.size() << endl;
    d.InfixPrint();
    d.PostPrint();
    d.Preorderprint();*/
    /*cout<<bt.size() << endl;
    bt.Preorderprint();
    bt.InfixPrint();
    bt.PostPrint();*/
    cout << endl;

    /*b.Preorderprint();
    b.InfixPrint();
    b.PostPrint();
    cout << b.size() << endl;*/

}

int main()
{
    test1();
    system("pause");
    return 0;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值