Skip to content

Diana - Space #35

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 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
119 changes: 85 additions & 34 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,105 @@ class TreeNode
attr_reader :key, :value
attr_accessor :left, :right

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

class Tree
attr_reader :root
def initialize
@root = nil
def initialize(key, val)
@key = key
@value = val
@left = nil
@right = nil
end
end

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

# Time Complexity: worst case unbalanced-O(n), bal-O(log n)
# Space Complexity: worst case-O(n), bal-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.

👍

if @root.nil?
@root = TreeNode.new(key, value)
else
self.add_helper(@root, key, value)
end
end

# Time Complexity:
# Space Complexity:
def add(key, value)
raise NotImplementedError
end
def add_helper(node, new_key, new_value)
if new_key <= node.key
node.left.nil? ? node.left = TreeNode.new(new_key, new_value) : add_helper(node.left, new_key, new_value)
else
node.right.nil? ? node.right = TreeNode.new(new_key, new_value) : add_helper(node.right, new_key, new_value)
end
end

# Time Complexity: unbal-O(n), bal-O(log n)
# Space Complexity: unbal-O(n), bal-O(log n)
def find(key, node=@root)
Comment on lines +37 to +39

Choose a reason for hiding this comment

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

👍

return nil if @root.nil?

# Time Complexity:
# Space Complexity:
def find(key)
raise NotImplementedError
if key < node.key
find(key, node.left)
elsif key > node.key
find(key, node.right)
else
return node.value
end
end

# Time Complexity:
# Space Complexity:

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
Comment on lines +52 to 54

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(node, array)
return array if node.nil?

inorder_helper(node.left, array)
array << {:key=>node.key, :value=>node.value}
inorder_helper(node.right, array)

return array
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder
Comment on lines +68 to 70

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(node, array)
return [] if node.nil?

array << {:key=>node.key, :value=>node.value}
preorder_helper(node.left, array)
preorder_helper(node.right, array)

return array
end

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

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 height
raise NotImplementedError
def postorder_helper(node, array)
return [] if node.nil?

postorder_helper(node.left, array)
postorder_helper(node.right, array)
array << {:key=>node.key, :value=>node.value}

return array
end

# Time Complexity: O(n)
# Space Complexity: O(1)
def height(node=@root)
Comment on lines +100 to +102

Choose a reason for hiding this comment

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

👍 , very compact!

node.nil? ? 0 : (1 + [height(node.right), height(node.left)].max)
end

# Optional Method
Expand Down