From e731857f82c0407730e3c06929aaa902fdd24fc8 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Fri, 12 Jul 2013 21:21:20 +0200 Subject: [PATCH] enhances print node to take care of more special cases --- 01_binary_search_trees/bst.py | 11 ++++++++++- 01_binary_search_trees/bst_test.py | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/01_binary_search_trees/bst.py b/01_binary_search_trees/bst.py index 561ca41..163475b 100644 --- a/01_binary_search_trees/bst.py +++ b/01_binary_search_trees/bst.py @@ -134,4 +134,13 @@ class BinarySearchNode(): """ prints a single tree node. """ - return (str(self.left_child.value) + " " + str(self.value) + " " + str(self.right_child.value) ) \ No newline at end of file + ret = "" + if self.has_left_child(): + ret += str(self.left_child.value) + " " + + ret += str(self.value) + + if self.has_right_child(): + ret += " " + str(self.right_child.value) + + return ret \ No newline at end of file diff --git a/01_binary_search_trees/bst_test.py b/01_binary_search_trees/bst_test.py index badf96b..187a814 100644 --- a/01_binary_search_trees/bst_test.py +++ b/01_binary_search_trees/bst_test.py @@ -208,5 +208,9 @@ class test_binary_search_tree(unittest.TestCase): self.assertEqual(bst.__str__(), "4 6 10") + self.assertEqual(bst.root_node.right_child.__str__(), "10 16") + self.assertEqual(bst.root_node.right_child.right_child.__str__(), "16") + + if __name__ == '__main__': unittest.main()