aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCédric Bonhomme <kimble.mandel@gmail.com>2013-03-19 13:03:04 +0100
committerCédric Bonhomme <kimble.mandel@gmail.com>2013-03-19 13:03:04 +0100
commit85cf121bd14b0e0564ea1bee5dafce175a1bbf2e (patch)
tree809a0b46f518bb6b4a6ad37d323ad89af21b8964
parentRestored favicon. (diff)
downloadnewspipe-85cf121bd14b0e0564ea1bee5dafce175a1bbf2e.tar.gz
newspipe-85cf121bd14b0e0564ea1bee5dafce175a1bbf2e.tar.bz2
newspipe-85cf121bd14b0e0564ea1bee5dafce175a1bbf2e.zip
In and Post order depth traversal.
-rw-r--r--source/binarytree.py40
-rw-r--r--source/mongodb.py2
-rw-r--r--source/testbinarytree.py1
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)
bgstack15