From 8bee866ab4d4882213f2b1d706f2e54250e8d462 Mon Sep 17 00:00:00 2001 From: Ben Moyer Date: Thu, 28 Sep 2017 10:27:40 -0700 Subject: [PATCH 1/2] fix pre-order traversal, add in-order and post-order traversal --- .../Binary Tree/binary_seach_tree.py | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/data_structures/Binary Tree/binary_seach_tree.py b/data_structures/Binary Tree/binary_seach_tree.py index 1dac948ae3c5..403cb6ca9718 100644 --- a/data_structures/Binary Tree/binary_seach_tree.py +++ b/data_structures/Binary Tree/binary_seach_tree.py @@ -67,12 +67,24 @@ def empty(self): return True return False - def preShow(self, curr_node): - if curr_node is None: + def preorderShow(self, curr_node): + if curr_node is not None: print(curr_node.getLabel(), end=" ") - self.preShow(curr_node.getLeft()) - self.preShow(curr_node.getRight()) + self.preorderShow(curr_node.getLeft()) + self.preorderShow(curr_node.getRight()) + + def postorderShow(self, curr_node): + if curr_node is not None: + self.postorderShow(curr_node.getLeft()) + self.postorderShow(curr_node.getRight()) + print(curr_node.getLabel(), end=" ") + + def inorderShow(self, curr_node): + if curr_node is not None: + self.inorderShow(curr_node.getLeft()) + print(curr_node.getLabel(), end=" ") + self.inorderShow(curr_node.getRight()) def getRoot(self): return self.root @@ -89,15 +101,18 @@ def getRoot(self): 4 7 13 ''' -t = BinarySearchTree() -t.insert(8) -t.insert(3) -t.insert(1) -t.insert(6) -t.insert(4) -t.insert(7) -t.insert(10) -t.insert(14) -t.insert(13) - -t.preShow(t.getRoot()) +if __name__ == '__main__': + t = BinarySearchTree() + t.insert(8) + t.insert(3) + t.insert(1) + t.insert(6) + t.insert(4) + t.insert(7) + t.insert(10) + t.insert(14) + t.insert(13) + + t.preorderShow(t.getRoot()) + t.inorderShow(t.getRoot()) + t.postorderShow(t.getRoot()) From c006fe81051a169e67477e2f3de30cf624095528 Mon Sep 17 00:00:00 2001 From: Ben Moyer Date: Thu, 28 Sep 2017 10:35:50 -0700 Subject: [PATCH 2/2] fix typo in file --- data_structures/Binary Tree/binary_seach_tree.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/data_structures/Binary Tree/binary_seach_tree.py b/data_structures/Binary Tree/binary_seach_tree.py index 403cb6ca9718..1805983da6ce 100644 --- a/data_structures/Binary Tree/binary_seach_tree.py +++ b/data_structures/Binary Tree/binary_seach_tree.py @@ -8,7 +8,7 @@ class Node: def __init__(self, label): self.label = label self.left = None - self.rigt = None + self.right = None def getLabel(self): return self.label @@ -23,10 +23,10 @@ def setLeft(self, left): self.left = left def getRight(self): - return self.rigt + return self.right def setRight(self, right): - self.rigt = right + self.right = right class BinarySearchTree: @@ -70,7 +70,6 @@ def empty(self): def preorderShow(self, curr_node): if curr_node is not None: print(curr_node.getLabel(), end=" ") - self.preorderShow(curr_node.getLeft()) self.preorderShow(curr_node.getRight())