二叉树的遍历

二叉树的遍历 (Traversing Binay Tree) 是指从根结点出发,按照某种次序依次访问二叉树中的所有结点,使得每个结点被访问一次,且仅被访问一次。

问题

通用树结构的层次遍历算法是否可以用在二叉树结构上?如果可以,代码需要做怎样的改动?

设计思路

提供一组遍历相关的函数,按层次访问二叉树中的数据元素
函数功能说明
begin()初始化,准备进行遍历访问
next()移动游标,指向下一个结点
current()获取游标所指向的数据元素
end()判断游标是否到达尾部

层次遍历算法

  • 原料: class LinkQueue<T>;
  • 游标: LinkQueue<T>::front();
  • 思想:

    • begin() → 将根结点压入队列
    • current() → 访问队头元素指向的数据元素
    • next() → 队头元素弹出,将队头元素的孩子压入队列中(核心)
    • end() → 判断队列是否为空

层次遍历算法示例

image.png

编程实验:二叉树结构的层次遍历

【重构】文件:Tree.h

#ifndef TREE_H
#define TREE_H

#include "Object.h"
#include "TreeNode.h"
#include "SharedPointer.h"

namespace DTLib
{

template <typename T>
class Tree : public Object
{
public:
    Tree() = default;
    virtual bool insert(TreeNode<T> *node) = 0;
    virtual bool insert(const T &value, TreeNode<T> *parent) = 0;
    virtual SharedPointer<Tree<T>> remove(const T &value) = 0;
    virtual SharedPointer<Tree<T>> remove(TreeNode<T> *node) = 0;
    virtual TreeNode<T>* find(const T &value) const = 0;
    virtual TreeNode<T>* find(TreeNode<T> *node) const = 0;
    virtual TreeNode<T>* root() const = 0;
    virtual int degree() const = 0;
    virtual int count() const = 0;
    virtual int height() const = 0;
    virtual void clear() = 0;
    virtual bool begin() = 0;
    virtual bool end() = 0;
    virtual bool next() = 0;
    virtual T current() = 0;
protected:
    TreeNode<T> *m_root = nullptr;
};

}

#endif // TREE_H

【重构】文件:GTree.h

#ifndef GTREE_H
#define GTREE_H

#include "Tree.h"
#include "GTreeNode.h"
#include "Exception.h"
#include "LinkQueue.h"

namespace DTLib
{

template <typename T>
class GTree : public Tree<T>
{
public:
    GTree() = default;

    bool insert(TreeNode<T> *node) override
    {
        bool ret = true;

        if (node != nullptr)
        {
            if (this->m_root == nullptr)
            {
                node->parent = nullptr;
                this->m_root = node;
            }
            else
            {
                GTreeNode<T> *np = find(node->parent);

                if (np != nullptr)
                {
                    GTreeNode<T> *n = dynamic_cast<GTreeNode<T>*>(node);

                    if (np->child.find(n) < 0)
                    {
                        np->child.insert(n);
                    }
                }
                else
                {
                    THROW_EXCEPTION(InvalidOpertionExcetion, "Invalid partent tree node ...");
                }
            }
        }
        else
        {
            THROW_EXCEPTION(InvalidParameterExcetion, "Parameter node cannot be NULL ...");
        }

        return ret;
    }

    bool insert(const T &value, TreeNode<T> *parent) override
    {
        bool ret = true;

        GTreeNode<T> *node = GTreeNode<T>::NewNode();

        if (node != nullptr)
        {
            node->value = value;
            node->parent = parent;

            insert(node);
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to create node ...");
        }

        return ret;
    }

    SharedPointer<Tree<T>> remove(const T &value) override
    {
        GTree<T> *ret = nullptr;

        GTreeNode<T> *node = find(value);

        if (node != nullptr)
        {
            remove(node, ret);

            m_queue.clear();
        }
        else
        {
            THROW_EXCEPTION(InvalidParameterExcetion, "can not find the node ...");
        }

        return ret;
    }

    SharedPointer<Tree<T>> remove(TreeNode<T> *node) override
    {
        GTree<T> *ret = nullptr;

        node = find(node);

        if (node != nullptr)
        {
            remove(dynamic_cast<GTreeNode<T>*>(node), ret);

            m_queue.clear();
        }
        else
        {
            THROW_EXCEPTION(InvalidParameterExcetion, "Parameter node is invalid ...");
        }

        return ret;
    }

