本文整理汇总了Python中treelib.Tree.root方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.root方法的具体用法?Python Tree.root怎么用?Python Tree.root使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类treelib.Tree
的用法示例。
在下文中一共展示了Tree.root方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: remove_subtree
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import root [as 别名]
def remove_subtree(self, nid):
"""
Return a subtree deleted from this tree. If nid is None, an
empty tree is returned.
For the original tree, this method is similar to
`remove_node(self,nid)`, because given node and its children
are removed from the original tree in both methods.
For the returned value and performance, these two methods are
different:
`remove_node` returns the number of deleted nodes;
`remove_subtree` returns a subtree of deleted nodes;
You are always suggested to use `remove_node` if your only to
delete nodes from a tree, as the other one need memory
allocation to store the new tree.
"""
st = Tree()
if nid is None:
return st
if not self.contains(nid):
raise NodeIDAbsentError("Node '%s' is not in the tree" % nid)
st.root = nid
parent = self[nid].bpointer
self[nid].bpointer = None # reset root parent for the new tree
removed = []
for id in self.expand_tree(nid):
removed.append(id)
for id in removed:
st._nodes.update({id: self._nodes.pop(id)})
# Update its parent info
self.__update_fpointer(parent, nid, Node.DELETE)
return st
示例2: subtree
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import root [as 别名]
def subtree(self, nid):
"""
Return a shallow COPY of subtree with nid being the new root.
If nid is None, return an empty tree.
If you are looking for a deepcopy, please create a new tree
with this shallow copy,
e.g.
new_tree = Tree(t.subtree(t.root), deep=True)
This line creates a deep copy of the entire tree.
"""
st = Tree()
if nid is None:
return st
if not self.contains(nid):
raise NodeIDAbsentError("Node '%s' is not in the tree" % nid)
st.root = nid
for node_n in self.expand_tree(nid):
st._nodes.update({self[node_n].identifier: self[node_n]})
return st