In [1]:
In [1]:
from showtree import show_binary_tree
from random import sample
class Node:
def __init__(self, val=''):
self.data=val
self.left=None
self.right=None
def add_node(root, val):
if not root:
return Node(val)
if val<root.data:
root.left=add_node(root.left, val)
elif val>root.data:
root.right=add_node(root.right,val)
return root
def build_bst(lst):
bst=None
for v in lst:
bst=add_node(bst,v)
return bst
bst=build_bst([5,3,4,2,6,8])
# show_binary_tree(bst)
In [2]:
r=sample(range(1000),30)
show_binary_tree(build_bst(r))
In [ ]: