Skip to content
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
14 changes: 8 additions & 6 deletions Data_Structures_Answers.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
Add your answers to the questions below.

1. What is the runtime complexity of your `depth_first_for_each` method?

O(n) , it runs through each node
2. What is the space complexity of your `depth_first_for_each` function?

O(n)
3. What is the runtime complexity of your `breadth_first_for_each` method?

O(n)
4. What is the space complexity of your `breadth_first_for_each` method?

O(n)

5. What is the runtime complexity of the provided code in `names.py`?
O(n^2) since it is looping through each element of each list to compare to each element of the other
Copy link

Choose a reason for hiding this comment

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

✔️


6. What is the space complexity of the provided code in `names.py`?

O(n)
7. What is the runtime complexity of your optimized code in `names.py`?

O(n)
8. What is the space complexity of your optimized code in `names.py`?
O(n)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Sprint Challenge: Data Structures

Hunter Smith
In this week's Sprint you implemented some classic and fundamental data structures and learned about how to go about evaluating their respective runtimes and performance. This Sprint Challenge aims to assess your comfort with these topics through exercises that build on the data structures you implemented and the algorithmic intuition you've started to build up.

## Instructions
Expand Down
12 changes: 8 additions & 4 deletions names/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
f.close()

duplicates = []
for name_1 in names_1:
for name_2 in names_2:
if name_1 == name_2:
duplicates.append(name_1)
# for name_1 in names_1:
# for name_2 in names_2:
# if name_1 == name_2:
# duplicates.append(name_1)
a_set = set(names_1)
b_set = set(names_2)
Copy link

Choose a reason for hiding this comment

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

nice you can also use intersection or a for loop with an if all of these would be O(n)

if (a_set & b_set):
duplicates.append(repr(a_set & b_set))

end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
Expand Down
19 changes: 17 additions & 2 deletions search/binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@ def __init__(self, value):
self.right = None

def depth_first_for_each(self, cb):
pass
cb(self.value)
if self.left != None:
self.left.depth_first_for_each(cb)
if self.right != None:
self.right.depth_first_for_each(cb)
return
Copy link

Choose a reason for hiding this comment

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

✔️



def breadth_first_for_each(self, cb):
pass
nodes = [self]
#while loop going through every node
while len(nodes) > 0:
current = nodes.pop(0)
cb(current.value)
if current.left != None:
nodes.append(current.left)
if current.right != None:
nodes.append(current.right)


def insert(self, value):
new_tree = BinarySearchTree(value)
Expand Down