二叉树

递归是利用栈桢来实现的,而非递归则是用Stack来实现,递归相比较非递归来说,缺陷在栈桢储存在栈上,有可能发生栈溢出。
二叉树代码如下

#pragma once
#include <queue>
#include <stack>

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

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

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 = CreateTree(a, n, invalid, index);
    }

    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;
    }

    BinaryTree(const BinaryTree<T>& t)
    {
        _root = _Copy(t._root);
    }

    // t1 = t2
    /*BinaryTree<T>& operator=(const BinaryTree<T>& t)
    {
        if (this != &t)
        {
            Destroy(_root);
            _root = _Copy(t._root);
        }

        return *this;
    }*/

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

    ~BinaryTree()
    {
        Destroy(_root);
    }

    void Destroy(Node* root)
    {
        if (root == NULL)
            return;

        Destroy(root->_left);
        Destroy(root->_right);
        delete root;
    }

    void PrevOrder()
    {
        _PrevOrder(_root);
        cout<<endl;
    }

    void PrevOrderNonR()
    {
        stack<Node*> s;
        Node* cur = _root;

        while (cur || !s.empty())
        {
            while (cur)
            {
                cout<<cur->_data<<" ";
                s.push(cur);
                cur = cur->_left;
            }

            // top从栈取出来表示这个节点的左子树访问过了
            // 剩余右子树还没访问,循环子问题访问右树
            Node* top = s.top();
            s.pop();

            // 子问题的方式访问右树
            cur = top->_right;
        }

        cout<<endl;
    }

    void InOrder()
    {
        _InOrder(_root);
        cout<<endl;
    }

    void InOrderNonR()
    {
        Node* cur = _root;
        stack<Node*> s;
        while (cur || !s.empty())
        {
            while (cur)
            {
                s.push(cur);
                cur = cur->_left;
            }

            Node* top = s.top();
            s.pop();

            cout<<top->_data<<" ";

            // 子问题
            cur = top->_right;
        }
    }

    void PostOrderNonR()
    {
        Node* cur = _root;
        stack<Node*> s;
        Node* prev = NULL;

        while (cur || !s.empty())
        {
            while (cur)
            {
                s.push(cur);
                cur = cur->_left;
            }

            Node* front = s.top();
            if (front->_right == NULL || front->_right == prev)
            {
                cout<<front->_data<<" ";
                prev = front;
                s.pop();
            }
            else
            {
                // 子问题
                cur = front->_right;
            }
        }
        cout<<endl;
    }

    void LevelOrder()
    {
        queue<Node*> q;
        if (_root)
            q.push(_root);

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

            q.pop();
        }

        cout<<endl;
    }

    size_t Size()
    {
        return _Size(_root);
    }

    size_t LeafSize()
    {
        return _LeafSize(_root);
    }

    size_t GetKLevel(size_t k)
    {
        assert(k > 0);

        return _GetKLevel(_root, k);
    }

    size_t Depth()
    {
        return _Depth(_root);
    }

    Node* Find(const T& x)
    {
        return _Find(_root, x);
    }

    // 不关注增删查改--没有意义

protected:

    Node* _Find(Node* root, const T& x)
    {
        if(root == NULL)
            return NULL;

        if (root->_data == x)
            return root;

        Node* ret = _Find(root->_left, x);
        if (ret)
            return ret;

        return _Find(root->_right, x);
    }

    size_t _Depth(Node* root)
    {
        if (root == NULL)
            return 0;

        if (root->_left == NULL && root->_right == NULL)
            return 1;

        size_t left = _Depth(root->_left);
        size_t right = _Depth(root->_right);

        return left > right ? left+1 : right+1;
    }

    size_t _GetKLevel(Node* root, size_t k)
    {
        if (root == NULL)
            return 0;

        if (k == 1)
            return 1;

        return _GetKLevel(root->_left, k-1) + _GetKLevel(root->_right, k-1);
    }

    size_t _LeafSize(Node* root)
    {
        if (root == NULL)
        {
            return 0;
        }

        if (root->_left == NULL && root->_right == NULL)
        {
            return 1;
        }

        return _LeafSize(root->_left) + _LeafSize(root->_right);
    }

    void _InOrder(Node* root)
    {
        if (root == NULL)
            return;

        _InOrder(root->_left);
        cout<<root->_data<<" ";
        _InOrder(root->_right);
    }

    size_t _Size(Node* root)
    {
        if (root == NULL)
            return 0;

        return _Size(root->_left) + _Size(root->_right) +1;
    }

    void _PrevOrder(Node* root)
    {
        if (root == NULL)
            return;

        cout<<root->_data<<" ";
        _PrevOrder(root->_left);
        _PrevOrder(root->_right);
    }

    // 构建当前树或者子树
    Node* CreateTree(T* a, size_t n, const T& invalid, size_t& 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;
    }

protected:
    Node* _root;
};

void TestBinaryTree()
{
    int array [10] = {1, 2, 3, '#', '#', 4, '#', '#', 5, 6};
    BinaryTree<int> t1(array, sizeof(array)/sizeof(array[0]), '#');
    t1.PrevOrder();

    BinaryTree<int> t2(t1);
    t2.PrevOrderNonR();

    //t1.PrevOrderNonR();
    //t1.PostOrderNonR();

    //t1.InOrder();
    //t1.LevelOrder();

    //cout<<"Size?"<<t1.Size()<<endl;
    //cout<<"K Level?"<<t1.GetKLevel(3)<<endl;
    //cout<<"K Level?"<<t1.GetKLevel(4)<<endl;
    //cout<<"Leaf Size?"<<t1.LeafSize()<<endl;
    //cout<<"Depth?"<<t1.Depth()<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值