-
Notifications
You must be signed in to change notification settings - Fork 62
Paper- Andrea Palacios #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,115 @@ | ||
| class TreeNode: | ||
| def __init__(self, key, val = None): | ||
| if val == None: | ||
| val = key | ||
|
|
||
| class TreeNode: | ||
| def __init__(self, key, value = None): | ||
| if value == None: | ||
| value = key | ||
|
|
||
| self.key = key | ||
| self.value = val | ||
| self.value = value | ||
| self.left = None | ||
| self.right = None | ||
|
|
||
|
|
||
|
|
||
| class Tree: | ||
| def __init__(self): | ||
| self.root = None | ||
|
|
||
| def add_helper(self, current, key, value): | ||
| if not current: | ||
| return TreeNode(key,value) | ||
| if key <= current.key: | ||
| current.left = self.add_helper(current.left,key,value) | ||
| else: | ||
| current.right = self.add_helper(current.right,key,value) | ||
| return current | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(log n) | ||
| def add(self, key, value = None): | ||
| pass | ||
| if self.root == None: | ||
| self.root = TreeNode(key,value) | ||
| else: | ||
| self.add_helper(self.root, key,value) | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def find(self, key): | ||
|
Comment on lines
+35
to
37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 However the space complexity is O(1) because it's an iterative solution (no call stack). The time complexity is O(log n) if the tree is balanced and O(n) worst-case. |
||
| pass | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| current = self.root | ||
| while current != None: | ||
| if current.key == key: | ||
| return current.value | ||
| elif key < current.key: | ||
| current = current.left | ||
| else: | ||
| current = current.right | ||
| return None | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def inorder(self): | ||
|
Comment on lines
+48
to
50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| pass | ||
| tree_nodes = [] | ||
| return self.inorder_helper(self.root, tree_nodes) | ||
|
|
||
| def inorder_helper(self, root, tree_nodes): | ||
| if root: | ||
| self.inorder_helper(root.left, tree_nodes) | ||
| tree_nodes.append({ | ||
| "key":root.key, | ||
| "value":root.value | ||
| }) | ||
| self.inorder_helper(root.right, tree_nodes) | ||
| return tree_nodes | ||
|
|
||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def preorder(self): | ||
|
Comment on lines
+65
to
67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| pass | ||
| tree_nodes = [] | ||
| return self.preorder_helper(self.root, tree_nodes) | ||
|
|
||
| def preorder_helper(self, root, tree_nodes): | ||
| if root: | ||
| tree_nodes.append({ | ||
| "key":root.key, | ||
| "value":root.value | ||
| }) | ||
| self.preorder_helper(root.left, tree_nodes) | ||
| self.preorder_helper(root.right, tree_nodes) | ||
| return tree_nodes | ||
|
|
||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def postorder(self): | ||
|
Comment on lines
+83
to
85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| pass | ||
| tree_nodes = [] | ||
| return self.postorder_helper(self.root, tree_nodes) | ||
|
|
||
| def postorder_helper(self, root, tree_nodes): | ||
| if root: | ||
| self.postorder_helper(root.left, tree_nodes) | ||
| self.postorder_helper(root.right, tree_nodes) | ||
| tree_nodes.append({ | ||
| "key":root.key, | ||
| "value":root.value | ||
| }) | ||
| return tree_nodes | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(h) | ||
| def height(self): | ||
|
Comment on lines
+99
to
101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Nice work on time/space |
||
| pass | ||
| return self.height_helper(self.root) | ||
|
|
||
| def height_helper(self, current): | ||
| if not current: | ||
| return 0 | ||
|
|
||
| left = self.height_helper(current.left) | ||
| right = self.height_helper(current.right) | ||
| return max(left, right)+1 | ||
|
|
||
|
|
||
|
|
||
|
|
||
| # # Optional Method | ||
|
|
@@ -57,3 +124,5 @@ def bfs(self): | |
| # # Useful for printing | ||
| def to_s(self): | ||
| return f"{self.inorder()}" | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Nice recursive solution