I want to write a function that shows me if a given tree is BinarySearch or not.
This is what I wrote so far:
class Node:
def _isBinary(self):
L=[]
if self.left is not None and self.right is not None:
if self.left.data>self.data or self.right.data<self.data:
L.append(1)
else:
L+=self.left._isBinary()
L+=self.right._isBinary()
else:
if self.left is not None:
if self.left.data>self.datat:
L.append(1)
else:
self.left._isBinary()
if self.right is not None:
if self.right.data<self.data:
L.append(1)
else:
self.right._isBinary()
return L
class tree:
def isBinary(self):
if self.root is None:
return
else:
return not 1 in self.root._isBinary(self.root.data)
(obisivuly I just reported the interested part of the code) This code works finely, but gives me the incorrect answer when, for example, a number (bigger than the root) is in the left side of the tree, but is the children of a lower number:
99
/ \
8 888
\
100
It should give me False, instead it returns True. What can I do? (if possible, without changing completely my original code?)