-
Notifications
You must be signed in to change notification settings - Fork 44
Fire - Stacy #23
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?
Fire - Stacy #23
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 |
|---|---|---|
|
|
@@ -17,39 +17,52 @@ def initialize | |
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def add(key, value) | ||
| raise NotImplementedError | ||
| # Space Complexity: | ||
| def add(key, value = nil) | ||
| new_node = TreeNode.new(key, value) | ||
| if @root.nil? | ||
| @root = new_node | ||
| else | ||
| add_helper(@root, new_node) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Space Complexity: | ||
| def find(key) | ||
|
Comment on lines
+31
to
32
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 & Time complexity? |
||
| raise NotImplementedError | ||
| if @root.nil? | ||
| return nil | ||
| else | ||
| return find_helper(@root, key) | ||
| end | ||
|
|
||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def inorder | ||
|
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 & Time complexity? |
||
| raise NotImplementedError | ||
| return [] if @root.nil? | ||
| return inorder_helper(@root, []) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def preorder | ||
|
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 & Time complexity? |
||
| raise NotImplementedError | ||
| return [] if @root.nil? | ||
| return preorder_helper(@root, []) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def postorder | ||
|
Comment on lines
55
to
57
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 & Time complexity? |
||
| raise NotImplementedError | ||
| return [] if @root.nil? | ||
| return postorder_helper(@root, []) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def height | ||
|
Comment on lines
62
to
64
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 & Time complexity? |
||
| raise NotImplementedError | ||
| return height_helper(@root) | ||
| end | ||
|
|
||
| # Optional Method | ||
|
|
@@ -63,4 +76,62 @@ def bfs | |
| def to_s | ||
| return "#{self.inorder}" | ||
| end | ||
| end | ||
|
|
||
| private | ||
| def add_helper(current_node, new_node) | ||
| return new_node if current_node.nil? | ||
|
|
||
| if new_node.key <= current_node.key | ||
| current_node.left = add_helper(current_node.left, new_node) | ||
| else | ||
| current_node.right = add_helper(current_node.right, new_node) | ||
| end | ||
|
|
||
| return current_node | ||
| end | ||
|
|
||
| def find_helper(node, key) | ||
| if node.nil? | ||
| return nil | ||
| elsif node.key > key | ||
| return find_helper(node.left, key) | ||
| elsif node.key < key | ||
| return find_helper(node.right, key) | ||
| else | ||
| return node.value | ||
| end | ||
| end | ||
|
|
||
| def inorder_helper(node, array) | ||
| if node | ||
| inorder_helper(node.left, array) | ||
| array << {key: node.key, value: node.value} | ||
| inorder_helper(node.right, array) | ||
| end | ||
| return array | ||
| end | ||
|
|
||
| def preorder_helper(node, array) | ||
| if node | ||
| array << {key: node.key, value: node.value} | ||
| preorder_helper(node.left, array) | ||
| preorder_helper(node.right, array) | ||
| end | ||
| return array | ||
| end | ||
|
|
||
| def postorder_helper(node, array) | ||
| if node | ||
| postorder_helper(node.left, array) | ||
| postorder_helper(node.right, array) | ||
| array << {key: node.key, value: node.value} | ||
| end | ||
| return array | ||
| end | ||
|
|
||
| def height_helper(node) | ||
| return 0 if node.nil? | ||
| return 1 + [height_helper(node.right), height_helper(node.left)].max | ||
| end | ||
|
|
||
| end | ||
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.
👍 Space & Time complexity?