- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一棵树,其中一个节点可能以两种或多种方式分支:
A
/ \
B C
/ \
D E
/ \
F G
A
/ \
B C
/ \
X Y
'''
print all the possible trees
'''
class Tree():
'''each node is a dict of Nodes, because there can be multiple parses'''
def __init__(self, root=None):
self.root = self.build(root)
self.printTrees(root)
def build(self, p):
# p is a dict as in A = {0: ('a', B, C, False), 'terminal':False}
if p['terminal']: # if terminal
node = {}
for key in p.keys():
if key != 'terminal':
node[key] = Node(p[key][0], None, None, True)
return node
node = {}
for key in p.keys():
if key != 'terminal':
node[key] = Node(p[key][0], # type
self.build(p[key][1]), # left
self.build(p[key][2])) # right
return node
def printTrees(self, p):
'''TODO complete print one tree and then go to another tree!'''
if p['terminal']: # if terminal
for key in p.keys():
if key != 'terminal':
print(p[key][0], end=' ')
return
for key in p.keys():
if key != 'terminal':
print(p[key][0])
self.printTrees(p[key][1]) # left
self.printTrees(p[key][2]) # right
class Node():
def __init__(self, typ=None, left=None, right=None, terminal=False):
self.type = typ
self.left = left
self.right = right
self.terminal = terminal
def __str__(self):
return "Node: "+self.type
def main():
X = {0: ('x', None, None), 'terminal':True}
Y = {0: ('y', None, None), 'terminal':True}
G = {0: ('g', None, None), 'terminal':True}
F = {0: ('f', None, None), 'terminal':True}
D = {0: ('d', None, None), 'terminal':True}
C = {0: ('c', None, None), 'terminal':True}
E = {0: ('e', F, G, False), 'terminal':False}
B = {0: ('b', D, E, False), 1: ('b', X, Y, False), 'terminal':False}
A = {0: ('a', B, C, False), 'terminal':False}
t = Tree(A)
if __name__ == '__main__':
main()
最佳答案
要生成所有树组合,您可以遍历每个节点的可能子节点(存储在每个节点字典的整数键下),递归调用每个子节点的树构建方法,yield
返回结果:
首先,树设置:
def get_tree():
X = {0: ('x', None, None), 'terminal':True}
Y = {0: ('y', None, None), 'terminal':True}
G = {0: ('g', None, None), 'terminal':True}
F = {0: ('f', None, None), 'terminal':True}
D = {0: ('d', None, None), 'terminal':True}
C = {0: ('c', None, None), 'terminal':True}
E = {0: ('e', F, G, False), 'terminal':False}
B = {0: ('b', D, E, False), 1: ('b', X, Y, False), 'terminal':False}
A = {0: ('a', B, C, False), 'terminal':False}
return A
然后,生成所有树组合的函数:
from itertools import product
def get_trees(t):
for a, b in t.items():
if isinstance(a, int):
v = [[b[0]], *[[i] if i is None else list(get_trees(i)) for i in b[1:3]]]
yield from product(*v)
trees = list(get_trees(get_tree()))
for tree in trees:
print(tree)
输出:
('a', ('b', ('d', None, None), ('e', ('f', None, None), ('g', None, None))), ('c', None, None))
('a', ('b', ('x', None, None), ('y', None, None)), ('c', None, None))
关于python - 如何打印所有可能的树python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46268702/
降本手段一招鲜,增效方法吃遍天; 01 互联网行业里; 降本策略千奇百怪,手段却出奇一致;增效方法五花八门,手段更是花里胡哨; 对于企业来说;
有什么方法可以使用 angularjs 中的部分进行代码分组吗? 原因 --- 我的 Controller 包含太多代码。该 Controller 包含了多个方法和大量功能的代码,降低了代码的可读性。
不幸的是,我的数据库的数据模型必须改变,所以我正在寻找最轻松的方式来迁移我的数据。 此时情况如何: create table cargo{ id serial primary key, per
在 QTextEdit 对象中,假设我想知道字符在鼠标光标下的位置。 我会写... void MyQTextEditObject::mousePressEvent(QMouseEvent* mouse
是否可以在 C++ 中返回一个 return 语句或做一些具有类似功能的事情? 例如,如果代码中有几个函数将指针作为输入,并且每个函数都检查指针是否为 nullptr,这将很方便。如果它是一个 nul
我的 PC 上有一个控制台应用程序,它是 signalR 服务器。 我有一个 html 页面,它是互联网上的 signalR 客户端。但我尝试连接服务器,但我有一个错误的请求 400 错误。如果服务器
我想将应用程序作为后台进程运行。当点击应用程序图标时,它不会显示任何 View ,只会启动后台进程。 最佳答案 对于 iOS 这是不可能的,但是对于 android,react native 有 he
我知道有(昂贵的)框架可以让你在 VS C# 中编写 android 应用程序并将其编译为 android apk。 我也知道,可以在 VS 中编写 Java 应用程序(link)。 是否有可能,甚至
我在做: can :manage, :all if user.role == 'admin' can :approve, Anuncio do |anuncio| anuncio.try(:apr
我是一名优秀的程序员,十分优秀!