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

Ren - Fire #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
137 changes: 118 additions & 19 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,140 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
def add(key, value)
raise NotImplementedError
# Time Complexity: O(log n) if balanced, otherwise O(n) where n is number of nodes
# Space Complexity: O(n), n is height
def add(key, value = nil)
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.

👍


new_node = TreeNode.new(key, value)

if @root.nil?
@root = new_node
else
add_helper(@root, new_node)
end
end

# Time Complexity:
# Space Complexity:
def add_helper(current, new_node)
return new_node if current.nil?

if new_node.key <= current.key
current.left = add_helper(current.left, new_node)
else
current.right = add_helper(current.right, new_node)
end

return current
end

# Time Complexity: O(log n) again if balanced
# Space Complexity: O(n) again
def find(key)
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
if @root.nil?
return @root
else
return find_helper(@root, key)
end
end

# Time Complexity:
# Space Complexity:
def find_helper(current, key)
if current.nil?
return nil
elsif key < current.key
return find_helper(current.left, key)
elsif key > current.key
return find_helper(current.right, key)
else
return current.value
end
end

# Time Complexity: O(n), n is number of nodes
# Space Complexity: O(n)
def inorder
Comment on lines +66 to 68

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
tree_node_arr = []
current = @root

return tree_node_arr if @root.nil?

inorder_helper(current, tree_node_arr)
end

# Time Complexity:
# Space Complexity:
def inorder_helper(current, arr)
# traverse the left subtree
# visit the current node
# traverse the right subtree

if current
inorder_helper(current.left, arr)
arr.push({key: current.key, value: current.value})
inorder_helper(current.right, arr)
end
return arr
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder
Comment on lines +90 to 92

Choose a reason for hiding this comment

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

👍

raise NotImplementedError

tree_node_arr = []
current = @root

return tree_node_arr if current.nil?

preorder_helper(current, tree_node_arr)
end

# Time Complexity:
# Space Complexity:
def preorder_helper(current, arr)
# visit the current node
# traverse the left subtree
# traverse the right subtree

if current
arr.push({key: current.key, value: current.value})
preorder_helper(current.left, arr)
preorder_helper(current.right, arr)
end
return arr
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder
Comment on lines +115 to 117

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
tree_node_arr = []
current = @root

return tree_node_arr if @root.nil?
postorder_helper(current, tree_node_arr)
end

# Time Complexity:
# Space Complexity:
def postorder_helper(current, arr)
# traverse the left subtree
# traverse the right subtree
# visit the current node

if current
postorder_helper(current.left, arr)
postorder_helper(current.right, arr)
arr.push({key: current.key, value: current.value})
end
return arr
end

# Time Complexity: O(n)
# Space Complexity: O(log n) if balanced
def height
Comment on lines +138 to 140

Choose a reason for hiding this comment

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

👍

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

height_helper(current)
end

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

return [(height_helper(current.left) + 1), (height_helper(current.right) + 1)].max
end

# Optional Method
# Time Complexity:
# Space Complexity:
Expand All @@ -64,3 +162,4 @@ def to_s
return "#{self.inorder}"
end
end

2 changes: 1 addition & 1 deletion test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
end

it "will report the height for a balanced tree" do
expect(tree_with_nodes.height).must_equal 3
expect(tree_with_nodes.height).must_equal 4
end

it "will report the height for unbalanced trees" do
Expand Down