-
Notifications
You must be signed in to change notification settings - Fork 44
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
base: master
Are you sure you want to change the base?
Faiza A. #17
Changes from 6 commits
a599aac
75492e8
1d18461
6cf24db
f085123
80a52d7
05a0892
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,45 +18,101 @@ def initialize | |
|
||
# Time 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
end | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
def bfs | ||
raise NotImplementedError | ||
def bfs(current = @root, array = []) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Time and space complexity?