[英]InOrder Traversal in Python
The problem I am tackle with is to find the first occurrence node in its inorder traversal in a BST. 我要解决的问题是在BST中找到其顺序遍历中的第一个出现节点。 The code I have is given below
我的代码如下
def Inorder_search_recursive(node,key):
if not node:
return None
InOrder_search_recursive(node.lChild)
if node.value==key:
return node
InOrder_search_recursive(node.rChild)
This code always return None, what's wrong with it. 这段代码总是返回None,这是怎么回事。 I think I've return node when I find a node with value k.
当我找到一个值为k的节点时,我想已经返回了节点。 Why cannot python pass this node???Thanks in advance
为什么python无法通过该节点?
When you call yourself recursively, like this: 当您递归调用自己时,如下所示:
InOrder_search_recursive(node.lChild)
That's just a normal function call, like any other. 就像其他函数一样,这只是一个普通的函数调用。 It just calls the function and gets back a result.
它只是调用该函数并返回结果。 It doesn't automatically
return
the value from that function, or do anything else. 它不会自动从该函数
return
值,或执行其他任何操作。
So, you do the left-subtree search, ignore the results, then go on to check node.value == key
, and, if that fails, you do the right-subtree search, again ignore the results, and fall off the end of the function, meaning you return None
. 因此,您进行左子树搜索,忽略结果,然后继续检查
node.value == key
,如果失败,则进行右子树搜索,再次忽略结果,最后函数的含义,即您返回None
。
To make this work, you need to return
the value you got back. 为了使此工作有效,您需要
return
获得的值。 But, of course, only if it's not None
. 但是,当然,仅当它
not None
。
Also, you forgot to pass the key
argument down to the recursive call, so you're just going to get a TypeError
. 另外,您忘记了将
key
参数传递给递归调用,因此您只会得到TypeError
。 (I'm guessing your real code doesn't have this problem, but since you didn't show us your real code, or a working example, I can't be sure…) (我猜您的真实代码没有这个问题,但是由于您没有向我们展示您的真实代码或有效的示例,所以我不确定…)
So: 所以:
def Inorder_search_recursive(node, key):
if not node:
return None
result = InOrder_search_recursive(node.lChild, key)
if result is not None:
return result
if node.value==key:
return node
return InOrder_search_recursive(node.rChild, key)
(You don't need the not None
check for the right-side search, because if it returns None
, we have nothing else to try and are just going to return None
anyway.) (你并不需要
not None
对右侧的搜索检查,因为如果它返回None
,我们没有别的尝试,只是要返回None
反正。)
Since your problem is to find the first occurrence node in its inorder traversal
, you should 1) traverse the tree in-order and 2) stop when you find the first occurrence. 由于您的问题是
to find the first occurrence node in its inorder traversal
顺序to find the first occurrence node in its inorder traversal
,因此您应该1)按顺序遍历树,并且2)在to find the first occurrence node in its inorder traversal
时停止。
def search(node, key):
if node is None:
return None
# Search the left subtree and return early if key is found
n = search(node.lChild, key)
if n is not None:
return n
# Check middle and return early if key is found
if node.value == key:
return node
# Search right subtree
return search(node.rChild, key)
My other answer gives the novice-friendly solution, but if you want more powerful and concise answer: 我的另一个答案是对新手友好的解决方案,但是如果您想要更强大,更简洁的答案:
def Inorder_search_recursive_all(node, key):
if not node:
return
yield from InOrder_search_recursive(node.lChild, key)
if node.value==key:
yield node
yield from InOrder_search_recursive(node.rChild, key)
This generates all matches in the tree, in order. 这将按顺序生成树中的所有匹配项。 And it gives them to you as an iterator, so if you just want the first, you can stop as soon as you find one, with no wasted work:
它为您提供了一个迭代器,因此,如果您只想要第一个,则可以在找到一个迭代器后立即停止,而不会浪费工作:
def Inorder_search_recursive(node, key):
return next(Inorder_search_recursive_all(node, key), None)
The tutorial section on Iterators and the following section on Generators explains most of how this works. 关于迭代器的教程部分以及关于生成器的以下部分说明了大多数工作原理。 The only missing bit is an explanation of
yield from
, which is explained in PEP 380 . 唯一缺少的一点是对
yield from
的解释,这在PEP 380中进行了解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.