Data structures 从二叉树中的顺序遍历序列中查找顺序遍历
我只给出了二叉树的一个预序遍历序列(例如,{a,b,d,c,e}),任务是从中找出顺序序列。如果这是一个重复的问题,请原谅。。。。谢谢我认为你不能仅仅根据二叉树的前序遍历来找出顺序遍历。正如您对二叉搜索树所说的,排序将为您提供索引遍历。我认为您无法仅基于二叉树的前序遍历来找到索引遍历。正如您所说的二进制搜索树,排序将为您提供按序遍历。我在Python中准备了一个函数,用于从按序遍历中获取按序遍历。希望这能帮你摆脱困境 比如说, 如果你像那样输入postorder 输入后序遍历:acedbigf 预定的是 预顺序遍历是GAECFTJOLP 顺序是 顺序遍历是ABCDEFGHIData structures 从二叉树中的顺序遍历序列中查找顺序遍历,data-structures,binary-tree,sequence,traversal,Data Structures,Binary Tree,Sequence,Traversal,我只给出了二叉树的一个预序遍历序列(例如,{a,b,d,c,e}),任务是从中找出顺序序列。如果这是一个重复的问题,请原谅。。。。谢谢我认为你不能仅仅根据二叉树的前序遍历来找出顺序遍历。正如您对二叉搜索树所说的,排序将为您提供索引遍历。我认为您无法仅基于二叉树的前序遍历来找到索引遍历。正如您所说的二进制搜索树,排序将为您提供按序遍历。我在Python中准备了一个函数,用于从按序遍历中获取按序遍历。希望这能帮你摆脱困境 比如说, 如果你像那样输入postorder 输入后序遍历:acedbigf
def from_post_order(post_order_items, order_type="pre"):
bst = BinarySearchTree()
values = [item for item in post_order_items]
root = values[-1] # the last item in the post_order item is ROOT
bst.insert(root) # insert ROOT
values.pop(-1) # and remove it from post_order_items
left_child = [] # the left child of ROOT for Post-order
right_child = [] # the right child of ROOT for Post-order
for v in values:
if v > root:
right_child.append(v)
else:
left_child.append(v)
for i in range(len(left_child + right_child)):
if len(left_child) != 0:
bst.insert(left_child[-1]) # insert left child
left_child.pop(-1) # remove the inserted left child from the list
if len(right_child) != 0:
bst.insert(right_child[-1]) # insert right child
right_child.pop(-1) # remove the inserted right child from the list
if order_type == "pre":
print("The pre-order traversal is ")
bst.preorder(print)
elif order_type == "in":
print("The in-order traversal is ")
bst.inorder(print)
else:
print("The post-order traversal is ")
bst.postorder(print)
我在Python中准备了一个函数,用于从后序遍历获得前序遍历。希望这能帮你摆脱困境 比如说, 如果你像那样输入postorder 输入后序遍历:acedbigf 预定的是 预顺序遍历是GAECFTJOLP 顺序是 顺序遍历是ABCDEFGHI
def from_post_order(post_order_items, order_type="pre"):
bst = BinarySearchTree()
values = [item for item in post_order_items]
root = values[-1] # the last item in the post_order item is ROOT
bst.insert(root) # insert ROOT
values.pop(-1) # and remove it from post_order_items
left_child = [] # the left child of ROOT for Post-order
right_child = [] # the right child of ROOT for Post-order
for v in values:
if v > root:
right_child.append(v)
else:
left_child.append(v)
for i in range(len(left_child + right_child)):
if len(left_child) != 0:
bst.insert(left_child[-1]) # insert left child
left_child.pop(-1) # remove the inserted left child from the list
if len(right_child) != 0:
bst.insert(right_child[-1]) # insert right child
right_child.pop(-1) # remove the inserted right child from the list
if order_type == "pre":
print("The pre-order traversal is ")
bst.preorder(print)
elif order_type == "in":
print("The in-order traversal is ")
bst.inorder(print)
else:
print("The post-order traversal is ")
bst.postorder(print)
这里要注意的是,如果树是带有数字键值的二叉搜索树,那么我知道顺序遍历序列是给定的前顺序或后顺序遍历序列的排序序列。但我想知道,如果树不是BST,而是只有字母标签的BT,那该怎么办。这里需要注意的是,如果树是带有数字键值的二元搜索树,那么我知道按序遍历序列是给定的前序或后序遍历序列的排序序列。但我想知道,如果这棵树不是BST,而是只有字母标签的BT,那该怎么办。