-
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
Rachael Water Class #37
base: master
Are you sure you want to change the base?
Changes from all commits
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,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 | ||||||||
return | ||||||||
end | ||||||||
else input.key > here.key | ||||||||
if here.right != nil | ||||||||
here = here.right | ||||||||
else here.right = input | ||||||||
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.
Suggested change
|
||||||||
return | ||||||||
end | ||||||||
end | ||||||||
end | ||||||||
end | ||||||||
|
||||||||
# Time Complexity: | ||||||||
# Space Complexity: | ||||||||
def find(key) | ||||||||
Comment on lines
51
to
53
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. 👍 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
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. 👍 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
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. 👍 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
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. 👍 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
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. 👍 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: | ||||||||
|
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.
This works, just it's not standard coding style.