Skip to content

Marjan-Pine#14

Open
Marjan-abm wants to merge 1 commit intoAda-C16:masterfrom
Marjan-abm:master
Open

Marjan-Pine#14
Marjan-abm wants to merge 1 commit intoAda-C16:masterfrom
Marjan-abm:master

Conversation

@Marjan-abm
Copy link
Copy Markdown

@Marjan-abm Marjan-abm commented May 12, 2022

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? :Heap has a faster insertion and deletion than binary search tree, Heap time complexity is O(log n) and BST is O(n)
Could you build a heap with linked nodes? : A heap can be implemented using a singly linked list
Why is adding a node to a heap an O(log n) operation? : The number of comparisons and swaps to move the new element to its correct position in worst case scenario would be one swap per level of the heap. since there log n levels to the heap, then adding a node is O(log n)
Were the heap_up & heap_down methods useful? Why? : They are useful in swapping and comparing between parent and child per level till the node ends up in its correct position

Copy link
Copy Markdown

@kyra-patton kyra-patton left a comment

Choose a reason for hiding this comment

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

✨💫 Nice work Marjan, you have a solid implementation. I left some comments below on complexity. I'm marking this as yellow since the comprehension questions are not filled out. You can resubmit with them filled out for a green! Let me know what questions you have.

🟡

Comment thread heaps/heap_sort.py
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n log n)
Space Complexity: O(n)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment thread heaps/min_heap.py
@@ -19,18 +19,30 @@ def __init__(self):
def add(self, key, value = None):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment thread heaps/min_heap.py
self.store.append(HeapNode(key,value))
self.heap_up(len(self.store)-1)

def remove(self):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✨ Space and time complexity?

Comment thread heaps/min_heap.py
@@ -44,10 +56,11 @@ def __str__(self):

def empty(self):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment thread heaps/min_heap.py
Time complexity: ?
Space complexity: ?
Time complexity: O(log n)
Space complexity: O(1)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✨ The recursive call stack here is going to make the space complexity O(log n) as each recursive call halves the heap

Comment thread heaps/min_heap.py
self.swap(index, parent_node)
self.heap_up(parent_node)

def heap_down(self, index):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

 ✨

Copy link
Copy Markdown

@kyra-patton kyra-patton left a comment

Choose a reason for hiding this comment

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

Thanks for updating. I would note that other big differences between heaps and BSTs are that heaps are semi-ordered and maintain a complete binary tree.

🟢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants