class BinNode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class BinTree:
def __init__(self, data):
self.root = BinNode(data)
def preprint(self, r):
if r is None:
return
print(r.data, end=' ')
self.preprint(r.left)
self.preprint(r.right)
def findX(self, r, x):
if r is None:
return
if r.data==x:
return r
found = self.findX(r.left, x)
if found is not None:
return found
return self.findX(r.right, r)
def insert(self, r, p, lorr, x):
found = self.findX(r, p)
if found is None:
return
if lorr == 0:
if found.left is not None:
return
found.left = BinNode(x)
else:
if found.right is not None:
return
found.right = BinNode(x)
bt = BinTree(11)
bt.insert(bt.root, 11, 0, 22)
bt.insert(bt.root, 11, 1, 33)
bt.insert(bt.root, 22, 1, 44)
bt.insert(bt.root, 22, 0, 55)
bt.insert(bt.root, 33, 0, 66)
bt.preprint(bt.root)