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

Morgan - Leaves #34

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
Binary file added .DS_Store
Binary file not shown.
129 changes: 99 additions & 30 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,66 +1,135 @@
class TreeNode
attr_reader :key, :value
attr_accessor :left, :right

def initialize(key, val)
def initialize(key, val)
@key = key
@value = val
@left = nil
@right = nil
end
end
end

class Tree
attr_reader :root
def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log (n))
# Space Complexity: O(log (n))
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.

👍

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

# Time Complexity:
# Space Complexity:

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

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

return current_node
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def find(key)
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.

If the tree is balanced this is O(log n) for both time & space and O(n) if it's not.

raise NotImplementedError
helper_find(@root, key)
end

# Time Complexity:
# Space Complexity:

def helper_find(current_node, key)
return nil if current_node.nil?
return current_node.value if current_node.key == key

if key < current_node.key
helper_find(current_node.left, key)
else
helper_find(current_node.right, key)
end
end

# Time Complexity: o(n)
# Space Complexity: O(n)
def inorder
Comment on lines +55 to 57

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:

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

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: O(n)
# Space Complexity: O(n)
def preorder
Comment on lines +72 to 74

Choose a reason for hiding this comment

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

👍

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

# Time Complexity:
# Space Complexity:

def preorder_helper(current_node, list)
if current_node.nil?
return list
end

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

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

Choose a reason for hiding this comment

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

👍

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

# Time Complexity:
# Space Complexity:

def postorder_helper(current_node, list)
if current_node.nil?
return list
end

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

# Time Complexity: O(n)
# Space Complexity: O(n)
def height
Comment on lines +106 to 108

Choose a reason for hiding this comment

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

You're visiting each node so the time complexity is right, but the space complexity (for a balanced tree) is O(log n ) because your call stack will never get greater than the height of the tree.

raise NotImplementedError
return height_helper(@root, 0, 1)
end


def height_helper(current_node, max, count)
return max if current_node.nil?

if count > max
max = count
end

height_helper(current_node.left, max, count + 1)
height_helper(current_node.right, max, count + 1)
end

# Optional Method
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
end

# Useful for printing
def to_s
return "#{self.inorder}"
end
end

end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

require_relative "../lib/tree"