上述问题输入输出应为>>> t1 = Tree(6, [Tree(2, [Tree(1), Tree(4)]), Tree(7, [Tree(7), Tree(8)])]) >>> is_bst(t1) True >>> t2 = Tree(8, [Tree(2, [Tree(9), Tree(1)]), Tree(3, [Tree(6)]), Tree(5)]) >>> is_bst(t2) False >>> t3 = Tree(6, [Tree(2, [Tree(4), Tree(1)]), Tree(7, [Tree(7), Tree(8)])]) >>> is_bst(t3) False >>> t4 = Tree(1, [Tree(2, [Tree(3, [Tree(4)])])]) >>> is_bst(t4) True >>> t5 = Tree(1, [Tree(0, [Tree(-1, [Tree(-2)])])]) >>> is_bst(t5) True >>> t6 = Tree(1, [Tree(4, [Tree(2, [Tree(3)])])]) >>> is_bst(t6) True >>> t7 = Tree(2, [Tree(1, [Tree(5)]), Tree(4)]) >>> is_bst(t7) False
时间: 2024-03-03 21:50:53 浏览: 118
Here's how the `is_bst` function could be tested using the input/output pairs you provided:
```python
t1 = Tree(6, [Tree(2, [Tree(1), Tree(4)]), Tree(7, [Tree(7), Tree(8)])])
assert is_bst(t1) == True
t2 = Tree(8, [Tree(2, [Tree(9), Tree(1)]), Tree(3, [Tree(6)]), Tree(5)])
assert is_bst(t2) == False
t3 = Tree(6, [Tree(2, [Tree(4), Tree(1)]), Tree(7, [Tree(7), Tree(8)])])
assert is_bst(t3) == False
t4 = Tree(1, [Tree(2, [Tree(3, [Tree(4)])])])
assert is_bst(t4) == True
t5 = Tree(1, [Tree(0, [Tree(-1, [Tree(-2)])])])
assert is_bst(t5) == True
t6 = Tree(1, [Tree(4, [Tree(2, [Tree(3)])])])
assert is_bst(t6) == True
t7 = Tree(2, [Tree(1, [Tree(5)]), Tree(4)])
assert is_bst(t7) == False
```
These assertions test the `is_bst` function against the expected output for each of the provided input trees. If all of the assertions pass, then the function should be correct for these test cases.
阅读全文