[英]What data structure does this function represent?
I have the following function and I'm not really sure if they are implementing a Binary Tree or a B-Tree.我有以下 function 我不确定他们是在实现二叉树还是 B 树。
Here is the code:这是代码:
def foo(x):
if x:
a, b, c = x
return foo(a) + b + foo(c)
else:
return 0
Could anyone help me figuring out which data structures are being used?谁能帮我弄清楚正在使用哪些数据结构?
That is indeed a binary tree but, to some (usually those more comfortable with pointers), a rather strange implementation.这确实是一棵二叉树,但对于某些人(通常是那些更喜欢指针的人)来说,这是一个相当奇怪的实现。 Each node of the tree is a 3-tuple holding:
树的每个节点都是一个 3 元组:
Your foo
function is actually summing up all the nodes, althoug I'd make a few minor changes:您的
foo
function 实际上是对所有节点的总结,尽管我会做一些小改动:
def sum_tree(tpl):
if tpl:
return foo(tpl[0]) + tpl[1] + foo(tpl[2])
return 0
# Construct tree for testing:
# __42__ (42)
# / \
# 7 5 (12)
# / \ /
# 12 17 3 (32)
# ----
# (86)
tree = [[[None, 12, [None, 7, None]], 17, None], 42, [[None, 3, None], 5, None]]
print(sum_tree(tree)) # Output is `86`.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.