【數據結構】56_樹中節點的刪除操做

刪除的方式

  • 基於數據元素的刪除node

    • SharedPointer<Tree<T>> remove(const T &value)
  • 基於節點的刪除ios

    • SharedPointer<Tree<T>> remove(TreeNode<T> *node)

刪除操做成員函數的設計要點

  • 將被刪節點所表明的子樹進行刪除
  • 刪除函數返回一顆堆空間中的樹
  • 具體返回值爲指向樹的智能指針對象

樹中節點的刪除

image.png

實用的設計原則

當須要從函數中返回堆中的對象時,使用智能指針(SharedPointer) 做爲函數的返回值

刪除操做功能的定義

  • void remove(GTreeNode<T> node, GTree<T> &ret)編程

    • 將 node 做爲根節點的子樹從原來的樹中刪除
    • ret 做爲子樹返回 (ret 指向堆空間中的樹對象)

刪除函數的實現

image.png

編程實驗:樹節點的刪除操做

文件:GTree.hide

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

    int count() const override
    {
        return 0;
    }

    int height() const override
    {
        return 0;
    }

    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 ...");
        }
    }
};

}

#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);

    SharedPointer<Tree<char>> p =  t.remove(t.find('D'));

    const char *s = "KLFGMIJ";

    for (int i=0; i<7; ++i)
    {
        TreeNode<char> *node = t.find(s[i]);

        while (node != nullptr)
        {
            cout << node->value << " ";

            node = node->parent;
        }

        cout << endl;
    }

    cout << "----------------" << endl;

    for (int i=0; i<7; ++i)
    {
        TreeNode<char> *node = p->find(s[i]);

        while (node != nullptr)
        {
            cout << node->value << " ";

            node = node->parent;
        }

        cout << endl;
    }

    return 0;
}

輸出:this

K E B A
L E B A
F B A
G C A



----------------




M H D
I D
J D

小結

  • 刪錯操做將目標節點所代標的子樹移除
  • 刪除操做必須完善處理父節點和子結點的關係
  • 刪除操做的返回值爲指向樹的智能指針對象
  • 函數中返回堆中的對象時,使用智能指針做爲返回值

To be continued

思考:如何實現 GTree (通用樹結構) 的屬性操做函數
int degree() const;
int count() const;
int height() const;
相關文章
相關標籤/搜索