class Node:
def __init__(self,left,value,right):
self.left = left
self.value = value
self.right = right
def insert(self,x):
if x < self.value:
if self.left == None:
self.left = Node(None,x,None)
return True
else:
return self.left.insert(x)
elif x > self.value:
if self.right == None:
self.right = Node(None,x,None)
return True
else:
return self.right.insert(x)
else:
return False
def __str__(self):
if self.left != None:
if self.right != None:
return str(self.left) + "," + str(self.value) + "," + str(self.right)
else:
return str(self.left) + "," + str(self.value)
else:
if self.right != None:
return str(self.value) + "," + str(self.right)
else:
return str(self.value)
class BST:
def __init__(self):
self.root = None
def empty(self):
return self.root == None
def insert(self,x):
if self.empty():
self.root = Node(None,x,None)
else:
self.root.insert(x)
def __str__(self):
if self.empty():
return "{ }"
else:
return "{" + str(self.root) + "}"
t = BST()
t.insert(50)
t.insert(30)
t.insert(60)
print(t)
{30,50,60}