210 lines
3.6 KiB
C++
210 lines
3.6 KiB
C++
/**
|
|
* @file dl_tree.ixx
|
|
* @brief N叉树
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author lveyou
|
|
* @date 22-10-05
|
|
*
|
|
* @note 空树不存在根节点
|
|
* @note 如果T为指针,并不释放内存(和标准库一致)
|
|
*/
|
|
#pragma once
|
|
|
|
#include <vector>
|
|
#include <list>
|
|
|
|
#include "io/dl_log.h"
|
|
|
|
namespace dl
|
|
{
|
|
|
|
template<typename T>
|
|
class NTree
|
|
{
|
|
public:
|
|
class Node
|
|
{
|
|
friend class NTree<T>;
|
|
public:
|
|
Node(const T& v, Node* parent) :
|
|
_parent{ parent },
|
|
_v{v}
|
|
{}
|
|
|
|
T& GetValue() { return _v; }
|
|
Node* GetParent() { return _parent; }
|
|
std::vector<Node*>& GetChild() { return _child; }
|
|
private:
|
|
T _v;
|
|
Node* _parent;
|
|
std::vector<Node*> _child;
|
|
};
|
|
|
|
NTree()
|
|
{}
|
|
|
|
/**
|
|
* @brief 添加,parent为nullptr时,为添加根节点
|
|
* @retval 返回节点,失败返回空指针
|
|
*/
|
|
Node* Add(const T& v, const T& parent)
|
|
{
|
|
if (!parent)
|
|
{
|
|
if (!_allNode.empty())
|
|
{
|
|
log_err0("根节点已经存在,不能重复添加!");
|
|
return nullptr;
|
|
}
|
|
Node* node = new Node(v, nullptr);
|
|
_allNode.push_back(node);
|
|
return node;
|
|
}
|
|
|
|
Node* node_parent = Get(parent);
|
|
if (!node_parent)
|
|
{
|
|
log_err0("父节点不存在!");
|
|
return nullptr;
|
|
}
|
|
Node* node = new Node(v, node_parent);
|
|
_allNode.push_back(node);
|
|
node_parent->_child.push_back(node);
|
|
return node;
|
|
}
|
|
/**
|
|
* @brief 删除节点
|
|
*/
|
|
bool Del(Node* node)
|
|
{
|
|
auto iter = std::find(_allNode.begin(), _allNode.end(), node);
|
|
if (iter == _allNode.end())
|
|
{
|
|
log_err0("节点不存在!");
|
|
return false;
|
|
}
|
|
// 从父节点移除
|
|
auto iter1 = std::find(node->_parent->_child.begin(), node->_parent->_child.end(), node);
|
|
if (iter1 == node->_parent->_child.end())
|
|
{
|
|
log_err0("父节点引用错误!");
|
|
return false;
|
|
}
|
|
node->_parent->_child.erase(iter1);
|
|
|
|
// 子节点移除引用
|
|
for (Node* node_child : node->_child)
|
|
node_child->_parent = nullptr;
|
|
|
|
// 从主列表移除
|
|
delete node;
|
|
_allNode.erase(iter);
|
|
return true;
|
|
}
|
|
/**
|
|
* @brief 查找,失败返回nullptr
|
|
*/
|
|
Node* Get(T v)
|
|
{
|
|
for (Node* node : _allNode)
|
|
{
|
|
if (v == node->_v)
|
|
return node;
|
|
}
|
|
return nullptr;
|
|
}
|
|
/**
|
|
* @brief 包含节点
|
|
*/
|
|
bool Has(Node* node)
|
|
{
|
|
for (Node* iter : _allNode)
|
|
{
|
|
if (iter == node)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
/**
|
|
* @brief 修改父节点
|
|
*/
|
|
void SetParent(Node* node, Node* parent)
|
|
{
|
|
if (!parent)
|
|
{
|
|
log_err0("parent不能为空!");
|
|
return;
|
|
}
|
|
if (!node->_parent)
|
|
{
|
|
log_err0("自己不能为空节点!");
|
|
return;
|
|
}
|
|
|
|
if (!Has(node))
|
|
{
|
|
log_err0("节点不存在!");
|
|
return;
|
|
}
|
|
if (!Has(parent))
|
|
{
|
|
log_err0("parent节点不存在!");
|
|
return;
|
|
}
|
|
auto& vec_child = node->GetParent()->GetChild();
|
|
auto iter_child = std::find(vec_child.begin(), vec_child.end(), node);
|
|
if (iter_child == vec_child.end())
|
|
{
|
|
log_err0("旧parent不包含自身!");
|
|
return;
|
|
}
|
|
vec_child.erase(iter_child);
|
|
parent->_child.push_back(node);
|
|
node->_parent = parent;
|
|
}
|
|
/**
|
|
* @brief 遍历,保证父节点先遍历
|
|
*/
|
|
template<typename F>
|
|
void ForEach(F func)
|
|
{
|
|
if (_allNode.empty())
|
|
return;
|
|
|
|
std::stack<Node*> s;
|
|
s.push(_allNode.front());
|
|
|
|
while (!s.empty())
|
|
{
|
|
Node* node_this = s.top();
|
|
s.pop();
|
|
func(node_this->_v);
|
|
|
|
// 反向遍历
|
|
auto& c = node_this->_child;
|
|
for (auto iter = c.rbegin(); iter != c.rend(); ++iter)
|
|
{
|
|
Node* node = *iter;
|
|
s.push(node);
|
|
}
|
|
}
|
|
}
|
|
|
|
//! 标准库迭代方法不保证父节点先遍历
|
|
auto begin() { return _allNode.begin(); }
|
|
auto end() { return _allNode.end(); }
|
|
|
|
~NTree()
|
|
{
|
|
for (Node* node : _allNode)
|
|
{
|
|
delete node;
|
|
}
|
|
}
|
|
private:
|
|
std::list<Node*> _allNode;//所有节点,第一个为根节点
|
|
};
|
|
|
|
} |