diff --git a/book/book_second_edition.pdf b/book/book_second_edition.pdf index c52833d..e734a79 100644 Binary files a/book/book_second_edition.pdf and b/book/book_second_edition.pdf differ diff --git a/src/programming_paradigms/oop/do_benchmark.py b/src/programming_paradigms/modules/do_benchmark.py similarity index 100% rename from src/programming_paradigms/oop/do_benchmark.py rename to src/programming_paradigms/modules/do_benchmark.py diff --git a/src/programming_paradigms/oop/HashTable.py b/src/programming_paradigms/oop/HashTable.py deleted file mode 100644 index 073b4b1..0000000 --- a/src/programming_paradigms/oop/HashTable.py +++ /dev/null @@ -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() - diff --git a/src/trees/binary_tree.py b/src/trees/binary_tree.py index fc33a52..31f0102 100644 --- a/src/trees/binary_tree.py +++ b/src/trees/binary_tree.py @@ -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() \ No newline at end of file + print "Is this tree balanced? ", bt.isBalanced() + print (bt.preorder()) \ No newline at end of file diff --git a/src/trees/transversal_BST_ancestor.py b/src/trees/transversal_BST_ancestor.py index 51288cf..9be8c92 100644 --- a/src/trees/transversal_BST_ancestor.py +++ b/src/trees/transversal_BST_ancestor.py @@ -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