Skip to content
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

Leaves - Kristina #29

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
96 changes: 87 additions & 9 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,118 @@ def initialize
@root = nil
end

def add_helper(curr_node, key, value)
if curr_node.nil?
return TreeNode.new(key, value)
else
if key < curr_node.key
curr_node.left = add_helper(curr_node.left, key, value)
else
curr_node.right = add_helper(curr_node.right, key, value)
end
end

return curr_node
end

# Time Complexity:
# Space Complexity:
def add(key, value)
raise NotImplementedError
def add(key, value)
Comment on lines 33 to +35

Choose a reason for hiding this comment

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

👍 , but the space and time complexity?

@root = add_helper(@root, key, value)
end

def find_helper(curr_node, key)
if curr_node.nil?
return nil
end

if curr_node.key == key
return curr_node.value
end

if curr_node.key > key
return find_helper(curr_node.left, key)
elsif curr_node.key < key
return find_helper(curr_node.right, key)
end

return nil
end

# Time Complexity:
# Space Complexity:
def find(key)
Comment on lines 57 to 59

Choose a reason for hiding this comment

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

👍 , but the space and time complexity?

raise NotImplementedError
return find_helper(@root, key)
end

def inorder_helper(current_node, list)
return list if current_node.nil?

inorder_helper(current_node.left, list)
list << {key: current_node.key, value: current_node.value}

inorder_helper(current_node.right, list)
return list
end

# Time Complexity:
# Space Complexity:
def inorder
raise NotImplementedError
def inorder(node,)
Comment on lines 73 to +75

Choose a reason for hiding this comment

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

👍 , but the space and time complexity?

Choose a reason for hiding this comment

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

Suggested change
def inorder(node,)
def inorder

list = []
inorder_helper(@root, list)
end

# Time Complexity:
# Space Complexity:
def preorder
raise NotImplementedError
def preorder_helper(current_node, list)
Comment on lines 80 to +82

Choose a reason for hiding this comment

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

👍 , but the space and time complexity?

return list if current_node.nil?
list << {key: current_node.key, value: current_node.value}

preorder_helper(current_node.left, list)
preorder_helper(current_node.right, list)

return list
end

def pre_order

Choose a reason for hiding this comment

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

Suggested change
def pre_order
def preorder

list = []
return preorder_helper(@root, list)
end

# Time Complexity:
# Space Complexity:
def postorder_helper(current_node, list)
Comment on lines 97 to +99

Choose a reason for hiding this comment

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

👍 , but the space and time complexity?

return [] if current_node.nil?

postorder_helper(current_node.left, list)
postorder_helper(current_node.right, list)
list << {key: current_node.key, value: current_node.value}
return list
end

def postorder
raise NotImplementedError
list = []
return postorder_helper(@root, list)
end

def height_helper(current_node)
return 0 if current_node.nil?

left_height = height_helper(current_node.left)
right_height = height_helper(current_node.right)

if left_height > right_height
return left_height + 1
else
return right_height + 1
end
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
height = 0
return height_helper(@root)
end

# Optional Method
Expand Down