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

Faiza A. #17

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
88 changes: 72 additions & 16 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,101 @@ def initialize

# Time Complexity:

Choose a reason for hiding this comment

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

Time and space complexity?

# Space Complexity:
def add(key, value)
raise NotImplementedError
def add(key, value, current = @root)
new_node = TreeNode.new(key, value)

if @root == nil
@root = new_node
else
if key <= current.key

if current.left == nil
current.left = new_node
else
add(key, value, current.left)
end

else

if current.right == nil
current.right = new_node
else
add(key, value, current.right)
end

end
end
end

# Time Complexity:
# Space Complexity:
def find(key)
raise NotImplementedError
# Time Complexity:
# Space Complexity:
def find(key, current = @root)
if @root == nil
return nil
elsif key == current.key
return current.value
elsif key <= current.key
find(key, current.left)
else
find(key, current.right)
end
end

# Time Complexity:
# Space Complexity:
def inorder
raise NotImplementedError
def inorder(current = @root, array = [])
if current == nil
return array
else
inorder(current.left, array)
array << { key: current.key, value: current.value }
inorder(current.right, array)
end
end



# Time Complexity:
# Space Complexity:
def preorder
raise NotImplementedError
def preorder(current = @root, array = [])
if current == nil
return array
else
array << { key: current.key, value: current.value }
preorder(current.left, array)
preorder(current.right, array)
end
end

# Time Complexity:
# Space Complexity:
def postorder
raise NotImplementedError
def postorder(current = @root, array = [])
if current == nil
return array
else
postorder(current.left, array)
postorder(current.right, array)
array << { key: current.key, value: current.value }
end
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
def height(current = @root, array)

Choose a reason for hiding this comment

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

The height of a tree at any node will be the max height of the left subtree compared to the right subtree plus one.

Does that help?

Remember if a current is nil the height is 0.


end

# Optional Method
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
def bfs(current = @root, array = [])

Choose a reason for hiding this comment

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

For this you'll need a Queue, which we'll talk about in class a bit.

if current == nil
return array
else
bfs(current.left, array)
array << { key: current.key, value: current.value }
bfs(current.right, array)
end
end

# Useful for printing
Expand Down
4 changes: 3 additions & 1 deletion test/tree_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require_relative 'test_helper'

require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

Expand Down