Why the sequence of data in a heap is not like what I thought
Question:
I’m learning the Heap data structure and found this link
I’m expecting the output of heapq.heapify([5, 7, 9, 1, 3])
to be
[1, 3, 9, 5, 7]
However, I saw it returns this:
[1, 3, 9, 7, 5]
As some suggested that the order on the same level does not matter, then I guess I just feel confused that how does the [5,7]
in the original sequence swapped position since the order in the same level does not matter.
Could someone explain why it is like this?
Answers:
Note that the Heap data structure only guarantees the top to be the minimum [or maximum]. It isn’t anything like a Binary Search Tree, which I gather is what you’re expecting with the order.
The invariant for a Binary Search Tree is that that for every node x
, all the keys in the left subtree should be smaller than x
and all the keys in the right subtree should be greater than x
.
However, in a Heap, the invariant is just that that every node x
should be greater [or smaller] than its children. Note how it doesn’t specify anything about its left or right subtree.
The sequence [1, 3, 9, 7, 5]
is a valid heap. Note how every parent is smaller than its children.
1
3 9
7 5
57913 # Starting list
Following the "heapify" procedure (for the min-heap):
As explained in this video, we iterate through the elements of our list one by one from the right, and each iteration check if the heap below it is a valid heap. If not – we swap the elements of this sub-heap until it becomes valid.
5
7 9
1 3
3: has no elements below it –> it’s a valid heap
1: has no elements below it –> it’s a valid heap
9: has no elements below it –> it’s a valid heap
7 > min(1, 3)
,
1 < 3 –> so choose 1
to swap with 7
5
1 9
7 3
5 > min(1, 9)
,
1 < 9 –> so choose 1
to swap with 5:
1
5 9
7 3
5 > min(3, 7)
,
3 < 7 –> so choose 3
to swap with 5
1
3 9
7 5
13975 # Result
I’m learning the Heap data structure and found this link
I’m expecting the output of heapq.heapify([5, 7, 9, 1, 3])
to be
[1, 3, 9, 5, 7]
However, I saw it returns this:
[1, 3, 9, 7, 5]
As some suggested that the order on the same level does not matter, then I guess I just feel confused that how does the [5,7]
in the original sequence swapped position since the order in the same level does not matter.
Could someone explain why it is like this?
Note that the Heap data structure only guarantees the top to be the minimum [or maximum]. It isn’t anything like a Binary Search Tree, which I gather is what you’re expecting with the order.
The invariant for a Binary Search Tree is that that for every node x
, all the keys in the left subtree should be smaller than x
and all the keys in the right subtree should be greater than x
.
However, in a Heap, the invariant is just that that every node x
should be greater [or smaller] than its children. Note how it doesn’t specify anything about its left or right subtree.
The sequence [1, 3, 9, 7, 5]
is a valid heap. Note how every parent is smaller than its children.
1
3 9
7 5
57913 # Starting list
Following the "heapify" procedure (for the min-heap):
As explained in this video, we iterate through the elements of our list one by one from the right, and each iteration check if the heap below it is a valid heap. If not – we swap the elements of this sub-heap until it becomes valid.
5
7 9
1 3
3: has no elements below it –> it’s a valid heap
1: has no elements below it –> it’s a valid heap
9: has no elements below it –> it’s a valid heap
7 > min(1, 3)
,
1 < 3 –> so choose 1
to swap with 7
5
1 9
7 3
5 > min(1, 9)
,
1 < 9 –> so choose 1
to swap with 5:
1
5 9
7 3
5 > min(3, 7)
,
3 < 7 –> so choose 3
to swap with 5
1
3 9
7 5
13975 # Result