fix isbst bt

This commit is contained in:
Mari Wahl 2014-08-31 15:00:39 -04:00
parent 1af8145904
commit 8adc69d669
5 changed files with 39 additions and 88 deletions

Binary file not shown.

View File

@ -1,81 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
class HashTable:
''' HashTable class that implements the Map abstract data type with 2 lists'''
def __init__(self):
self.size = 11
self.slots = [None] * self.size
self.data = [None] * self.size
def put(self,key,data):
hashvalue = self.hashfunction(key,len(self.slots))
if self.slots[hashvalue] == None:
self.slots[hashvalue] = key
self.data[hashvalue] = data
else:
if self.slots[hashvalue] == key:
self.data[hashvalue] = data
else:
nextslot = self.rehash(hashvalue,len(self.slots))
while self.slots[nextslot] != None and \
self.slots[nextslot] != key:
nextslot = self.rehash(nextslot,len(self.slots))
if self.slots[nextslot] == None:
self.slots[nextslot]=key
self.data[nextslot]=data
else:
self.data[nextslot] = data
def hashfunction(self,key,size):
return key%size
def rehash(self,oldhash,size):
return (oldhash+1)%size
def get(self,key):
startslot = self.hashfunction(key,len(self.slots))
data = None
stop = False
found = False
position = startslot
while self.slots[position] != None and \
not found and not stop:
if self.slots[position] == key:
found = True
data = self.data[position]
else:
position=self.rehash(position,len(self.slots))
if position == startslot:
stop = True
return data
def __getitem__(self,key):
return self.get(key)
def __setitem__(self,key,data):
self.put(key,data)
def test_HashTable(module_name='this module'):
H = HashTable()
H[54]="buffy"
H[26]="xander"
H[17]="giles"
print(H.slots)
print(H.data)
s = 'Tests in {name} have {con}!'
print(s.format(name=module_name, con='passed'))
if __name__ == '__main__':
test_HashTable()

View File

@ -46,7 +46,7 @@ class NodeBT(object):
self.right = new_node self.right = new_node
else: else:
self.left = self.left._addNextNode(value, level_here+1) 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): def _searchForNode(self, value):
@ -99,19 +99,26 @@ class NodeBT(object):
elif not self.right and self.left: elif not self.right and self.left:
return self.left._isBalanced() return self.left._isBalanced()
def _isBST(self): def _isBST(self, mintree=None, maxtree=None):
''' Find whether the tree is a BST, inorder ''' ''' Find whether the tree is a BST, inorder '''
if self.item: if self.item:
if not mintree:
mintree = self.item
if not maxtree:
maxtree = self.item
if self._isLeaf(): if self._isLeaf():
return True return True
elif self.left: elif self.left:
if self.left.item < self.item: if self.left.item < self.item and mintree > self.left.item:
return self.left._isBST() mintree = self.left.item
return self.left._isBST(mintree, maxtree)
else: else:
return False return False
elif self.right: elif self.right:
if self.right.item > self.item: if self.right.item > self.item and maxtree < self.right.item:
return self.right._isBST() maxtree = self.right.item
return self.right._isBST(mintree, maxtree)
else: else:
return False return False
else: else:
@ -121,6 +128,9 @@ class NodeBT(object):
class BinaryTree(object): class BinaryTree(object):
def __init__(self): def __init__(self):
@ -141,6 +151,7 @@ class BinaryTree(object):
else: else:
print "Node not found." print "Node not found."
def getNodeLevel(self, item): def getNodeLevel(self, item):
node = self.root._searchForNode(item) node = self.root._searchForNode(item)
if node: if node:
@ -148,19 +159,37 @@ class BinaryTree(object):
else: else:
print('Node not found') print('Node not found')
def isRoot(self, value): def isRoot(self, value):
return self.root.item == value return self.root.item == value
def getHeight(self): def getHeight(self):
return self.root._getMaxHeight() return self.root._getMaxHeight()
def isBalanced(self): def isBalanced(self):
return self.root._isBalanced() return self.root._isBalanced()
def isBST(self): def isBST(self):
return self.root._isBST() 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__': if __name__ == '__main__':
bt = BinaryTree() bt = BinaryTree()
@ -173,4 +202,5 @@ if __name__ == '__main__':
print "Is node 1 a root? ", bt.isRoot(1) print "Is node 1 a root? ", bt.isRoot(1)
print "Whats the tree height? ", bt.getHeight() print "Whats the tree height? ", bt.getHeight()
print "Is this tree BST? ", bt.isBST() print "Is this tree BST? ", bt.isBST()
print "Is this tree balanced? ", bt.isBalanced() print "Is this tree balanced? ", bt.isBalanced()
print (bt.preorder())

View File

@ -24,6 +24,8 @@ def find_ancestor(path, low_value, high_value):
return current_value return current_value
elif low_value <= current_value <= high_value: elif low_value <= current_value <= high_value:
return current_value return current_value
else:
return None # this is probably never touched