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

Water - Sophia #41

Open
wants to merge 1 commit 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
150 changes: 119 additions & 31 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,144 @@
class TreeNode
attr_reader :key, :value
attr_accessor :left, :right

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

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

# Time Complexity:
# Space Complexity:
# Time Complexity: Olog(n)
# Space Complexity: O(1)
def add(key, value)
raise NotImplementedError
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: Olog(n)
# Space Complexity: O(1)
def find(key)
raise NotImplementedError
return nil if @root.nil?
return @root.value if @root.key == key

current = @root
while current != nil
if key == current.key
return current.value
elsif key < current.key
current = current.left
elsif key > current.key
current = current.right
end
end
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
raise NotImplementedError
return [] if @root.nil?
list = []
stack = []
current = @root


while !current.nil? || !stack.empty?

while !current.nil?
stack << current
current = current.left
end

current = stack.pop
list << {key: current.key, value: current.value}
current = current.right
end

return list
end

# Time Complexity:
# Space Complexity:
def preorder
raise NotImplementedError

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder
return [] if @root.nil?
list = []
stack = []
current = @root

stack.push(current)
while !stack.empty?
current = stack.pop
list << {key: current.key, value: current.value}
stack << current.right if current.right
stack << current.left if current.left
end

return list
end

# Time Complexity:
# Space Complexity:
def postorder
raise NotImplementedError

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

list = []
s1 = []
s2 = []
current = @root

s1 << current
while !s1.empty?
current = s1.pop
s2 << current
s1 << current.left if current.left
s1 << current.right if current.right
end

while !s2.empty?
current = s2.pop
list << {key: current.key, value: current.value}
end

return list
end

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

def height_helper(current)
if current.nil?
return 0
else
return 1 + [height_helper(current.left), height_helper(current.right)].max
end
end


# Optional Method
# Time Complexity:
Expand All @@ -63,4 +151,4 @@ def bfs
def to_s
return "#{self.inorder}"
end
end
end