    GTreeNode<T>* find(const T &value) const override
    {
        return find(root(), value);
    }

    GTreeNode<T>* find(TreeNode<T> *node) const override
    {
       return find(root(), dynamic_cast<GTreeNode<T>*>(node));
    }

    GTreeNode<T>* root() const override
    {
        return dynamic_cast<GTreeNode<T>*>(this->m_root);
    }

    int degree() const override
    {
        return degree(root());
    }

    int count() const override
    {
        return count(root());
    }

    int height() const override
    {
        return height(root());
    }

    void clear() override
    {
        free(root());

        this->m_root = nullptr;

        m_queue.clear();
    }

    bool begin() override
    {
        bool ret = (root() != nullptr);

        if (ret)
        {
            m_queue.clear();
            m_queue.add(root());
        }

        return ret;
    }

    bool end() override
    {
        return (m_queue.length() == 0);
    }

    bool next() override
    {
        bool ret = (m_queue.length() > 0);

        if (ret)
        {
            GTreeNode<T> *node = m_queue.front();

            m_queue.remove();

            for (node->child.move(0); !node->child.end(); node->child.next())
            {
                m_queue.add(node->child.current());
            }
        }

        return ret;
    }

    T current() override
    {
        if (!end())
        {
            return m_queue.front()->value;
        }
        else
        {
            THROW_EXCEPTION(InvalidOpertionExcetion, "No value at current position ...");
        }
    }

    ~GTree()
    {
        clear();
    }

protected:
    LinkQueue<GTreeNode<T>*> m_queue;

    GTree(const GTree<T>&) = default;
    GTree<T>& operator = (const GTree<T>&) = default;

    GTreeNode<T> *find(GTreeNode<T>* node, const T &value) const
    {
        GTreeNode<T> *ret = nullptr;

        if (node != nullptr)
        {
            if (node->value == value)
            {
                return node;
            }
            else
            {
                for (node->child.move(0); !node->child.end() && (ret == nullptr); node->child.next())
                {
                    ret = find(node->child.current(), value);
                }
            }
        }

        return ret;
    }

    GTreeNode<T> *find(GTreeNode<T>* node, GTreeNode<T> *obj) const
    {
        GTreeNode<T> *ret = nullptr;

        if (node == obj)
        {
            return node;
        }
        else
        {
            if (node != nullptr)
            {
                for (node->child.move(0); !node->child.end() && (ret == nullptr); node->child.next())
                {
                    ret = find(node->child.current(), obj);
                }
            }
        }

        return ret;
    }

    void free(GTreeNode<T> *node)
    {
        if (node != nullptr)
        {
            for (node->child.move(0); !node->child.end(); node->child.next())
            {
                free(node->child.current());
            }

            if (node->flag())
            {
                delete node;
            }
        }
    }

    void remove(GTreeNode<T> *node, GTree<T> *&ret)
    {
        ret = new GTree<T>();

        if (ret != nullptr)
        {
            if (node == root())
            {
                this->m_root = nullptr;
            }
            else
            {
                GTreeNode<T> *parent = dynamic_cast<GTreeNode<T>*>(node->parent);

                parent->child.remove(parent->child.find(node));

                node->parent = nullptr;
            }

            ret->m_root = node;
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to create tree ...");
        }
    }

    int count(GTreeNode<T> *node) const
    {
        int ret = 0;

        if (node != nullptr)
        {
            ret = 1;

            for (node->child.move(0); !node->child.end(); node->child.next())
            {
                ret += count(node->child.current());
            }
        }

        return ret;
    }

    int height(GTreeNode<T> *node) const
    {
        int ret = 0;

        if (node != nullptr)
        {
            for (node->child.move(0); !node->child.end(); node->child.next())
            {
                int h = height(node->child.current());

                if (ret < h)
                {
                    ret = h;
                }
            }

            ret += 1;
        }

        return ret;
    }

    int degree(GTreeNode<T> *node) const
    {
        int ret = 0;

        if (node != nullptr)
        {
            ret = node->child.length();

            for (node->child.move(0); !node->child.end(); node->child.next())
            {
                int d = degree(node->child.current());

                if (ret < d)
                {
                    ret = d;
                }
            }
        }

        return ret;
    }
};

}

#endif // GTREE_H

