如何创建将二叉树转换为元组的函数?
Posted
技术标签:
【中文标题】如何创建将二叉树转换为元组的函数?【英文标题】:How do i create a function to convert a binary tree to a tuple? 【发布时间】:2021-12-01 00:56:43 【问题描述】:我遇到了这个问题,我的任务是将元组转换为二叉树,然后将二叉树转换回元组并返回树和元组。我能够将元组转换为树,但我未能创建一个函数来执行相反的操作。我只是一个尝试学习数据结构的初学者。
这里的 parse_tuple 函数用于解析一个元组以创建一个可以正常工作的二叉树。
请帮我修复我的 tree_to_tuple 函数。任何解决逻辑的见解或提示都会很棒。
谢谢
#used for creating binary tree
class TreeNode:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
#used to parse over the tuple to create a bianry tree
def parse_tuple(data):
if isinstance(data, tuple) and len(data) == 3:
node = TreeNode(data[1])
node.left = parse_tuple(data[0])
node.right = parse_tuple(data[2])
elif data is None:
node = None
else:
node = TreeNode(data)
return node
#doesnt work
def tree_to_tuple(node):
if isinstance(node, TreeNode) and node.left is not None and node.right is not None:
node_mid = node.key
node_left = tree_to_tuple(node.left)
node_right = tree_to_tuple(node.right)
elif node.left is None:
node_left = None
else:
node_right = None
return (node_left, node_mid, node_right)
tree_tuple = ((1, 3, None), 2, ((None, 3, 4), 5, (6, 7, 8)))
tree2 = parse_tuple(tree_tuple)
tree_tuple2 = (1, 2, 3)
tree = parse_tuple(tree_tuple2)
print(tree_to_tuple(tree2))
如果我尝试使用 tree_to_tuple,这就是我得到的错误
文件“main.py”,第 45 行,在 tree_to_tuple 中 返回(node_left,node_mid,node_right) UnboundLocalError:在赋值之前引用了局部变量“node_left”。
【问题讨论】:
嗯,你总是返回(node_left, node_mid, node_right)
,但如果你经过elif
或else
分支,那么它们就不会被定义。在elif
分支中你只定义node_left
而忘记了node_mid 和node_right,在else
分支中你只定义了node_right 而忘记了node_mid 和node_left。
【参考方案1】:
你很接近,但你的测试有点混乱。
这是一个补丁:
def tree_to_tuple(node):
if isinstance(node, TreeNode):
# special case if the tree has no left and no right sub-tree
if node.left is None and node.right is None:
return node.key
return (
tree_to_tuple(node.left),
node.key,
tree_to_tuple(node.right)
)
raise ValueError('this is not a tree')
tree_tuple = ((1, 3, None), 2, ((None, 3, 4), 5, (6, 7, 8)))
tree2 = parse_tuple(tree_tuple)
tree_tuple2 = (1, 2, 3)
tree = parse_tuple(tree_tuple2)
print(tree_to_tuple(tree2))
输出:
((1, 3, None), 2, ((None, 3, 4), 5, (6, 7, 8)))
【讨论】:
非常感谢这个解决方案``` @ShivaKharbanda 不客气 :)【参考方案2】:这是我编写的对我有用的一段代码。
def tree_to_tuple(node):
if isinstance(node, TreeNode):
if node.left is not None and node.right is not None:
node_mid = node.key
node_left = tree_to_tuple(node.left)
node_right = tree_to_tuple(node.right)
return (node_left, node_mid, node_right)
elif node.left is None and node.right is None:
return node.key
elif node.left is None and node.right is not None:
node_mid = node.key
node_right = tree_to_tuple(node.right)
node_left = None
return (node_left, node_mid, node_right)
elif node.left is not None and node.right is None:
node_mid = node.key
node_right = None
node_left = tree_to_tuple(node.left)
return (node_left, node_mid, node_right)
else:
print("It's not a tree")
【讨论】:
以上是关于如何创建将二叉树转换为元组的函数?的主要内容,如果未能解决你的问题,请参考以下文章
编程将二叉树(子女右兄弟链)转换为树然后将树转换为二叉树(直接修改) 非递归实现
java - 如何使用堆栈安全(基于堆)递归将二叉树转换为java中的列表?