Skip to content

Time - Haben #25

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 3 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
150 changes: 127 additions & 23 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,66 +1,170 @@
# frozen_string_literal: true

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:
def print_root(node = @root)
if node.nil?
puts 'root is nil'
else
puts node.value
puts node.key
end
end


# Time Complexity:
# Space Complexity:
def add(key, value)
Comment on lines +32 to 34

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current_node = @root

@root = TreeNode.new(key, value) if current_node.nil?

until current_node.nil?
if (key < current_node.key) && current_node.left.nil?
current_node.left = TreeNode.new(key, value)
elsif (key > current_node.key) && current_node.right.nil?
current_node.right = TreeNode.new(key, value)
elsif key < current_node.key
current_node = current_node.left
elsif key > current_node.key
current_node = current_node.right
else
return
end
end
end

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

def find_recursive(current, key)
Comment on lines +54 to +57

Choose a reason for hiding this comment

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

Also a recursive version, nice 👍

return nil if current.nil?

return current.value if key == current.key

if key < current.key
find_recursive(current.left, key)
elsif key > current.key
find_recursive(current.right, key)
end
end

# Time Complexity:
# Space Complexity:
def find(key)
raise NotImplementedError
find_recursive(@root, key)
end

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

def inorder_recursion(current, traverse_array)
return if current.nil?

inorder_recursion(current.left, traverse_array)

traverse_array << { key: current.key, value: current.value }

inorder_recursion(current.right, traverse_array)
end

# Time Complexity:
# Space Complexity:
def inorder

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
traverse_array = []

inorder_recursion(@root, traverse_array)

traverse_array
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder_recursion(current, traverse_array)
return if current.nil?

traverse_array << { key: current.key, value: current.value }
preorder_recursion(current.left, traverse_array)
preorder_recursion(current.right, traverse_array)
end

# Time Complexity:
# Space Complexity:
def preorder

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
traverse_array = []

preorder_recursion(@root, traverse_array)

traverse_array
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder_recursion(current, traverse_array)
return if current.nil?

postorder_recursion(current.left, traverse_array)
postorder_recursion(current.right, traverse_array)
traverse_array << { key: current.key, value: current.value }
end

# Time Complexity:
# Space Complexity:
def postorder

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
traverse_array = []

postorder_recursion(@root, traverse_array)

traverse_array
end

# Time Complexity:
# Space Complexity:
# Time Complexity:
# Space Complexity:

def height
Comment on lines +130 to 133

Choose a reason for hiding this comment

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

⚠️ , Time complexity O(n) and space complexity O(h) where h is the height of the tree.

However I don't think this method works because it assumes the longest lengths of the trees are on the left and right edges, not somewhere in the middle.

An alternative solution which I think does work

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

      return [height_helper(current.left, count + 1), height_helper(current.left, count + 1)].max
    end

raise NotImplementedError
count_right = 0
count_left = 0
node = @root
return count_left if node.nil?

count_left = height_helper(node, 'left', count_left)
count_right = height_helper(node, 'right', count_right)
if count_right > count_left
count_right
else
count_left
end
end

def height_helper(node, side, count)
return count if node.nil?

count += 1
if side == 'left'
height_helper(node.left, 'left', count)
else
height_helper(node.right, 'right', count)
end
end

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

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