文件:BTree.h

#ifndef BTREE_H
#define BTREE_H

#include "Tree.h"
#include "BTreeNode.h"
#include "Exception.h"
#include "LinkQueue.h"

namespace DTLib
{

template <typename T>
class BTree : public Tree<T>
{
public:
    BTree() = default;

    bool insert(TreeNode<T> *node) override
    {
        return insert(node, ANY);
    }

    virtual bool insert(TreeNode<T> *node, BTNodePos pos)
    {
        bool ret = true;

        if (node != nullptr)
        {
            if (this->m_root == nullptr)
            {
                node->parent = nullptr;
                this->m_root = node;
            }
            else
            {
                BTreeNode<T> *np = find(node->parent);

                if (np != nullptr)
                {
                    ret = insert(dynamic_cast<BTreeNode<T>*>(node), np, pos);
                }
                else
                {
                    THROW_EXCEPTION(InvalidParameterExcetion, "Invalid parent tree node ...");
                }
            }
        }
        else
        {
            THROW_EXCEPTION(InvalidParameterExcetion, "Parameter can not be null ...");
        }

        return ret;
    }

    bool insert(const T &value, TreeNode<T> *parent) override
    {
        return insert(value, parent, ANY);
    }

    virtual bool insert(const T &value, TreeNode<T> *parent, BTNodePos pos)
    {
        bool ret = true;
        BTreeNode<T> *node = BTreeNode<T>::NewNode();

        if (node != nullptr)
        {
            node->value = value;
            node->parent = parent;

            ret = insert(node, pos);

            if (!ret)
            {
                delete node;
            }
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to create node ...");
        }

        return ret;
    }

    SharedPointer<Tree<T>> remove(const T &value) override
    {
        BTree<T> *ret = nullptr;

        BTreeNode<T> *node = find(value);

        if (node != nullptr)
        {
            remove(node, ret);

            m_queue.clear();
        }
        else
        {
            THROW_EXCEPTION(InvalidParameterExcetion, "Can not find the tree node via value ...");
        }

        return ret;
    }

    SharedPointer<Tree<T>> remove(TreeNode<T> *node) override
    {
        BTree<T> *ret = nullptr;

        node = find(node);

        if (node != nullptr)
        {
            remove(dynamic_cast<BTreeNode<T>*>(node), ret);

            m_queue.clear();
        }
        else
        {
            THROW_EXCEPTION(InvalidParameterExcetion, "Parameter node is invalid ...");
        }

        return ret;
    }

    BTreeNode<T>* find(const T &value) const override
    {
        return find(root(), value);
    }

    BTreeNode<T>* find(TreeNode<T> *node) const override
    {
        return find(root(), dynamic_cast<BTreeNode<T>*>(node));
    }

    BTreeNode<T>* root() const override
    {
        return dynamic_cast<BTreeNode<T>*>(this->m_root);
    }

    int degree() const override
    {
        return degree(root());
    }

    int count() const override
    {
        return count(root());
    }

    int height() const override
    {
        return height(root());
    }

    void clear() override
    {
        free(root());

        this->m_root = nullptr;
    }

    bool begin() override
    {
        bool ret = (root() != nullptr);

        if (ret)
        {
            m_queue.clear();
            m_queue.add(root());
        }

        return ret;
    }

    bool end() override
    {
        return (m_queue.length() == 0);
    }

    bool next() override
    {
        bool ret = (m_queue.length() > 0);

        if (ret)
        {
            BTreeNode<T> *node = m_queue.front();

            m_queue.remove();

            if (node->left != nullptr)
            {
                m_queue.add(node->left);
            }

            if (node->right != nullptr)
            {
                m_queue.add(node->right);
            }
        }

        return ret;
    }

    T current() override
    {
        if (!end())
        {
            return m_queue.front()->value;
        }
        else
        {
            THROW_EXCEPTION(InvalidOpertionExcetion, "No value at current position ...");
        }
    }

    ~BTree()
    {
        clear();
    }

protected:
    LinkQueue<BTreeNode<T>*> m_queue;

    BTree(const BTree<T>&) = default;
    BTree<T>& operator = (const BTree<T>&) = default;

