我有一个这样的节点树:
class Node:
next # the next node or None
prev # the previous node or None
parent # the parent or None
children[] # ordered list of child nodes
columns[] # a list of data. Currently only holdt the
# string representation of the node in the model.
由于我事先无法知道模型有多大,我得出的结论是递归不是一种选择。我想在内存中保留尽可能少的节点。这就是我的方法应该打印的内容:
- 0
-- 0:0
--- 0:0:0
--- 0:0:1
---- 0:0:1:0
---- 0:0:1:1
--- 0:0:2
-- 0:1
- 1
但这就是它所做的打印:
- 0
-- 0:0
-- 0:1
-- 0
- 1
--- 0:0:0
--- 0:0:1
--- 0:0:2
-- 0:1
-- 0
- 1
--- 0:0:1
---- 0:0:1:0
---- 0:0:1:1
--- 0:0:2
-- 0
---- 0:0:1:0
---- 0:0:1:1
--- 0:0:2
---- 0:0:1:1
---- 0:0:1:1
这是我写的代码:
def print_tree(from_path):
nodelist = []
root_node = model.get_iter(from_path)
nodelist.append((root_node, 0)) # the second item in the tuple is the indentation
while nodelist:
node = nodelist[0][0]
indent = nodelist[0][1]
del(nodelist[0])
print("{0} {1}".format("-" * indent, node.columns[0]))
if node.children:
child = node.children[0]
nodelist.append((child, indent +1))
while child.next:
nodelist.append((child.next, indent +1))
child = child.next
if node.next:
next = node.next
nodelist.append((next, indent))
非常感谢任何帮助。
答案 0 :(得分:2)
由于每个节点都有对其父节点的引用,我认为您可以遍历整个树,一次只在内存中保留一个节点。我在理解你的代码时遇到了一些麻烦(特别是每个节点如何加载到内存中),所以我将在伪代码中发布我的建议:
def visit(node,indent):
# Load your node data
print("{0} {1}".format("-" * indent, node.columns[0])) # Do something with your data
# Unload your node data
if len(node.children) > 0 :
return (node.children[0], indent+1) # Visit the first child, if there is one
while node.next is None: # If no sibling, your parent is done
node = node.parent
indent -= 1
if node is None: # Root node reached, end the traversal
return None
return (node.next, indent) # Visit your next sibling, if there is one
cursor = (root_node, 0)
while cursor is not None:
cursor = visit(*cursor)
如果节点本身必须动态加载(即next
,prev
,parent
和children
只包含另一个节点数据的路径,而不是对Node
对象),告诉我,我会更新答案(只需更改一些加载/卸载的位置)。当然,如果卸载只是将对象留给垃圾收集器,那就更容易了......
答案 1 :(得分:2)
正如mgibsonbr指出的那样,既然你正在存储一个父指针,那么可以迭代地执行此操作,同时仅跟踪当前节点(及其缩进):
def print_tree(from_path):
node, indent = model.get_iter(from_path), 0
while node:
if indent: # don't print the root
print "-" * indent, node.columns[0]
if node.children: # walk to first child before walking to next sibling
node = node.children[0]
indent += 1
elif node.next: # no children, walk to next sibling if there is one
node = node.next
else:
# no children, no more siblings: find closet ancestor w/ more siblings
# (note that this might not be the current node's immediate parent)
while node and not node.next:
node = node.parent
indent -= 1
node = node and node.next
您可以将print
行替换为yield indent, node
,将其转换为生成器。
我不得不模拟一些测试数据来调试它。这就是我所拥有的,以防其他人想玩。我认为根不能有兄弟姐妹(没有理由它没有next
并且在columns
中存储数据,但是你希望你的缩进从1开始)。
class Node(object):
def __init__(self, parent=None, sib=None, value=""):
self.parent = parent
self.prev = sib
self.next = None
self.children = []
self.columns = [str(value)]
def child(self, value):
child = Node(self, None, value)
if self.children:
self.children[-1].next = child
child.prev = self.children[-1]
self.children.append(child)
return child
def sib(self, value):
return self.parent.child(value)
class model(object):
@staticmethod
def get_iter(_):
root = Node()
root.child("0").child("0:0").child("0:0:0").sib("0:0:1") \
.child("0:0:1:0").sib("0:0:1:0").parent.sib("0:0:2")
root.children[0].child("0:1").parent.sib("1")
return root