树中结点的数目

  • 定义功能:count(node)

    • 在 node 为根结点的树中统计结点数目

image.png

树结点数目的计算示例

count(A) = count(B) + count(C) + count(D) + 1

image.png

编程实验:树中结点的数目

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 count() const override
{
    return count(root());
}

树的高度

  • 定义功能:height(node)

    • 获取 node 为根结点的树的高度

image.png

树的高度示例

height(A) = MAX{ height(B), height(C), height(D) } + 1

image.png

编程实验:树的高度

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 height() const override
{
    return height(root());
}

树的度数

  • 定义功能:degree(node)

    • 获取 node 为根结点的树的度数

image.png

树的度计算示例

degree(A) = MAX{degree(B), degree(C), degree(D), 3}

image.png

编程实验:树的度数

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

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

编程实验:数中属性的操作

文件:GTree.h

#ifndef GTREE_H
#define GTREE_H

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

namespace DTLib
{

template <typename T>
class GTree : public Tree<T>
{
public:
    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);
        }
        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);
        }
        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;
    }

    ~GTree()
    {
        clear();
    }

protected:
    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

文件:main.cpp

#include <iostream>
#include "GTree.h"

using namespace std;
using namespace DTLib;

int main()
{
    GTree<char> t;
    GTreeNode<char> *node = nullptr;

    GTreeNode<char> root;

    root.value = 'A';
    root.parent = nullptr;

    t.insert(&root);

    node = t.find('A');
    t.insert('B', node);
    t.insert('C', node);
    t.insert('D', node);

    node = t.find('B');
    t.insert('E', node);
    t.insert('F', node);

    node = t.find('E');
    t.insert('K', node);
    t.insert('L', node);

    node = t.find('C');
    t.insert('G', node);

    node = t.find('D');
    t.insert('H', node);
    t.insert('I', node);
    t.insert('J', node);

    node = t.find('H');
    t.insert('M', node);

    cout << t.count() << endl;
    cout << t.height() << endl;
    cout << t.degree() << endl;

    return 0;
}

输出:

13
4
3

To be continued

思考: 如何遍历 GTree (通用树结构) 的每一个结点?

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


TianSong
737 声望139 粉丝

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