Skip to content

Time - Hannah T #38

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

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
125 changes: 102 additions & 23 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,62 +1,141 @@
require "pry"

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(1)
def add(key, value)
Comment on lines +22 to 24

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
new_node = TreeNode.new(key, value)
if !@root
@root = new_node
return
end
current = @root
while current != nil
if key < current.key
if current.left
current = current.left
else
current.left = new_node
return
end
else
if current.right
current = current.right
else
current.right = new_node
return
end
end
end
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(Log n)
# Space Complexity: O(1)
def find(key)
Comment on lines +50 to 52

Choose a reason for hiding this comment

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

👍

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

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

👍 , interesting choice to make an inner function. That's a very JavaScripty thing to do here.

raise NotImplementedError
ary = []

def innerfunc(node, ary)
return if node.nil?
innerfunc(node.left, ary)
ary << { :key => node.key, :value => node.value }
innerfunc(node.right, ary)
end

innerfunc(@root, ary)
return ary
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(Log n)
# Space Complexity: O(n)
def preorder
Comment on lines +82 to 84

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
ary = []

def innerfunc(node, ary)
return if node.nil?
ary << { :key => node.key, :value => node.value }
innerfunc(node.left, ary)
innerfunc(node.right, ary)
end

innerfunc(@root, ary)
return ary
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(Log n)
# Space Complexity: O(n)

def postorder
Comment on lines +98 to 101

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
ary = []
def innerfunc(node, ary)
return if node.nil?
innerfunc(node.left, ary)
innerfunc(node.right, ary)
ary << { :key => node.key, :value => node.value }
end

innerfunc(@root, ary)
return ary
end

# Time Complexity:
# Space Complexity:
# Time Complexity:
# Space Complexity:
def height
Comment on lines +114 to 116

Choose a reason for hiding this comment

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

👍 , time and space complexity?

raise NotImplementedError
def innerfunc(node)
return 0 if node.nil?
return 1 if !node.left && !node.right
return [innerfunc(node.left) + 1, innerfunc(node.right) + 1].max

Choose a reason for hiding this comment

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

Very slightly more compact

Suggested change
return [innerfunc(node.left) + 1, innerfunc(node.right) + 1].max
return [innerfunc(node.left), innerfunc(node.right)].max + 1

end

return innerfunc(@root)
end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity:
# Space Complexity:
def bfs
Comment on lines +127 to 129

Choose a reason for hiding this comment

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

Nice work with BFS. Time and space complexity?

raise NotImplementedError
q = [@root]
ary = []
while !q.empty?
current = q.pop
q << current.left if current.left
q << current.right if current.right
ary << { :key => current.key, :value => current.value }
end
return ary
end

# Useful for printing
Expand Down