From c10041adce74db7fab004509feb5b799bc062f17 Mon Sep 17 00:00:00 2001 From: Brittany Clemons Date: Tue, 25 Jan 2022 19:23:18 -0800 Subject: [PATCH] binary_search_tree_ completed --- binary_search_tree/tree.py | 136 ++++++++++++++++++---- tests/__pycache__/__init__.cpython-39.pyc | Bin 152 -> 147 bytes 2 files changed, 111 insertions(+), 25 deletions(-) diff --git a/binary_search_tree/tree.py b/binary_search_tree/tree.py index cdd5abc..9f696e3 100644 --- a/binary_search_tree/tree.py +++ b/binary_search_tree/tree.py @@ -1,5 +1,5 @@ class TreeNode: - def __init__(self, key, val = None): + def __init__(self, key, val=None): if val == None: val = key @@ -7,53 +7,139 @@ def __init__(self, key, val = None): self.value = val self.left = None self.right = None - + + def add(self, key, value=None): + if key < self.key: + if self.left is None: + self.left = TreeNode(key, value) + return self.left + return self.left.add(key, value) + else: + if self.right is None: + self.right = TreeNode(key, value) + return self.right + return self.right.add(key, value) + + def inorder(self, list): + if self.left is not None: + self.left.inorder(list) + + list.append({ + "key": self.key, + "value": self.value + }) + + if self.right is not None: + self.right.inorder(list) + + def preorder(self, list): + list.append({ + "key": self.key, + "value": self.value + }) + + if self.left is not None: + self.left.preorder(list) + + if self.right is not None: + self.right.preorder(list) + + def postorder(self, list): + if self.left is not None: + self.left.postorder(list) + + if self.right is not None: + self.right.postorder(list) + + list.append({ + "key": self.key, + "value": self.value + }) + + def height(self): + if self.left is None: + leftHeight = 0 + else: + leftHeight = self.left.height() + + if self.right is None: + rightHeight = 0 + else: + rightHeight = self.right.height() + + return 1 + max(leftHeight, rightHeight) class Tree: def __init__(self): self.root = None - # Time Complexity: - # Space Complexity: - def add(self, key, value = None): - pass + # Time Complexity: + # Space Complexity: + def add(self, key, value=None): + if self.root is None: + self.root = TreeNode(key, value) + return self.root + else: + return self.root.add(key, value) + + def find_helper(self, current, key): + if current == None: + return None + elif current.key == key: + return current.value + elif key < current.key: + return self.find_helper(current.left, key) + + return self.find_helper(current.right, key) - # Time Complexity: - # Space Complexity: + # Time Complexity: o(1) + # Space Complexity:o(n) def find(self, key): - pass + return self.find_helper(self.root, key) - # Time Complexity: - # Space Complexity: + # Time Complexity: o(1) + # Space Complexity: o(1) def inorder(self): - pass + list = [] + if self.root is not None: + self.root.inorder(list) + return list + + # Time Complexity: o(n) + # Space Complexity:0(n) - # Time Complexity: - # Space Complexity: def preorder(self): - pass + list = [] + if self.root is not None: + self.root.preorder(list) + return list - # Time Complexity: - # Space Complexity: + # Time Complexity: 0(log n) + # Space Complexity:o(1) def postorder(self): - pass + list = [] + if self.root is not None: + self.root.postorder(list) + return list - # Time Complexity: - # Space Complexity: + # Time Complexity: o(n) + # Space Complexity:0(n) def height(self): - pass + if self.root is None: + return 0 + return self.root.height() # # Optional Method -# # Time Complexity: -# # Space Complexity: +# # Time Complexity: +# # Space Complexity: + def bfs(self): pass - - # # Useful for printing + def to_s(self): return f"{self.inorder()}" diff --git a/tests/__pycache__/__init__.cpython-39.pyc b/tests/__pycache__/__init__.cpython-39.pyc index aa55c2ed2909da5566d50f92972b26b188bfaed0..6783307c94b0079f191c6dcd01bbb4a5e382c72e 100644 GIT binary patch delta 50 zcmbQiIGK?*k(ZZ?0SI1Yy-sAD$ZI8Op&weDT2!o`n3AZUl3L~nq>_u{(@OIurWykP DTR9JE delta 55 zcmbQtID?Tlk(ZZ?0SE$HIT9Hr@>W3Dm78UC!XB1@?=O!oSCFbN*>L;co>X)SE J7EDYq1^|I^5NZGb