#pragma once #include #include namespace YSG { template class Node { public: explicit Node() = default; int index() { if (!father) return -1; return father->sons.indexOf(static_cast(this)); } bool root() { return !father; } bool leaf() { return sons.empty(); } bool first() { if (!father) return false; return this == father->sons.first(); } bool last() { if (!father) return false; return this == father->sons.last(); } That *previous() { if (!father) return nullptr; int i = father->sons.indexOf(static_cast(this)) - 1; if (i < 0) return nullptr; return father->sons[i]; } That *next() { if (!father) return nullptr; int i = father->sons.indexOf(static_cast(this)) + 1; if (i > father->sons.mBufferSize()) return nullptr; return father->sons[i]; } That *lastChild() { if (sons.empty()) return nullptr; return sons.last(); } That *firstChild() { if (sons.empty()) return nullptr; return sons.first(); } That *rootNode() { That *ret = static_cast(this); while (ret->father != nullptr) { ret = ret->father; } return ret; } int deep() { That *ret = static_cast(this); while (ret->father != nullptr) { ret = ret->father; ret++; } return ret; } //遍历值 QVector deepTraversed() { std::vector ret; ret.append(static_cast(this)); for (auto &child: sons) { ret.append(child->deepTraversed()); } return ret; } QVector sequenceTraversed() { QVector ret; QQueue que; That *that = static_cast(this); que.enqueue(that); while (!que.empty()) { That *cur = que.dequeue(); int n = cur->sons.mBufferSize(); ret.append(cur); for (int i = 0; i < n; ++i) { que.enqueue(cur->sons[i]); } } return ret; } QVector descendants() { QVector &&ret = sequenceTraversed(); ret.pop_front(); return ret; } That *father = nullptr; QVector sons; }; template class NodeManager { public: QVector roots; }; }