二叉树的简单实现

二叉树是常见的数据结构掌握它还是很有必要滴

#pragma once

template<class T>
struct BinaryTreeNode
{
    T _data;
    BinaryTreeNode<T>* _left;
    BinaryTreeNode<T>* _right;

    BinaryTreeNode(const T& x)
        :_data(x)
        , _left(NULL)
        , _right(NULL)
    {}

};

template<class T>
class BinaryTree{
    typedef BinaryTreeNode<T> Node;
public:
    BinaryTree()
        :_root(NULL)
    {}
    BinaryTree(T* a, size_t n, const T& invalid)
    {
        size_t index=0;
        _root=createBinaryTree(a,n, invalid, index);
    }
    BinaryTree(const BinaryTree<T>& t)
    {
        _root = _Copy(t._root);
    }
    BinaryTree<T>& operator=(BinaryTree<T>& t)
    {
        if (this != &t)
        {
            swap(t._root,_root);
        }
    }
//当你传参进来的时候会创建一个跟你一模一样的局部变
//量,这个局部变量在函数调用结束后就被销毁了,
//然后现在我们让局部变量的根节点和我们的根节点交换,
//现在我们的根节点指向的就是我们需要赋值成的那棵树,
//而局部变量的根节点指向的使我们以前的内容,
//现在刚刚好在作用域结束后局部变量帮我们释放
//空间,而我们现在的根节点_root,已经指向我想要的内容了,
    ~BinaryTree()
    {
        _destory(_root);
    }

    void PrevOrder()//前序遍历 递归
    {
        cout << "PrevOrder:" << endl;
        return _prevorder(_root);
    }
    void _prevorder(Node* root)
    {
        if (root == NULL)
            return;

        cout << root->_data << " ";
        _prevorder(root->_left);
        _prevorder(root->_right);
    }
    void InOrder()//中序遍历 递归
    {
        cout << "InOrder:" << endl;
        return _inorder(_root);
    }
    void _inorder(Node* root)
    {
        if (root == NULL)
            return ;

        _inorder(root->_left);
        cout << root->_data << " ";
        _inorder(root->_right);
    }
    void PostOrder()//后序遍历 递归
    {
        cout << "PostOrder:" << endl;
        return _postorder(_root);
    }
    void _postorder(Node* root)
    {
        if (root == NULL)
            return ;

        _postorder(root->_left);
        _postorder(root->_right);
        cout << root->_data << " ";
    }
    void LevelOrder()//层序遍历
    {
        cout << "LevelOrder" << endl;
        return _levelorder(_root);
    }
    void _levelorder(Node* root)
    {
        queue<Node*> q1;
        if (root == NULL)
        {
            return;
        }
        q1.push(root);

        while (!q1.empty())
        {
            Node* front = q1.front();
            cout << front->_data << " ";
            if (front->_left)
                q1.push(front->_left);
            if (front->_right)
                q1.push(front->_right);

            q1.pop();
        }
    }

    size_t Size()
    {
        return _size(_root);
    }
    size_t _size(Node* _root)
    {
        if (_root == NULL)
            return 0;

        size_t left = _size(_root->_left);
        size_t right = _size(_root->_right);
        return left + right + 1;
    }
    size_t Depth()
    {
        return _depth(_root);
    }
    size_t _depth(Node* _root)
    {
        if (_root == NULL)
            return 0;

        size_t left = _depth(_root->_left);
        size_t right = _depth(_root->_right);
        return (left > right) ? left + 1 : right + 1;
    }
    size_t LeafSize()//叶子节点数
    {
        return _leafsize(_root);
    }
    size_t _leafsize(Node* _root)
    {
        if (_root == NULL)
            return 0;
        if (_root->_left == NULL&&_root->_right == NULL)
            return 1;
        size_t left = _leafsize(_root->_left);
        size_t right = _leafsize(_root->_right);
        return left + right;
    }
    size_t GetKLevel(size_t k)//第K层的结点数
    {
        return _getklevel(_root, k);
    }
    size_t _getklevel(Node* root, size_t k)
    {
        if (root == NULL && k<1)
            return 0;
        if (k == 1)
            return 1;
        return _getklevel(root->_left, k - 1) + _getklevel(root->_right, k - 1);
    }
    Node* Find(const T& x)
    {
        return _find(_root, x);
    }
    Node* _find(Node* root, const T& x)
    {
        if (root == NULL)
            return NULL;
        if (root->_data == x)
            return _root;
        Node* tmp = _find(root->_left, x)
        if (tmp)
        {
            return tmp;
        }
        return _find(root->_right, x);
    }
protected:
    Node* createBinaryTree(T*a, size_t n, const T& invalid,size_t& index)//注意这里传的是 index的引用 
    {
        Node* root = NULL;
        if (index<n && a[index] != invalid)
        {
            root = new Node(a[index]);
            root->_left=createBinaryTree( a, n, invalid, ++index);
            root->_right=createBinaryTree(a, n, invalid, ++index);
        }
        return root;
    }
    Node* _Copy(Node* root)
    {
        if (root == NULL)
        {
            return NULL;
        }
        Node* newroot = new Node(root->_data);
        newroot->_left = _Copy(root->_left);
        newroot->_right = _Copy(root->_right);
        return newroot;
    }
    void _destory(Node* _root)
    {
        if (_root)
        {
            _destory(_root->_left);
            _destory(_root->_right);
            delete _root;
            _root = NULL;
        }
    }
private:
    Node* _root;
    size_t index;
    size_t invalid;
};


void Test1()
{
    int a1[10] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6 };
    //int a2[15] = {1,2,'#',3,'#','#',4,5,'#',6,'#',7,'#','#',8}; 
    BinaryTree<int> t1(a1, 10, '#');
    t1.PrevOrder();
    cout << endl;
    t1.InOrder();
    cout << endl;
    t1.LevelOrder();
    cout << endl;
    cout << "t1 Size:" << t1.Size() << endl;
    cout << "t1 Depth:" << t1.Depth() << endl;
    cout << "t1 LeafSize:" << t1.LeafSize();
}

这里写图片描述

前 中 后序遍历非递归https://blog.csdn.net/Important_/article/details/81870206

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值