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

Lynn - Edges #14

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

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n) must divide and conquer based on if new key is > || <
# Space Complexity: O(1)
def add(key, value)
raise NotImplementedError
new_node = TreeNode.new(key, value)

if @root.nil?
return @root = new_node
end

current = @root
add_helper(current, new_node)

end

# Time Complexity:
# Space Complexity:
def add_helper(root, add_node)

Choose a reason for hiding this comment

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

I like the helpers you created. Just a note, you could even put these helpers in the TreeNode class.

if root.nil?
return add_node
elsif add_node.key < root.key
root.left = add_helper(root.left, add_node)
elsif add_node.key > root.key
root.right = add_helper(root.right, add_node)
end
return root
end

# Time Complexity: O(log n) - a divide and conquer algorithm
# Space Complexity: O(1)

def find(key)
raise NotImplementedError
if @root.nil?
return nil
end

current = @root
return find_helper(current, key)
end

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

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
raise NotImplementedError
tree_inorder = []

if @root.nil?
return tree_inorder
end

current = @root
inorder_helper(current.left, tree_inorder)
tree_inorder << {:key => current.key, :value => current.value}
inorder_helper(current.right, tree_inorder)

return tree_inorder
end

# Time Complexity:
# Space Complexity:
def inorder_helper(current_node, tree_array)
tree_hash = {}
if current_node.nil?
return
end

inorder_helper(current_node.left, tree_array)
tree_hash[:key] = current_node.key
tree_hash[:value] = current_node.value
tree_array << tree_hash
inorder_helper(current_node.right, tree_array)
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder
raise NotImplementedError
tree_inorder = []
if @root.nil?
return tree_inorder
end

current = @root
tree_inorder << {:key => current.key, :value => current.value}
preorder_helper(current.left, tree_inorder)
preorder_helper(current.right, tree_inorder)

return tree_inorder
end

# Time Complexity:
# Space Complexity:
def preorder_helper(current_node, tree_array)
tree_hash = {}
if current_node.nil?
return
end

tree_hash[:key] = current_node.key
tree_hash[:value] = current_node.value
tree_array << tree_hash
preorder_helper(current_node.left, tree_array)
preorder_helper(current_node.right, tree_array)
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder
raise NotImplementedError
tree_postorder = []
if @root.nil?
return tree_postorder
end

current = @root
postorder_helper(current.left, tree_postorder)
postorder_helper(current.right, tree_postorder)
tree_postorder << {:key => current.key, :value => current.value}

return tree_postorder
end

# Time Complexity:
# Space Complexity:
def postorder_helper(current_node, tree_array)
tree_hash = {}
if current_node.nil?
return
end

postorder_helper(current_node.left, tree_array)
postorder_helper(current_node.right, tree_array)
tree_hash[:key] = current_node.key
tree_hash[:value] = current_node.value
tree_array << tree_hash
end

# Time Complexity: O(n)
# Space Complexity: O(1)
def height
raise NotImplementedError
if @root.nil?
return 0
end

current = @root

height_helper(current)
end

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

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

# Optional Method
Expand Down
22 changes: 11 additions & 11 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@
end
end

describe "breadth first search" do
it "will give an empty array for an empty tree" do
expect(tree.bfs).must_equal []
end

it "will return an array of a level-by-level output of the tree" do
expect(tree_with_nodes.bfs).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"},
{:key=>10, :value=>"Karla"}, {:key=>1, :value=>"Mary"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
end
# describe "breadth first search" do
# it "will give an empty array for an empty tree" do
# expect(tree.bfs).must_equal []
# end
#
# it "will return an array of a level-by-level output of the tree" do
# expect(tree_with_nodes.bfs).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"},
# {:key=>10, :value=>"Karla"}, {:key=>1, :value=>"Mary"},
# {:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
# end
# end
end