-
Notifications
You must be signed in to change notification settings - Fork 68
Shaina Beth C16 Spruce #53
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 |
|---|---|---|
|
|
@@ -14,42 +14,154 @@ class Tree: | |
| def __init__(self): | ||
| self.root = None | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(1) | ||
| def add(self, key, value = None): | ||
| pass | ||
| cur = self.root | ||
| if cur == None: | ||
| self.root = TreeNode(key, value) | ||
| return | ||
| while True: | ||
| if key < cur.key: | ||
| if cur.left == None: | ||
| cur.left = TreeNode(key, value) | ||
| return | ||
| else: | ||
| cur = cur.left | ||
| else: | ||
| if cur.right == None: | ||
| cur.right = TreeNode(key, value) | ||
| return | ||
| else: | ||
| cur = cur.right | ||
|
|
||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(1) | ||
| def find(self, key): | ||
|
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 | ||
| cur = self.root | ||
| if cur == None: | ||
| return None | ||
| while cur != None and cur.key != key: | ||
| if key < cur.key: | ||
| cur = cur.left | ||
| else: | ||
| cur = cur.right | ||
| if cur == None: | ||
| return None | ||
| return cur.value | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def inorder(self): | ||
|
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 inner function |
||
| pass | ||
| result = [] | ||
| def inorder_helper(node): | ||
| if node == None: | ||
| return | ||
| if node.left != None: | ||
| inorder_helper(node.left) | ||
| result.append({ | ||
| "key": node.key, | ||
| "value": node.value | ||
| }) | ||
| if node.right != None: | ||
| inorder_helper(node.right) | ||
| inorder_helper(self.root) | ||
| return result | ||
|
|
||
|
|
||
|
|
||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def preorder(self): | ||
|
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 | ||
| result = [] | ||
| def preorder_helper(node): | ||
| if node == None: | ||
| return | ||
| result.append({ | ||
| "key": node.key, | ||
| "value": node.value | ||
| }) | ||
| if node.left != None: | ||
| preorder_helper(node.left) | ||
| if node.right != None: | ||
| preorder_helper(node.right) | ||
| preorder_helper(self.root) | ||
| return result | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def postorder(self): | ||
|
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 | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| result = [] | ||
| def postorder_helper(node): | ||
| if node == None: | ||
| return | ||
| if node.left != None: | ||
| postorder_helper(node.left) | ||
| if node.right != None: | ||
| postorder_helper(node.right) | ||
| result.append({ | ||
| "key": node.key, | ||
| "value": node.value | ||
| }) | ||
| postorder_helper(self.root) | ||
| return result | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
|
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. 🪐 Space complexity will be O(n) because you make a recursive call on each subtree (each node can be considered a subtree) |
||
| def height(self): | ||
| pass | ||
|
|
||
| if self.root == None: | ||
| return 0 | ||
| def height_helper(node, height): | ||
| if node == None: | ||
| return | ||
| left_best = -1 | ||
| right_best = -1 | ||
| if node.left != None: | ||
| left_best = height_helper(node.left, height + 1) | ||
| if node.right != None: | ||
| right_best = height_helper(node.right, height + 1) | ||
| return max([left_best, right_best, height]) | ||
| return height_helper(self.root, 1) | ||
|
|
||
| # # Optional Method | ||
| # # Time Complexity: | ||
| # # Space Complexity: | ||
| # # Time Complexity: O(n) | ||
| # # Space Complexity: O(n) | ||
| def bfs(self): | ||
|
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. 🌟 Very nice! |
||
| pass | ||
| cur = self.root | ||
| if cur == None: | ||
| return [] | ||
|
|
||
| res = [{ | ||
| "key": self.root.key, | ||
| "value": self.root.value | ||
| }] | ||
| parents = [self.root] | ||
|
|
||
| while len(parents) > 0: | ||
| new_parents = [] | ||
| for parent in parents: | ||
| if parent.left != None: | ||
| res.append({ | ||
| "key": parent.left.key, | ||
| "value": parent.left.value | ||
| }) | ||
| new_parents.append(parent.left) | ||
| if parent.right != None: | ||
| res.append({ | ||
| "key": parent.right.key, | ||
| "value": parent.right.value | ||
|
|
||
| }) | ||
| new_parents.append(parent.right) | ||
| parents = new_parents | ||
| return res | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
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.
✨