From 20b48cd787961dc8e9ba41ba5fd1e79308ce13cc Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Mon, 18 Mar 2013 18:07:07 +0100 Subject: Ported binary tree implementation to Python 3. --- source/binarytree.py | 24 ++++++++++++------------ source/testbinarytree.py | 22 ++++++++++++---------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/source/binarytree.py b/source/binarytree.py index 48b8ba4e..a83016e0 100644 --- a/source/binarytree.py +++ b/source/binarytree.py @@ -88,7 +88,7 @@ class CBOrdTree(object): pass else: self.printTree(root.left) - print root.data, + print(root.data, end=' ') self.printTree(root.right) def printRevTree(self, root): @@ -98,7 +98,7 @@ class CBOrdTree(object): pass else: self.printRevTree(root.right) - print root.data, + print(root.data, end=' ') self.printRevTree(root.left) if __name__ == "__main__": @@ -108,21 +108,21 @@ if __name__ == "__main__": root = BTree.addNode(0) # ask the user to insert values for i in range(0, 5): - data = int(raw_input("insert the node value nr %d: " % i)) + data = int(input("insert the node value nr %d: " % i)) # insert values BTree.insert(root, data) - print + print() BTree.printTree(root) - print + print() BTree.printRevTree(root) - print - data = int(raw_input("insert a value to find: ")) + print() + data = int(input("insert a value to find: ")) if BTree.lookup(root, data): - print "found" + print("found") else: - print "not found" + print("not found") - print BTree.minValue(root) - print BTree.maxDepth(root) - print BTree.size(root) \ No newline at end of file + print(BTree.minValue(root)) + print(BTree.maxDepth(root)) + print(BTree.size(root)) \ No newline at end of file diff --git a/source/testbinarytree.py b/source/testbinarytree.py index 27245a68..0d637758 100644 --- a/source/testbinarytree.py +++ b/source/testbinarytree.py @@ -12,8 +12,10 @@ import binarytree print("Loading articles from the database...") database = mongodb.Articles() +begin = time.time() articles = database.get_articles() -print("Articles loaded ({}).".format(len(articles))) +end = time.time() +print(("{} articles loaded in {} seconds.".format(len(articles), end-begin))) print("Generating the binary tree...") begin = time.time() @@ -23,15 +25,15 @@ root = BTree.addNode(articles[0]) for article in articles[1:]: BTree.insert(root, article) end = time.time() -print("Generation done ({0:2f} seconds).".format(end-begin)) +print(("Generation done in {0:2f} seconds.".format(end-begin))) -print "Maximum depth of the tree:" -print BTree.maxDepth(root) -print "Oldest article:" +print("Maximum depth of the tree:") +print(BTree.maxDepth(root)) +print("Oldest article:") oldest_article = BTree.minValue(root) -print(oldest_article["article_date"].strftime('%Y-%m-%d %H:%M') + \ - " - " + oldest_article["article_title"]) -print "Newest article:" +print((oldest_article["article_date"].strftime('%Y-%m-%d %H:%M') + \ + " - " + oldest_article["article_title"])) +print("Newest article:") newest_article = BTree.maxValue(root) -print(newest_article["article_date"].strftime('%Y-%m-%d %H:%M') + \ - " - " + newest_article["article_title"]) \ No newline at end of file +print((newest_article["article_date"].strftime('%Y-%m-%d %H:%M') + \ + " - " + newest_article["article_title"])) -- cgit