发送反馈
生成搜索树测试
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
针对二元搜索树创建包含极端情况的单元测试
在对 Cloud 控制台使用不同参数值时,或通过直接调用 Vertex AI API,您可以直接查询模型,并测试返回的结果。
系统指令
您是一位经验丰富的程序员,精通各种常见算法。您的任务是为二元搜索树的实现创建足够详细的单元测试,以确保此类的实现正确无误。请勿提供说明,只需创建单元测试即可。
我有一个二元搜索树的代码实现,但我不确定它是否正确。为了确认这一点,希望您能编写单元测试,以便我测试此类中的每个函数(私有函数除外)。您的单元测试必须包含极端情况,例如对空树执行操作和处理重复值。此外,您的代码应包含 main
函数。
bst.py
from dataclasses import dataclass
from typing import Optional
@dataclass
class TreeNode :
value : int
left : Optional [ 'TreeNode' ] = None
right : Optional [ 'TreeNode' ] = None
class BST :
def __init__ ( self ):
self . root : Optional [ TreeNode ] = None
def insert ( self , value : int ) - > None :
if not self . root :
self . root = TreeNode ( value )
else :
self . _insert_helper ( self . root , value )
def _insert_helper ( self , node : TreeNode , value : int ) - > None :
if value < node . value :
if node . left is None :
node . left = TreeNode ( value )
else :
self . _insert_helper ( node . left , value )
else :
if node . right is None :
node . right = TreeNode ( value )
else :
self . _insert_helper ( node . right , value )
def find ( self , value : int ) - > Optional [ TreeNode ]:
return self . _find_helper ( self . root , value )
def _find_helper ( self , node : Optional [ TreeNode ], value : int ) - > Optional [ TreeNode ]:
if node is None or node . value == value :
return node
if value < node . value :
return self . _find_helper ( node . left , value )
return self . _find_helper ( node . right , value )
def _lookup_min ( self , node : TreeNode ) - > TreeNode :
current = node
while current . left :
current = current . left
return current
def print_inorder ( self ) - > None :
self . _print_inorder_helper ( self . root )
print ()
def _print_inorder_helper ( self , node : Optional [ TreeNode ]) - > None :
if node :
self . _print_inorder_helper ( node . left )
print ( node . value , end = ' ' )
self . _print_inorder_helper ( node . right )
响应
import unittest
from bst import BST , TreeNode
class TestBST ( unittest . TestCase ):
def test_insert_empty ( self ):
bst = BST ()
bst . insert ( 5 )
self . assertEqual ( bst . root . value , 5 )
def test_insert_multiple ( self ):
bst = BST ()
bst . insert ( 5 )
bst . insert ( 3 )
bst . insert ( 7 )
bst . insert ( 1 )
bst . insert ( 4 )
bst . insert ( 6 )
bst . insert ( 8 )
self . assertEqual ( bst . root . value , 5 )
self . assertEqual ( bst . root . left . value , 3 )
self . assertEqual ( bst . root . right . value , 7 )
def test_insert_duplicates ( self ):
bst = BST ()
bst . insert ( 5 )
bst . insert ( 5 )
self . assertEqual ( bst . root . value , 5 )
self . assertIsNone ( bst . root . left )
self . assertIsNone ( bst . root . right )
def test_find_empty ( self ):
bst = BST ()
self . assertIsNone ( bst . find ( 5 ))
def test_find_present ( self ):
bst = BST ()
bst . insert ( 5 )
bst . insert ( 3 )
bst . insert ( 7 )
self . assertEqual ( bst . find ( 5 ) . value , 5 )
self . assertEqual ( bst . find ( 3 ) . value , 3 )
self . assertEqual ( bst . find ( 7 ) . value , 7 )
def test_find_absent ( self ):
bst = BST ()
bst . insert ( 5 )
bst . insert ( 3 )
bst . insert ( 7 )
self . assertIsNone ( bst . find ( 1 ))
self . assertIsNone ( bst . find ( 9 ))
def test_find_duplicates ( self ):
bst = BST ()
bst . insert ( 5 )
bst . insert ( 5 )
self . assertEqual ( bst . find ( 5 ) . value , 5 )
def test_print_inorder_empty ( self ):
bst = BST ()
bst . print_inorder ()
def test_print_inorder_non_empty ( self ):
bst = BST ()
bst . insert ( 5 )
bst . insert ( 3 )
bst . insert ( 7 )
bst . insert ( 1 )
bst . insert ( 4 )
bst . insert ( 6 )
bst . insert ( 8 )
bst . print_inorder ()
if __name__ == '__main__' :
unittest . main ()
型号:
gemini-1.5-flash-002
温度:
0.2
输出令牌数量上限:
8192
TopK:
40.0
TopP:
0.95
发送反馈
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可 获得了许可,并且代码示例已根据 Apache 2.0 许可 获得了许可。有关详情,请参阅 Google 开发者网站政策 。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-12-05。
需要向我们提供更多信息?
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2024-12-05。"],[],[]]