将 Children 个节点添加到 Python 中的树
Adding Children Nodes to a Tree in Python
我正在尝试在 python 中创建一棵树,我希望稍后能够对其进行广度优先遍历。但是,当我尝试遍历节点(即根)的 children 时,它似乎没有找到任何 children。但是,如果我尝试单独访问 child (root.children[1]),它是有效的。我很确定我在做一些愚蠢的事情,但我不确定在哪里。这是我的代码:
class Component:
def __init__(self, compName, originName, connected = False): #removed to decrease code size
def __str__(self):
return "\n\tComponent Name: {0}\n\tOrigin Name: {1}\n\tConnected: {2}\n".format(self.name, self.origin, str(self.connected));
class Node:
def __init__(self, component, parent):
self.children = [];
def add_node(self, node):
self.children.append(node);
#...left out some broken code that is commented out...#
def __str__(self):
return "{0}".format(str(self.component));
class Tree:
def __init__(self):
def add_component(self, component, parent = None):
if self.root is None:
self.root = Node(component, parent);
else:
self.root.add_node(Node(component, parent));
def breadth_first(node):
result = [];
queue = [];
queue.append(node);
while queue:
node = queue.pop(0);
result.append(node);
plog("Adding to result\n");
plog(str(node));
if node in node.children:
for child in node.children:
if child not in result and child not in queue:
queue.append(child);
else:
plog("NO CHILDREN\n"); // this is being displayed
return result;
def main(ScriptArgument, oDesktop):
component = Component("Laminate", "B7_TX");
tree = Tree();
tree.add_component(component);
component = Component("B7", "B7_TX");
tree.add_component(component, "Laminate");
result = breadth_first(tree.root);
for x in result:
plog(str(x) + "\n");
这是我得到的输出:
Adding to result # output from breadth-first
Component Name: Laminate #output from breadth-first
Origin Name: B7_TX
Connected: False
NO CHILDREN # this is indicating that the child was not added properly, I believe
Component Name: Laminate # output from the loop in main
Origin Name: B7_TX
Connected: False
我的"if"检查错了。我正在检查根(或当前)节点是否在它自己的子列表中,这是错误的。我只是将其更改为检查是否有任何子项(如果 node.children:) 并且有效。
为什么要检查父项是否在具有以下行的子项列表中?
if node in node.children:
我会简单地做:
[...]
while queue:
node = queue.pop(0);
result.append(node);
plog("Adding to result\n");
plog(str(node));
for child in node.children:
if child not in result and child not in queue:
queue.append(child);
return result;
我正在尝试在 python 中创建一棵树,我希望稍后能够对其进行广度优先遍历。但是,当我尝试遍历节点(即根)的 children 时,它似乎没有找到任何 children。但是,如果我尝试单独访问 child (root.children[1]),它是有效的。我很确定我在做一些愚蠢的事情,但我不确定在哪里。这是我的代码:
class Component:
def __init__(self, compName, originName, connected = False): #removed to decrease code size
def __str__(self):
return "\n\tComponent Name: {0}\n\tOrigin Name: {1}\n\tConnected: {2}\n".format(self.name, self.origin, str(self.connected));
class Node:
def __init__(self, component, parent):
self.children = [];
def add_node(self, node):
self.children.append(node);
#...left out some broken code that is commented out...#
def __str__(self):
return "{0}".format(str(self.component));
class Tree:
def __init__(self):
def add_component(self, component, parent = None):
if self.root is None:
self.root = Node(component, parent);
else:
self.root.add_node(Node(component, parent));
def breadth_first(node):
result = [];
queue = [];
queue.append(node);
while queue:
node = queue.pop(0);
result.append(node);
plog("Adding to result\n");
plog(str(node));
if node in node.children:
for child in node.children:
if child not in result and child not in queue:
queue.append(child);
else:
plog("NO CHILDREN\n"); // this is being displayed
return result;
def main(ScriptArgument, oDesktop):
component = Component("Laminate", "B7_TX");
tree = Tree();
tree.add_component(component);
component = Component("B7", "B7_TX");
tree.add_component(component, "Laminate");
result = breadth_first(tree.root);
for x in result:
plog(str(x) + "\n");
这是我得到的输出:
Adding to result # output from breadth-first
Component Name: Laminate #output from breadth-first
Origin Name: B7_TX
Connected: False
NO CHILDREN # this is indicating that the child was not added properly, I believe
Component Name: Laminate # output from the loop in main
Origin Name: B7_TX
Connected: False
我的"if"检查错了。我正在检查根(或当前)节点是否在它自己的子列表中,这是错误的。我只是将其更改为检查是否有任何子项(如果 node.children:) 并且有效。
为什么要检查父项是否在具有以下行的子项列表中?
if node in node.children:
我会简单地做:
[...]
while queue:
node = queue.pop(0);
result.append(node);
plog("Adding to result\n");
plog(str(node));
for child in node.children:
if child not in result and child not in queue:
queue.append(child);
return result;