Skip to content

Liv - Space #28

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 5 commits 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
97 changes: 79 additions & 18 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,47 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: o(logn)
# Space Complexity: o(logn)
def add(key, value)
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.

👍 , the space and time complexities are correct assuming the tree is balanced.

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

# Time Complexity:
# Space Complexity:
# Time Complexity: o(logn)
# Space Complexity: o(logn)
def find(key)
Comment on lines +25 to 27

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @root
return nil if current.nil?

until current.key == key
(current.key > key) ? (current = current.left) : (current = current.right)
end

return current.value
end

# Time Complexity:
# Space Complexity:
# Time Complexity: on
# Space Complexity: on
def inorder
Comment on lines +38 to 40

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return inorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
# Time Complexity: on
# Space Complexity: on
def preorder
Comment on lines +44 to 46

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
preorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
# Time Complexity: on
# Space Complexity: on
def postorder
Comment on lines +50 to 52

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
postorder_helper(@root, [])
end

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

# Optional Method
Expand All @@ -63,4 +70,58 @@ def bfs
def to_s
return "#{self.inorder}"
end


##HELPERS

def add_helper(node, key, value)
return TreeNode.new(key, value) if node.nil?

if key < node.key
node.left = add_helper(node.left, key, value)
else
node.right = add_helper(node.right, key, value)
end

return node
end

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

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

return list
end

def preorder_helper(current, list)
return list if current.nil?

list << {key: current.key, value: current.value}
preorder_helper(current.left, list)
preorder_helper(current.right, list)

return list
end

def postorder_helper(current, list)
return list if current.nil?

postorder_helper(current.left, list)
postorder_helper(current.right, list)
list << {key: current.key, value: current.value}

return list
end

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

left = height_helper(current.left)
right = height_helper(current.right)

left > right ? (left + 1) : (right + 1)
end
end