[英]Effectively remove list of nodes from n-ary tree
I have n-ary tree of game objects. 我有游戏对象的n-ary树。 The user randomly selects some objects in a tree and wants to delete.
用户随机选择树中的某些对象并要删除。 Problem is that some objects are children of the another.
问题在于某些对象是另一对象的子对象。 It turns out after each deletion of the node inside the hierarchy I must iterate through all selected nodes and remove it.
事实证明,每次删除层次结构内的节点后,我必须遍历所有选定的节点并将其删除。 Is the algorithm faster than O (n^2)?
算法比O(n ^ 2)快吗?
Upd: to make it more clear what I need, I wrote pseudocode: Upd:为了更清楚我的需要,我编写了伪代码:
struct TreeNode
{
vector<TreeNode*> childs;
};
void removeNodeHierarchy(list<TreeNode*>& nodes, TreeNode *n)
{
for(TreeNode *child : n->childs())
removeNodeHierarchy(nodes, child);
nodes.remove(n); // complexity nodes.size()
delete n;
}
// The function I'm trying to write
// Problem: Total complexity = nodes.size() * nodes.size()
void removeNodes(list<TreeNode*>& nodes, TreeNode *root)
{
while (!nodes.empty()) // complexity nodes.size()
{
TreeNode *n = nodes.first();
removeNodeHierarchy(nodes, n);
}
}
void main()
{
TreeNode *tree = ...
list<TreeNode*> nodes = ...
removeNodes(nodes, tree);
}
In order to search that node, it will take you O(n^d), where d is the depth of the tree. 为了搜索该节点,它将花费你O(n ^ d),其中d是树的深度。 So it will be faster if d < 2 which I think will almost never be the case for a big game tree.
所以如果d <2那么它会更快,我认为对于一个大型游戏树几乎不会这样。 Are you sure that n from n-ary and O(n^2) are the same symbol?
您确定n元和O(n ^ 2)中的n是同一符号吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.