diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/binarytree.py | 40 | ||||
-rw-r--r-- | source/mongodb.py | 2 | ||||
-rw-r--r-- | source/testbinarytree.py | 1 |
3 files changed, 32 insertions, 11 deletions
diff --git a/source/binarytree.py b/source/binarytree.py index 8a793540..e12c5b6d 100644 --- a/source/binarytree.py +++ b/source/binarytree.py @@ -105,27 +105,47 @@ class CBOrdTree(object): else: return self.size(root.left) + 1 + self.size(root.right) - def printTree(self, root): + def pre_order_traversal(self, root, result=[]): """ - Prints the tree path. + Depth-first. Pre-order traversal. """ if root == None: pass else: - self.printTree(root.left) - print(root.data, end=' ') - self.printTree(root.right) + result.append(root.data) + self.in_order_traversal(root.left, result) + self.in_order_traversal(root.right, result) + return result - def printRevTree(self, root): + def in_order_traversal(self, root, result=[]): """ - Prints the tree path in reverse order. + Depth-first. In-order traversal. """ if root == None: pass else: - self.printRevTree(root.right) - print(root.data, end=' ') - self.printRevTree(root.left) + self.in_order_traversal(root.left, result) + result.append(root.data) + self.in_order_traversal(root.right, result) + return result + + def post_order_traversal(self, root, result=[]): + """ + Depth-first. Post-order traversal. + """ + if root == None: + pass + else: + self.in_order_traversal(root.left, result) + self.in_order_traversal(root.right, result) + result.append(root.data) + return result + + def __str__(self): + """ + Pretty display. + """ + return ", ".join([article["article_title"] for article in self.in_order_traversal(self.root)]) if __name__ == "__main__": # Point of entry in execution mode. diff --git a/source/mongodb.py b/source/mongodb.py index 38081780..9e64fe4f 100644 --- a/source/mongodb.py +++ b/source/mongodb.py @@ -45,7 +45,7 @@ class Articles(object): Creates a new collection for a new feed. """ collection = self.db[new_collection["feed_id"]] - collection.create_index([("article_date", pymongo.DESCENDING)], {"unique":False, "sparse":False}) + #collection.create_index([("article_date", pymongo.DESCENDING)], {"unique":False, "sparse":False}) collection.ensure_index('article_content', pymongo.ASCENDING) collection.insert(new_collection) diff --git a/source/testbinarytree.py b/source/testbinarytree.py index d60df372..526ddd7f 100644 --- a/source/testbinarytree.py +++ b/source/testbinarytree.py @@ -40,3 +40,4 @@ print("Newest article:") newest_article = BTree.maxValue(root) print((newest_article["article_date"].strftime('%Y-%m-%d %H:%M') + \ " - " + newest_article["article_title"])) +print(BTree) |