-
Notifications
You must be signed in to change notification settings - Fork 43
Raisah #33
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?
Raisah #33
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 |
|---|---|---|
|
|
@@ -16,48 +16,200 @@ def initialize | |
| @root = nil | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(logn), at worst, the algorighm will be called O(logn) times to add the new node to the bottom of the tree. | ||
| # Space Complexity: O(1) the amount of data stored is constant, though the space required in memory for the stack trace will be O(logn) | ||
| def add(key, value) | ||
| raise NotImplementedError | ||
| if @root.nil? | ||
| @root = TreeNode.new(key, value) | ||
| else | ||
| add_helper(@root, key, value) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def add_helper(current, key, value) | ||
| if current.nil? | ||
| return TreeNode.new(key, value) | ||
| else | ||
| if key < current.key | ||
| current.left = add_helper(current.left, key, value) | ||
| else | ||
| current.right = add_helper(current.right, key, value) | ||
| end | ||
| end | ||
|
|
||
| return current | ||
| end | ||
|
|
||
| # Time Complexity: O(logn), at worst, the algorighm will be called O(logn) times to add the new node to the bottom of the tree. | ||
| # Space Complexity: O(1) the amount of data stored is constant, though the space required in memory for the stack trace will be O(logn) | ||
| def find(key) | ||
|
Comment on lines
+43
to
45
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. 👍 |
||
| raise NotImplementedError | ||
| if @root.nil? | ||
| return nil | ||
| else | ||
| return find_helper(@root, key) | ||
| end | ||
| end | ||
|
|
||
| def find_helper(current, key) | ||
| if current.key == key | ||
| return current.value | ||
| end | ||
|
|
||
| if key < current.key | ||
| find_helper(current.left, key) | ||
| else | ||
| find_helper(current.right, key) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n), because the algorithm will have to visit every node on the tree to add them to the nodes_array list | ||
| # Space Complexity: O(n) because the size of the nodes_array list will vary linearly with the number of nodes in the tree. | ||
| def inorder | ||
|
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. 👍 |
||
| raise NotImplementedError | ||
| return inorder_helper(@root, []) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def inorder_helper(current, nodes_array) | ||
| unless current.nil? | ||
| inorder_helper(current.left, nodes_array) | ||
| nodes_array << { key: current.key, value: current.value } | ||
| inorder_helper(current.right, nodes_array) | ||
| end | ||
|
|
||
| return nodes_array | ||
| end | ||
|
|
||
| # Time Complexity: O(n), because the algorithm will have to visit every node on the tree to add them to the nodes_array list | ||
| # Space Complexity: O(n) because the size of the nodes_array list will vary linearly with the number of nodes in the tree. | ||
| def preorder | ||
|
Comment on lines
+81
to
83
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. 👍 |
||
| raise NotImplementedError | ||
| return preorder_helper(@root, []) | ||
| end | ||
|
|
||
| def preorder_helper(current, nodes_array) | ||
| unless current.nil? | ||
| nodes_array << { key: current.key, value: current.value } | ||
| preorder_helper(current.left, nodes_array) | ||
| preorder_helper(current.right, nodes_array) | ||
| end | ||
|
|
||
| return nodes_array | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n), because the algorithm will have to visit every node on the tree to add them to the nodes_array list | ||
| # Space Complexity: O(n) because the size of the nodes_array list will vary linearly with the number of nodes in the tree. | ||
| def postorder | ||
|
Comment on lines
+97
to
99
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. 👍 |
||
| raise NotImplementedError | ||
| return postorder_helper(@root, []) | ||
| end | ||
|
|
||
| def postorder_helper(current, nodes_array) | ||
| unless current.nil? | ||
| postorder_helper(current.left, nodes_array) | ||
| postorder_helper(current.right, nodes_array) | ||
| nodes_array << { key: current.key, value: current.value } | ||
| end | ||
|
|
||
| return nodes_array | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n), because the algorithm will have to visit every node on the tree in order to determine the height of the tree | ||
| # Space Complexity: O(1) because the amount of data stored is constant, though the space required in memory for the stack trace will be O(n) | ||
| def height | ||
|
Comment on lines
+113
to
115
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. |
||
| raise NotImplementedError | ||
| return 0 if @root.nil? | ||
|
|
||
| return height_helper(@root) | ||
| end | ||
|
|
||
| def height_helper(current) | ||
| return 0 if current.nil? | ||
|
|
||
| left = 1 + height_helper(current.left) | ||
| right = 1 + height_helper(current.right) | ||
|
|
||
| return [left, right].max | ||
| end | ||
|
|
||
| # Optional Method | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n), because the algorithm will have to visit every node on the tree to add them to the nodes_array list | ||
| # Space Complexity: O(n) because the size of the nodes_array list will vary linearly with the number of nodes in the tree. | ||
| def bfs | ||
|
Comment on lines
+131
to
133
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. Unfortunately this is more like a preorder traversal, this is because you traverse the entire left subtree before you traverse the right subtree. Remember all nodes at level 2 should be next to each other in the array and level 3 etc. |
||
| raise NotImplementedError | ||
| nodes_array = [] | ||
|
|
||
| unless @root.nil? | ||
| nodes_array << { key: @root.key, value: @root.value} | ||
| end | ||
|
|
||
| return bfs_helper(@root, nodes_array) | ||
| end | ||
|
|
||
| def bfs_helper(current, nodes_array) | ||
| return nodes_array if current.nil? | ||
|
|
||
| nodes_array << { key: current.left.key, value: current.left.value } if current.left | ||
| nodes_array << { key: current.right.key, value: current.right.value } if current.right | ||
|
|
||
| bfs_helper(current.left, nodes_array) | ||
| bfs_helper(current.right, nodes_array) | ||
|
|
||
| return nodes_array | ||
| end | ||
|
|
||
|
|
||
| # couldn't get this to work in time :( | ||
| def delete(key) | ||
|
Comment on lines
+156
to
+157
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. It's hard. |
||
| return nil if @root.nil? | ||
|
|
||
| # do something if root is the node to be deleted | ||
|
|
||
|
|
||
| to_delete = find_node(@root, key) | ||
| parent_node = find_parent(@root, nil, key) | ||
|
|
||
| if to_delete.left && to_delete.right | ||
| find_replacement(@root, to_delete.key) | ||
| elsif to_delete.left | ||
| if parent_node.right == to_delete | ||
| parent_node.right = to_delete.left | ||
| elsif parent_node.left == to_delete | ||
| parent_node.left = to_delete.left | ||
| end | ||
| return | ||
| elsif to_delete.right | ||
| if parent_node.right == to_delete | ||
| parent_node.right = to_delete.right | ||
| elsif parent_node.left == to_delete | ||
| parent_node.left = to_delete.right | ||
| end | ||
| return | ||
| else | ||
| parent_node.left = nil | ||
| parent_node.right = nil | ||
| return | ||
| end | ||
| end | ||
|
|
||
| def find_node(current, key) | ||
| return current if current.key == key | ||
|
|
||
| if key < current.key | ||
| find_node(current.left, key) | ||
| else | ||
| find_node(current.right, key) | ||
| end | ||
| end | ||
|
|
||
| def find_parent(current, parent, key) | ||
| return parent if current.key == key | ||
|
|
||
| if key < current.key | ||
| find_parent(current.left, current, key) | ||
| else | ||
| find_parent(current.right, current, key) | ||
| end | ||
| end | ||
|
|
||
| def find_replacement(current, to_delete_key, closest_lower_node = nil) | ||
| return closest_lower_node if current.nil? | ||
| end | ||
|
|
||
|
|
||
| # Useful for printing | ||
| def to_s | ||
|
|
||
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.
👍