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

Fire - Alice D #20

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions .idea/tree-practice.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .rakeTasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Settings><!--This file was automatically generated by Ruby plugin.
You are allowed to:
1. Remove rake task
2. Add existing rake tasks
To add existing rake tasks automatically delete this file and reload the project.
--><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Run tests" fullCmd="test" taksId="test" /><RakeTask description="" fullCmd="default" taksId="default" /></RakeGroup></Settings>
122 changes: 103 additions & 19 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,133 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
def add(key, value)
raise NotImplementedError
# Time Complexity: O(log n); although if the tree is not balanced, this could be O(n)
# Space Complexity: O(1)
def add(key, value = nil)
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.

👍 Correct time complexity given the assumptions

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

current = @root

while key <= current.key || key > current.key
if key == current.key
"This key is already taken."
elsif key < current.key
if current.left.nil?
current.left = TreeNode.new(key,value)
return value
else
current = current.left
end
else
if current.right.nil?
current.right = TreeNode.new(key,value)
return value
else
current = current.right
end
end
end

end

# Time Complexity:

# Time Complexity: O(log n); although if the tree is not balanced, this could be O(n)
# Space Complexity:
def find(key)
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.

👍 Correct time complexity given the assumptions

space complexity ???

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

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
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
array = []
current = @root
inorder_helper(array, current)
end

# Time Complexity:
# Space Complexity:
def inorder_helper(array, current)
return array if current.nil?

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

end

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

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
array = []
current = @root
preorder_helper(array, current)
end

# Time Complexity:
# Space Complexity:
def preorder_helper(array, current)
return array if current.nil?
#middle
array << {:key => current.key, :value => current.value}
#left
preorder_helper(array, current.left)
#right
preorder_helper(array, current.right)

end

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

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
array = []
current = @root
postorder_helper(array, current)
end

# Time Complexity:
# Space Complexity:
def postorder_helper(array, current)
return array if current.nil?

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

end

# Time Complexity: O(log n); although if the tree is not balanced, this could be O(n)
# Space Complexity: O(1)
def height
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.

👍 the time complexity is O(n) and space complexity is O(log n) if the tree is balanced and O(n) otherwise.

raise NotImplementedError
height_helper(@root)
end

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

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


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