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

Lisa Cee #3

Open
wants to merge 2 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
26 changes: 25 additions & 1 deletion algorithms/Algorithms_Answers.md
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
Add your answers to the Algorithms exercises here.
Add your answers to the Algorithms exercises here.
Exercise 1
a - Linear
b - O(log n)
c - O(sqroot(n))
d - O(n log n)
e - O(n ^ 3)
f - O(bunnies)
g - O(n)

Exercise 2
a -
minimum = a[0]
difference = 0
for i in range (1, len(a)):
minimum = min(minimum, a[i])
difference = max(difference, a[i] - minimum)
return difference
b - Binary Search

Exercise 3
a - first item = pivot
O n ^ 2
n pivots, n elements
b - O (n log n)
13 changes: 8 additions & 5 deletions data_structures/Data_Structures_Answers.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
Add your answers to the questions below.

1. What is the runtime complexity of your `depth_first_for_each` method?
Linear - depends on how many nodes in tree

2. What is the space complexity of your `depth_first_for_each` function?

Also linear - add item to stack for each node
3. What is the runtime complexity of your `breadth_first_for_each` method?

Linear
4. What is the space complexity of your `breadth_first_for_each` method?

Linear b/c queue
5. What is the runtime complexity of your `heapsort` function?

6. What is the space complexity of the `heapsort` function? Recall that your implementation should return a new array with the sorted data. What would be the space complexity if your function instead altered the input array?
O(n log n)
6. What is the space complexity of the `heapsort` function? Recall that your implementation should return a new array with the sorted data. What would be the space complexity if your function instead altered the input array?
Alter the input array? Constant
Our implementation? Linear
31 changes: 29 additions & 2 deletions data_structures/ex_1/binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,37 @@ def __init__(self, value):
self.right = None

def depth_first_for_each(self, cb):
pass
if self.value:
cb(self.value)
if self.left:
self.left.depth_first_for_each(cb)
if self.right:
self.right.depth_first_for_each(cb)

def breadth_first_for_each(self, cb):
pass
q = []
# add start node to queue
q.append(self)
# while queue, explore node at front and remove
while len(q) > 0:
current = q.pop(0)
#check level below, if left add to queue
if current.left:
q.append( current.left )
if current.right:
q.append(current.right)
cb(current.value)
# this_level = [self]
# while this_level:
# next_level = list()
# for item in this_level:
# cb(item.value)
# if item.left:
# next_level.append(item.left)
# if item.right:
# next_level.append(item.right)
# this_level = next_level
# pass

def insert(self, value):
new_tree = BinarySearchTree(value)
Expand Down
13 changes: 12 additions & 1 deletion data_structures/ex_2/heap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
def heapsort(arr):
pass
#make heap using arr
h = Heap()
for num in arr:
h.insert(num)
#build array to store sorted
sorted = [0] * len(arr)
#in max heap, largest element at root
for i in range(len(arr)):
# sorted[len(arr) - i - 1]
sorted[-i - 1] = h.delete()
#remove root from heap and insert into LHS sorted array
return sorted

class Heap:
def __init__(self):
Expand Down