mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-25 01:41:20 -04:00
fix isbst bt
This commit is contained in:
parent
1af8145904
commit
8adc69d669
5 changed files with 39 additions and 88 deletions
|
@ -46,7 +46,7 @@ class NodeBT(object):
|
|||
self.right = new_node
|
||||
else:
|
||||
self.left = self.left._addNextNode(value, level_here+1)
|
||||
return self
|
||||
return self ## this is important, because the node return to the main
|
||||
|
||||
|
||||
def _searchForNode(self, value):
|
||||
|
@ -99,19 +99,26 @@ class NodeBT(object):
|
|||
elif not self.right and self.left:
|
||||
return self.left._isBalanced()
|
||||
|
||||
def _isBST(self):
|
||||
def _isBST(self, mintree=None, maxtree=None):
|
||||
''' Find whether the tree is a BST, inorder '''
|
||||
if self.item:
|
||||
if not mintree:
|
||||
mintree = self.item
|
||||
if not maxtree:
|
||||
maxtree = self.item
|
||||
|
||||
if self._isLeaf():
|
||||
return True
|
||||
elif self.left:
|
||||
if self.left.item < self.item:
|
||||
return self.left._isBST()
|
||||
if self.left.item < self.item and mintree > self.left.item:
|
||||
mintree = self.left.item
|
||||
return self.left._isBST(mintree, maxtree)
|
||||
else:
|
||||
return False
|
||||
elif self.right:
|
||||
if self.right.item > self.item:
|
||||
return self.right._isBST()
|
||||
if self.right.item > self.item and maxtree < self.right.item:
|
||||
maxtree = self.right.item
|
||||
return self.right._isBST(mintree, maxtree)
|
||||
else:
|
||||
return False
|
||||
else:
|
||||
|
@ -121,6 +128,9 @@ class NodeBT(object):
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class BinaryTree(object):
|
||||
|
||||
def __init__(self):
|
||||
|
@ -141,6 +151,7 @@ class BinaryTree(object):
|
|||
else:
|
||||
print "Node not found."
|
||||
|
||||
|
||||
def getNodeLevel(self, item):
|
||||
node = self.root._searchForNode(item)
|
||||
if node:
|
||||
|
@ -148,19 +159,37 @@ class BinaryTree(object):
|
|||
else:
|
||||
print('Node not found')
|
||||
|
||||
|
||||
def isRoot(self, value):
|
||||
return self.root.item == value
|
||||
|
||||
|
||||
def getHeight(self):
|
||||
return self.root._getMaxHeight()
|
||||
|
||||
|
||||
def isBalanced(self):
|
||||
return self.root._isBalanced()
|
||||
|
||||
|
||||
def isBST(self):
|
||||
return self.root._isBST()
|
||||
|
||||
|
||||
def preorder(self):
|
||||
current = self.root
|
||||
nodes, stack = [], []
|
||||
while stack or current:
|
||||
if current:
|
||||
nodes.append(current.item) # thats what change
|
||||
stack.append(current)
|
||||
current = current.left
|
||||
else:
|
||||
current = stack.pop()
|
||||
current = current.right
|
||||
return nodes
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
bt = BinaryTree()
|
||||
|
@ -173,4 +202,5 @@ if __name__ == '__main__':
|
|||
print "Is node 1 a root? ", bt.isRoot(1)
|
||||
print "Whats the tree height? ", bt.getHeight()
|
||||
print "Is this tree BST? ", bt.isBST()
|
||||
print "Is this tree balanced? ", bt.isBalanced()
|
||||
print "Is this tree balanced? ", bt.isBalanced()
|
||||
print (bt.preorder())
|
Loading…
Add table
Add a link
Reference in a new issue