aboutsummaryrefslogtreecommitdiff
path: root/source/binarytree.py
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 /source/binarytree.py
parentRestored favicon. (diff)
downloadnewspipe-85cf121bd14b0e0564ea1bee5dafce175a1bbf2e.tar.gz
newspipe-85cf121bd14b0e0564ea1bee5dafce175a1bbf2e.tar.bz2
newspipe-85cf121bd14b0e0564ea1bee5dafce175a1bbf2e.zip
In and Post order depth traversal.
Diffstat (limited to 'source/binarytree.py')
-rw-r--r--source/binarytree.py40
1 files changed, 30 insertions, 10 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.
bgstack15