mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
fix isbst bt
This commit is contained in:
parent
1af8145904
commit
8adc69d669
Binary file not shown.
@ -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()
|
||||
|
@ -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()
|
||||
@ -174,3 +203,4 @@ if __name__ == '__main__':
|
||||
print "Whats the tree height? ", bt.getHeight()
|
||||
print "Is this tree BST? ", bt.isBST()
|
||||
print "Is this tree balanced? ", bt.isBalanced()
|
||||
print (bt.preorder())
|
@ -24,6 +24,8 @@ def find_ancestor(path, low_value, high_value):
|
||||
return current_value
|
||||
elif low_value <= current_value <= high_value:
|
||||
return current_value
|
||||
else:
|
||||
return None # this is probably never touched
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user