Question: Here is a definition of a function is BST that takes a binary tree and decides if it is a binary search tree. Function: is
Here is a definition of a function is BST that takes a binary tree and decides if it is a binary search tree.
Function: is BST Inputs: t, a binary tree Preconditions: items in the t must be (pairwise) comparable Output: result, a Boolean Postconditions: result is true if t is a binary search tree, or if for every node in t the left child is either empty or less than the node's root and the right child is either empty or greater than the node's root - and false otherwise
The recursive function is_BST() provided in the file Q3_BST.py implements the is BSTfunction.
THIS CODE IS HERE IN BOLD:
# implementation of is BST function def is_BST(t:Tree) -> bool: """ Preconditions: items in the tree must be comparable Postconditions: output is true if t is a binary search tree, false otherwise """ if is_empty(t): return True else: the_root = t.root left_tree = t.left left_root = left_tree.root right_tree = t.right right_root = right_tree.root if left_root == None: left_root_ok = True else: left_root_ok = (left_root < the_root) if right_root == None: right_root_ok = True else: right_root_ok = (right_root > the_root) return (is_BST(left_tree) and is_BST(right_tree) and left_root_ok and right_root_ok)
Write code to construct test trees and complete the test table with six different tests to test this implementation. Add comments to the test table to indicate any edge cases.
THIS IS MY CODE WITH THE OUTPUT BELOW IN BOLD:
%run -i m269_tree %run -i m269_util %run -i Q3_BST
# Write your code to construct test trees here BST1 = join(10, join(5, join(3), join(7)), join(15, join(13), join(17))) BST2 = join(10, join(5, join(3), join(7)), join(15, join(13), join(20))) not_BST1 = join(10, join(5, join(3), join(7)), join(15, join(13), join(10))) not_BST2 = join(10, join(5, join(3), join(12)), join(15, join(13), join(7))) not_BST3 = join(10, join(5, join(3), join(7)), join(15, join(13), EMPTY))
# Complete the test table BST_tests = [ # case, t, result ('empty tree', EMPTY, True), ('single node tree', join(10), True), ('valid BST tree', BST1, True), ('valid BST tree', BST2, True), ('invalid BST tree', not_BST1, False), ('invalid BST tree', not_BST2, False), ('invalid BST tree', not_BST3, False) ]
test(is_BST, BST_tests)
I AM GETTING THE FOLLOWING OUTPUT. I AM NOT SURE HOW TO FIX IT. PLEASE ANSWER BELOW CHANGES I NEED TO MAKE:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) ~/Q3_BST.py in4 5 # Write your code to construct test trees here ----> 6 BST1 = join(10, join(5, join(3), join(7)), join(15, join(13), join(17))) 7 BST2 = join(10, join(5, join(3), join(7)), join(15, join(13), join(20))) 8 not_BST1 = join(10, join(5, join(3), join(7)), join(15, join(13), join(10))) TypeError: join() missing 2 required positional arguments: 'left' and 'right'
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
