我想返回一个排序顺序的列表,只要给我一个方法的开始/结束值,例如,如果start=2和end=8,那么我想隐式地返回一个列表,在这个范围内,BST中的值按排序顺序排列。
由于我希望它是排序的顺序,并且不允许在方法调用后对列表进行后排序,我认为我应该通过顺序遍历来遍历bst。当我测试我的实现时,first first doctest返回[7,9,11],而不是预期的[5,7,9,11]。
from __future__ import annotations
from typing import Any, List, Optional, Tuple
class BinarySearchTree:
"""Binary Search Tree class.
# === Representation Invariants ===
# - If self._root is None, then so are self._left and self._right.
# This represents an empty BST.
# - If self._root is not None, then self._left and self._right
# are BinarySearchTrees.
# - (BST Property) If self is not empty, then
# all items in self._left are <= self._root, and
# all items in self._right are >= self._root.
"""
def __init__(self, root: Optional[Any]) -> None:
"""Initialize a new BST containing only the given root value.
If <root> is None, initialize an empty tree.
"""
if root is None:
self._root = None
self._left = None
self._right = None
else:
self._root = root
self._left = BinarySearchTree(None)
self._right = BinarySearchTree(None)
def is_empty(self) -> bool:
"""Return True if this BST is empty.
>>> bst = BinarySearchTree(None)
>>> bst.is_empty()
True
>>> bst = BinarySearchTree(10)
>>> bst.is_empty()
False
"""
return self._root is None
def items_in_range(self, start: Any, end: Any) -> List:
"""Return the items in this BST between <start> and <end>, inclusive.
Precondition: all items in this BST can be compared with <start> and
<end>.
The items should be returned in sorted order.
As usual, use the BST property to minimize the number of recursive
calls.
>>> bst = BinarySearchTree(7)
>>> left = BinarySearchTree(3)
>>> left._left = BinarySearchTree(2)
>>> left._right = BinarySearchTree(5)
>>> right = BinarySearchTree(11)
>>> right._left = BinarySearchTree(9)
>>> right._right = BinarySearchTree(13)
>>> bst._left = left
>>> bst._right = right
>>> bst.items_in_range(4, 11)
[5, 7, 9, 11]
>>> bst.items_in_range(10, 13)
[11, 13]
"""
if self.is_empty():
return []
else:
#use helper here
if end >= self._root >= start:
return (self._left._helper_items_in_range_left(start)
+ [self._root]
+ self._right._helper_item_in_range_right(end))
elif self._root > end:
return self._left.items_in_range(start,end)
elif self._root < start:
return self._right.items_in_range(start,end)
else:
pass
def _helper_items_in_range_left(self, start):
if self.is_empty():
return []
elif self._root < start:
return []
else:
return self._left._helper_items_in_range_left(start) +\
[self._root] + self._right._helper_items_in_range_left(start)
def _helper_item_in_range_right(self, end):
if self.is_empty():
return []
elif self._root > end:
return []
else:
return self._left._helper_item_in_range_right(end) + [self._root] +\
self._right._helper_item_in_range_right(end)
2条答案
按热度按时间0x6upsns1#
你可以使用类似这样的东西。注意我用一个不同的树结构测试了它。
使用的树类型:
测试对象:
打印:
ua4mk5z42#
这就是我如何定义一个方法,该方法返回给定范围内的节点列表(包括给定范围,非降序)。