To write a Python program to build a binary search tree using a built-in function.
- Start the program.
- Define
_build_bst_from_sorted_values(sorted_values)to recursively build a binary search tree (BST) from a sorted list. - Define
left_subtree(l)to print the left subtree of the BST. - Take user input for the number of elements and store the values in a list
a. - Sort the list and pass it to
_build_bst_from_sorted_values()to construct the BST. - Print the postorder traversal of the BST.
- Call
left_subtree(l)to print the left subtree. - Check whether the tree is a binary search tree using the
is_bstproperty. - End the program.
from binarytree import Node
root=Node(1)
root.left=Node(2)
root.right=Node(3)
root.left.left=Node(5)
root.left.right=Node(6)
print("Binary tree: ")
for i in (root.values):
print(i,"-->",end="")
print("\nLeft Subtree: ")
for i in (root[1].values):
print(i,"-->",end="")
Therefore, the output is the example to write a Python program to build a binary search tree using a built-in function.