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

Rachael Water Class #37

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
113 changes: 106 additions & 7 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,139 @@ def initialize

# Time Complexity:
# Space Complexity:
def add(key, value)
raise NotImplementedError
def add(key, value = 0)
# there may or may not be a root
# if there's no root then the key/value will be the new root
# otherwise check to see if value is bigger or smaller than the root value
# initialize new node with the key and value
# check if it's smaller or larger and if it's the last node of the branch
# if it's the last node then add it after that node
input = TreeNode.new(key,value)
if @root == nil
@root = input
return
else here = @root
end
while here != nil
if input.key < here.key
if here.left != nil
here = here.left
else here.left = input

Choose a reason for hiding this comment

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

This works, just it's not standard coding style.

Suggested change
else here.left = input
else
here.left = input

return
end
else input.key > here.key
if here.right != nil
here = here.right
else here.right = input

Choose a reason for hiding this comment

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

Suggested change
else here.right = input
else
here.right = input

return
end
end
end
end

# Time Complexity:
# Space Complexity:
def find(key)
Comment on lines 51 to 53

Choose a reason for hiding this comment

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

👍 space/time complexity?

raise NotImplementedError
if @root.nil?
return nil
end
here = @root
while here != nil
if here.key == key
return here.value
elsif key < here.key
here = here.left
elsif key > here.key
here = here.right
end
end
return nil
end

# Time Complexity:
# Space Complexity:
def inorder
Comment on lines 70 to 72

Choose a reason for hiding this comment

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

👍 space/time complexity?

raise NotImplementedError
values = []
if @root.nil?
return values
end
return inorder_helper(@root, values)
end

def inorder_helper(here, values)
if here.nil?
return values
end

inorder_helper(here.left, values)
values.push({:key=>here.key, :value=>here.value})
inorder_helper(here.right, values)
return values
end


# Time Complexity:
# Space Complexity:
def preorder
Comment on lines 92 to 94

Choose a reason for hiding this comment

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

👍 space/time complexity?

raise NotImplementedError
values = []
if @root.nil?
return values
end
return preorder_helper(@root, values)
end

def preorder_helper(here, values)
if here.nil?
return values
end

values.push({:key=>here.key, :value=>here.value})
preorder_helper(here.left, values)
preorder_helper(here.right, values)

return values
end

# Time Complexity:
# Space Complexity:
def postorder
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.

👍 space/time complexity?

raise NotImplementedError
values = []
if @root.nil?
return values
end
return postorder_helper(@root, values)
end

def postorder_helper(here, values)
if here.nil?
return values
end

postorder_helper(here.left, values)
postorder_helper(here.right, values)
values.push({:key=>here.key, :value=>here.value})
return values
end

# Time Complexity:
# Space Complexity:
def height
Comment on lines 135 to 137

Choose a reason for hiding this comment

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

👍 space/time complexity?

raise NotImplementedError
height = 0
if @root.nil?
return height
end
return height_helper(@root)
end

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



# Optional Method
# Time Complexity:
# Space Complexity:
Expand Down