Skip to content

Cedar - Jessica Carnes#18

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

Cedar - Jessica Carnes#18
Jessicawyn wants to merge 1 commit intoAda-C16:masterfrom
Jessicawyn:master

Conversation

@Jessicawyn
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? A heap has to be a complete binary tree where every level but the last is filled and the last is filled from left to right, a BST does not have completeness constraints. A heap is ordered as either a min-heap or a max-heap where the parent node is either smaller or larger than both the children. A BST on the other hand is ordered so that new nodes are placed on the left of the parent node if they are smaller or on the right if they are larger.
Could you build a heap with linked nodes? Yes, but it is easier with an array
Why is adding a node to a heap an O(log n) operation? Add a node has O(log n) time complexity because we can essentially ignore half of the tree. The new node is added as the last element in the array then it is compared to the parent index which is calculated as (i - 1) // 2, therefore we are essentially dropping half of the remaining elements from the list each time we compare up to find the right place for the new node.
Were the heap_up & heap_down methods useful? Why? Yes - the heap up method was useful for adding a value to the heap since the new value is added at the end of the array. Heap up then moved the new value up the heap until it found the proper spot. Similiarly, heap_down was useful for deleting items from the heap. To delete an item the last item in the array was switched with the first item which is then popped. Heap_down then traverses down the heap to find the proper spot for the value which was swapped to the root.

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 implementation Jessica! See comments below.

🟢

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.

@@ -22,16 +22,26 @@ def add(self, key, value = None):
Time Complexity: ?
Space Complexity: ?

Choose a reason for hiding this comment

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

✨ Time and space complexity?

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.

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

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.

✨ Nice iterative solution!

larger than either of its children and continues until
the heap property is reestablished.
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.

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