-
Notifications
You must be signed in to change notification settings - Fork 44
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
Savannah #18
base: master
Are you sure you want to change the base?
Savannah #18
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,47 +16,121 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log(n)) where n is the number of nodes | ||
# Space Complexity: O(1) | ||
def add(key, value) | ||
raise NotImplementedError | ||
new_node = TreeNode.new(key, value) | ||
if !@root | ||
@root = new_node | ||
return | ||
end | ||
add_node(new_node, @root) | ||
|
||
end | ||
|
||
def add_node(new_node, curr) | ||
if curr.key >= new_node.key | ||
if !curr.left | ||
curr.left = new_node | ||
return | ||
end | ||
add_node(new_node, curr.left) | ||
else | ||
if !curr.right | ||
curr.right = new_node | ||
return | ||
end | ||
add_node(new_node, curr.right) | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
# Time Complexity: O(log(n)) where n is the number of nodes | ||
# Space Complexity: O(1) | ||
def find(key) | ||
raise NotImplementedError | ||
return if !@root | ||
curr = @root | ||
while curr | ||
if curr.key == key | ||
return curr.value | ||
elsif curr.key > key | ||
curr = curr.left | ||
else | ||
curr = curr.right | ||
end | ||
end | ||
return false | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) where n is the number of nodes | ||
# Space Complexity: O(n) "" | ||
def inorder | ||
raise NotImplementedError | ||
inorder_traversal(@root, []) | ||
end | ||
|
||
def inorder_traversal(node, node_list) | ||
return node_list if !node | ||
inorder_traversal(node.left, node_list) | ||
visit(node, node_list) | ||
inorder_traversal(node.right, node_list) | ||
end | ||
|
||
def visit(node, arr) | ||
arr.push({key: node.key, value: node.value}) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) where n is the number of nodes | ||
# Space Complexity: O(n) "" | ||
def preorder | ||
raise NotImplementedError | ||
preorder_traversal(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder_traversal(node, node_list) | ||
return node_list if !node | ||
visit(node, node_list) | ||
preorder_traversal(node.left, node_list) | ||
preorder_traversal(node.right, node_list) | ||
end | ||
|
||
# Time Complexity: O(n) where n is the number of nodes | ||
# Space Complexity: O(n) "" | ||
def postorder | ||
raise NotImplementedError | ||
postorder_traversal(@root, []) | ||
end | ||
|
||
def postorder_traversal(node, node_list) | ||
return node_list if !node | ||
postorder_traversal(node.left, node_list) | ||
postorder_traversal(node.right, node_list) | ||
visit(node, node_list) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) where n is the number of nodes | ||
# Space Complexity: O(n) | ||
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. The space complexity will be O(n) if the tree is unbalanced (due to the system stack) or O(log n) if the tree is balanced. |
||
def height | ||
raise NotImplementedError | ||
longest_tree_path(@root) | ||
end | ||
|
||
def longest_tree_path(node) | ||
return 0 if !node | ||
right = longest_tree_path(node.right) | ||
left = longest_tree_path(node.left) | ||
return [right, left].max + 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. clever! |
||
end | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) where n is the number of nodes | ||
# Space Complexity: O(n) "" | ||
def bfs | ||
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. Niiiice! |
||
raise NotImplementedError | ||
bfs_queue = [] | ||
curr = @root | ||
node_list = [] | ||
while curr | ||
visit(curr, node_list) | ||
bfs_queue << curr.left if curr.left | ||
bfs_queue << curr.right if curr.right | ||
curr = bfs_queue.shift | ||
end | ||
return node_list | ||
end | ||
|
||
# Useful for printing | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,13 +18,12 @@ | |
|
||
it "add & find values" do | ||
tree.add(5, "Peter") | ||
expect(tree.find(5)).must_equal "Peter" | ||
|
||
tree.add(15, "Ada") | ||
expect(tree.find(15)).must_equal "Ada" | ||
|
||
tree.add(3, "Paul") | ||
tree.add(15, "Ada") | ||
expect(tree.find(5)).must_equal "Peter" | ||
expect(tree.find(3)).must_equal "Paul" | ||
expect(tree.find(15)).must_equal "Ada" | ||
|
||
end | ||
|
||
it "can't find anything when the tree is empty" do | ||
|
@@ -69,6 +68,21 @@ | |
end | ||
end | ||
|
||
describe "hieght" do | ||
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, I will use these for C12, if that's ok. |
||
it "will return 0 if tree is empty" do | ||
expect(tree.height()).must_equal 0 | ||
end | ||
|
||
it "will return the nuber of nodes in the longest path" do | ||
expect(tree_with_nodes.height).must_equal 4 | ||
tree_with_nodes.add(60, "sam") | ||
tree_with_nodes.add(58, "penny") | ||
tree_with_nodes.add(65, "sam") | ||
expect(tree_with_nodes.height).must_equal 6 | ||
end | ||
|
||
end | ||
|
||
describe "breadth first search" do | ||
it "will give an empty array for an empty tree" do | ||
expect(tree.bfs).must_equal [] | ||
|
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.
Remember since you're doing this recursively, the system stack will allocate space for each recursive call.
So if the tree is balanced the space complexity is O(log n), if it's widely unbalanced O(n). The same also applies to time complexity.