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 - Alice #44

Open
wants to merge 2 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
129 changes: 100 additions & 29 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,64 +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) for a balanced tree, O(n) for unbalanced
# Space Complexity: O(log n) for a balanced tree, O(n) for unbalanced
def add(key, value)
raise NotImplementedError
if @root.nil?
@root = TreeNode.new(key, value)
else
add_helper(@root, key, value)
end
end

# Time Complexity:
# Space Complexity:

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

# Time Complexity: O(log n) for a balanced tree, O(n) for unbalanced
# Space Complexity: O(log n) for a balanced tree, O(n) for unbalanced
def find(key)
raise NotImplementedError
return nil if @root.nil?

find_helper(@root, key)
end

# Time Complexity:
# Space Complexity:

def find_helper(current, key)
return nil if current.nil?

if key == current.key
return current.value
elsif key < current.key
find_helper(current.left, key)
else
find_helper(current.right, key)
end

end

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
raise NotImplementedError
inorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:

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)
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder
raise NotImplementedError
preorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:

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)
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder
raise NotImplementedError
postorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:

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}
end

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

height_helper(@root, 0)
end

def height_helper(current, height)
return height if current.nil?

height +=1
left = height_helper(current.left, height)
right = height_helper(current.right, height)

if left > right
return left
else
return right
end
end

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

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