Skip to content

Time - Jocelyn #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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 86 additions & 18 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,108 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(1)
def add_help(current, new_node)
Comment on lines +19 to +21

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 However since you are using recursion the space complexity is O(log n) if the tree is balanced (time as well) and O(n) if the tree is unbalanced.

return new_node if current.nil?

new_node.key <= current.key ?
current.left = add_help(current.left , new_node):
current.right = add_help(current.right, new_node)

return current
end

def add(key, value)
raise NotImplementedError
@root = add_help(@root, TreeNode.new(key,value))
end

# Time Complexity:
# Space Complexity:

# Time Complexity: log(n)
# Space Complexity: O(1)
def help_find(current, key)
Comment on lines +36 to +38

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 However since you are using recursion the space complexity is O(log n) if the tree is balanced (time as well) and O(n) if the tree is unbalanced.

return nil if current.nil?

if current.key == key
return current.value
elsif current.key < key
current = current.right
else
current = current.left
end

return help_find(current,key)
end

def find(key)
raise NotImplementedError
return help_find(@root, key)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(1)
def help_inorder(current)
Comment on lines +56 to +58

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 However since you are using recursion the space complexity is O(log n) if the tree is balanced and O(n) if the tree is unbalanced.

result = []
return [] if current.nil?
result += help_inorder(current.left)
result.push({:key=>current.key, :value => current.value})
result += help_inorder(current.right)
return result
end

def inorder
raise NotImplementedError
return help_inorder(@root)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: log(n)
# Space Complexity: O(1)
def help_preorder(current)
Comment on lines +71 to +73

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 However since you are using recursion the space complexity is O(log n) if the tree is balanced and O(n) if the tree is unbalanced.

result = []
return [] if current.nil?
result.push({:key=>current.key, :value => current.value})
result += help_preorder(current.left)
result += help_preorder(current.right)
return result
end

def preorder
raise NotImplementedError
return help_preorder(@root)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: log(n)
# Space Complexity: O(1)
def help_postorder(current)
Comment on lines +86 to +88

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 However since you are using recursion the space complexity is O(log n) if the tree is balanced and O(n) if the tree is unbalanced.

result = []
return [] if current.nil?
result += help_postorder(current.left)
result += help_postorder(current.right)
result.push({:key=>current.key, :value => current.value})
return result
end

def postorder
raise NotImplementedError
return help_postorder(@root)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: log(n)
# Space Complexity: O(1)
def height_helper(current)
num, left, right = 0, 0, 0
return 0 if current.nil?
left += height_helper(current.left)
right += height_helper(current.right)
num += 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just set num to 1 in line 104?


if left == right
return num + right
elsif left > right
return num + left
else
return num + right
end
end

def height
raise NotImplementedError
return height_helper(@root)
end

# Optional Method
Expand Down