Skip to content

Conversation

Marjan-abm
Copy link

@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

@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.

🟡

Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n log n)
Space Complexity: O(n)

Choose a reason for hiding this comment

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

self.store = []


def add(self, key, value = None):

Choose a reason for hiding this comment

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

self.store.append(HeapNode(key,value))
self.heap_up(len(self.store)-1)

def remove(self):

Choose a reason for hiding this comment

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

✨ Space and time complexity?

return f"[{', '.join([str(element) for element in self.store])}]"


def empty(self):

Choose a reason for hiding this comment

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

Time complexity: ?
Space complexity: ?
Time complexity: O(log n)
Space complexity: O(1)

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

self.swap(index, parent_node)
self.heap_up(parent_node)

def heap_down(self, index):

Choose a reason for hiding this comment

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

 ✨

Copy link

@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