From 887f9e5b01c36ae3fb24355046c78ab3aee390dd Mon Sep 17 00:00:00 2001 From: Akshay Sharma Date: Fri, 14 Oct 2016 00:01:44 +0530 Subject: [PATCH 1/2] added isinstance check --- traverals/binary_tree_traversals.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/traverals/binary_tree_traversals.py b/traverals/binary_tree_traversals.py index c1e8783b2..6b0dd5ee9 100644 --- a/traverals/binary_tree_traversals.py +++ b/traverals/binary_tree_traversals.py @@ -40,7 +40,8 @@ def build_tree(): def pre_order(node): - if not node: + if not isinstance(node, TreeNode) or not node: + print("Invalid input") return print(node.data, end=" ") pre_order(node.left) @@ -48,7 +49,8 @@ def pre_order(node): def in_order(node): - if not node: + if not isinstance(node, TreeNode) or not node: + print("Invalid input") return in_order(node.left) print(node.data, end=" ") @@ -56,7 +58,8 @@ def in_order(node): def post_order(node): - if not node: + if not isinstance(node, TreeNode) or not node: + print("Invalid input") return post_order(node.left) post_order(node.right) @@ -64,7 +67,8 @@ def post_order(node): def level_order(node): - if not node: + if not isinstance(node, TreeNode) or not node: + print("Invalid input") return q = queue.Queue() q.put(node) From d62e8e28681819b17077782202881a13ee64dfe4 Mon Sep 17 00:00:00 2001 From: Akshay Sharma Date: Fri, 14 Oct 2016 01:27:52 +0530 Subject: [PATCH 2/2] travis cli build fail --- traverals/binary_tree_traversals.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/traverals/binary_tree_traversals.py b/traverals/binary_tree_traversals.py index 6b0dd5ee9..591142b27 100644 --- a/traverals/binary_tree_traversals.py +++ b/traverals/binary_tree_traversals.py @@ -6,7 +6,6 @@ import queue class TreeNode: - def __init__(self, data): self.data = data self.right = None @@ -49,8 +48,7 @@ def pre_order(node): def in_order(node): - if not isinstance(node, TreeNode) or not node: - print("Invalid input") + if not isinstance(node, TreeNode) or not node: return in_order(node.left) print(node.data, end=" ") @@ -58,8 +56,7 @@ def in_order(node): def post_order(node): - if not isinstance(node, TreeNode) or not node: - print("Invalid input") + if not isinstance(node, TreeNode) or not node: return post_order(node.left) post_order(node.right) @@ -67,8 +64,7 @@ def post_order(node): def level_order(node): - if not isinstance(node, TreeNode) or not node: - print("Invalid input") + if not isinstance(node, TreeNode) or not node: return q = queue.Queue() q.put(node) @@ -83,6 +79,7 @@ def level_order(node): if __name__ == '__main__': import sys + print("\n********* Binary Tree Traversals ************\n") # For python 2.x and 3.x compatibility: 3.x has not raw_input builtin # otherwise 2.x's input builtin function is too "smart"