    virtual BTreeNode<T>* find(BTreeNode<T> *node, const T &value) const
    {
        BTreeNode<T> *ret = nullptr;

        if (node != nullptr)
        {
            if (node->value == value)
            {
                ret = node;
            }
            else
            {
                if (ret == nullptr)
                {
                    ret = find(node->left, value);
                }

                if (ret == nullptr)
                {
                    ret = find(node->right, value);
                }
            }
        }

        return ret;
    }

    virtual BTreeNode<T>* find(BTreeNode<T> *node, BTreeNode<T> *obj) const
    {
        BTreeNode<T> *ret = nullptr;

        if (node == obj)
        {
            ret = node;
        }
        else
        {
            if (node != nullptr)
            {
                if (ret == nullptr)
                {
                    ret = find(node->left, obj);
                }

                if (ret == nullptr)
                {
                    ret = find(node->right, obj);
                }
            }
        }

        return ret;
    }

    virtual bool insert(BTreeNode<T> *node, BTreeNode<T> *np, BTNodePos pos)
    {
        bool ret = true;

        if (pos == ANY)
        {
            if (np->left == nullptr)
            {
                np->left = node;
            }
            else if (np->right == nullptr)
            {
                np->right = node;
            }
            else
            {
                ret = false;
            }
        }
        else if (pos == LEFT)
        {
            if (np->left == nullptr)
            {
                np->left = node;
            }
            else
            {
                ret = false;
            }
        }
        else if (pos == RIGHT)
        {
            if (np->right == nullptr)
            {
                np->right = node;
            }
            else
            {
                ret = false;
            }
        }

        return ret;
    }

    virtual void remove(BTreeNode<T> *node, BTree<T> *&ret)
    {
        ret = new BTree<T>();

        if (ret != nullptr)
        {
            if (root() == node)
            {
                this->m_root = nullptr;
            }
            else
            {
                BTreeNode<T> *parent = dynamic_cast<BTreeNode<T>*>(node->parent);

                if (node == parent->left)
                {
                    parent->left = nullptr;
                }
                else if (node == parent->right)
                {
                    parent->right = nullptr;
                }

                node->parent = nullptr;
            }

            ret->m_root = node;
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create btree ...");
        }
    }

    virtual void free(BTreeNode<T> *node)
    {
        if (node != nullptr)
        {
            free(node->left);
            free(node->right);

            if (node->flag())
            {
                delete node;
            }
        }
    }

    int count(BTreeNode<T> *node) const
    {
        return (node != nullptr) ? (count(node->left) + count(node->right) + 1) : 0;
    }

    int height(BTreeNode<T> *node) const
    {
        int ret = 0;

        if (node != nullptr)
        {
            int lh = height(node->left);
            int rh = height(node->right);

            ret = ((lh > rh) ? lh : rh) + 1;
        }

        return ret;
    }

    int degree(BTreeNode<T> *node) const
    {
        int ret = 0;

        if (node != nullptr)
        {
            BTreeNode<T> *child[] = {node->left, node->right};
            ret = !!node->left + !!node->left;

            for (int i=0; (i<2) && (ret<2); ++i)
            {
                int d = degree(child[i]);

                if (ret < d)
                {
                    ret = d;
                }
            }
        }

        return ret;
    }
};

}

#endif // BTREE_H

文件:main.cpp

#include <iostream>
#include "BTreeNode.h"
#include "BTree.h"

using namespace std;
using namespace DTLib;

int main()
{
    BTree<int> bt;
    BTreeNode<int> *n = nullptr;

    bt.insert(1, nullptr);

    n = bt.find(1);
    bt.insert(2, n);
    bt.insert(3, n);

    n = bt.find(2);
    bt.insert(4, n);
    bt.insert(5, n);

    n = bt.find(4);
    bt.insert(8, n);
    bt.insert(9, n);

    n = bt.find(5);
    bt.insert(10, n);

    n = bt.find(3);
    bt.insert(6, n);
    bt.insert(7, n);

    for (bt.begin(); !bt.end(); bt.next())
    {
        cout << bt.current() << " ";
    }

    cout << endl;

    return 0;
}

输出:

1 2 3 4 5 6 7 8 9 10

To be continued

思考:BTree (二叉树结构) 是否只有一种遍历的方法?

以上内容整理于狄泰软件学院系列课程,请大家保护原创!


TianSong
734 声望138 粉丝

阿里山神木的种子在3000年前已经埋下,今天不过是看到当年注定的结果,为了未来的自己,今天就埋下一颗好种子吧