Understanding Heaps & Deleting Elements

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Elements
In summary: Heaps are more commonly used for things like maintaining a sorted list or searching for an element in an array.
  • #1
evinda
Gold Member
MHB
3,836
0
Hi! (Smile)

A heap is a complete binary tree $T$. That means that a node of the tree has at most two children and for each nonempty subtree $t$ of $T$ it holds that the key of $t$ is greater than all the keys of the left subtree of $t$ and smaller than all the keys of the right subtree of $t$. Also, it holds that each node has exactly two children except from those from the last level. The nodes from the last level don't have children and the last level has to be filled from the left side without lacks but it hasn't to be complete.

The following is for example a heap, right? (Thinking)

View attachment 3737If we want to delete any element of the heap, do we have to put at its position the last element of the heap and then check if the key of the element we put is greater than the keys of the left subtree and smaller than the keys of the right subtree? (Thinking)
 

Attachments

  • heap2.png
    heap2.png
    6.9 KB · Views: 65
Technology news on Phys.org
  • #2
That's not what a heap is as far as I know, you're describing a complete binary search tree. In a heap each node has its children nodes both compare $\leq$ (or $\geq$) their parent node according to some order. For instance this is a heap:

640px-Max-Heap.svg.png


Note that the children of every node are smaller than their parent, but e.g. 19 is smaller than 25 despite being higher up the tree, that's not a problem because they are not in the same subtree.

The Wikipedia article for the binary heap is actually pretty good and even has a description of the insertion and deletion operations, I think it could help if you read through it: Binary heap - Wikipedia, the free encyclopedia.
 
  • #3
Bacterius said:
That's not what a heap is as far as I know, you're describing a complete binary search tree. In a heap each node has its children nodes both compare $\leq$ (or $\geq$) their parent node according to some order. For instance this is a heap:
Note that the children of every node are smaller than their parent, but e.g. 19 is smaller than 25 despite being higher up the tree, that's not a problem because they are not in the same subtree.

I see.. (Smile) At the beginning we want to find the element with a specific index $i$ in order to delete it. How can we get access to it? (Thinking)
 
  • #4
evinda said:
I see.. (Smile) At the beginning we want to find the element with a specific index $i$ in order to delete it. How can we get access to it? (Thinking)

The heap is generally stored as an array, so accessing an element by its index is just a constant-time lookup into the array. Then its parent and children indices can be found through simple arithmetic. It's written in the Wikipedia article, for what it's worth.
 
  • #5
Bacterius said:
The heap is generally stored as an array, so accessing an element by its index is just a constant-time lookup into the array. Then its parent and children indices can be found through simple arithmetic. It's written in the Wikipedia article, for what it's worth.

Suppose that we want to delete the element with index $i$.
Then at the beginning do we have to execute the command:
[m] A=A[n-1][/m]
where [m] n [/m] is the number of elements?
If so how can we delete the element of the $(n-1)^{th}$ position? (Thinking)
 
  • #6
evinda said:
Suppose that we want to delete the element with index $i$.
Then at the beginning do we have to execute the command:
[m] A=A[n-1][/m]
where [m] n [/m] is the number of elements?
If so how can we delete the element of the $(n-1)^{th}$ position? (Thinking)


If you simply delete the very last element of a heap, isn't the heap property still satisfied? Which nodes could possibly be affected? Are they?
 
  • #7
Bacterius said:
If you simply delete the very last element of a heap, isn't the heap property still satisfied? Which nodes could possibly be affected? Are they?

I haven't understood how we delete the very last element of the heap... (Worried)
 
  • #8
evinda said:
I haven't understood how we delete the very last element of the heap... (Worried)

You just remove it from the array and decrease the array's length by 1. Poof, gone. Removing it doesn't affect the heap property of the tree, so it's still a heap after deleting the last element (this isn't true of all nodes, though, of course) and so there's nothing more to be done for that part.

So to delete an arbitrary element (by index) you replace it with the last element of the heap, get rid of the last element of the heap, and then "heapify" the heap by fixing up the tree edges to restore the heap property, which may involve moving nodes around depending on their relative order.

By the way, deleting an arbitrary element is not generally what a heap is for, mainly because you don't often have indices to your heap elements to begin with (and searching in a heap takes linear time in general). Are you sure you really want a heap and not a binary search tree?
 
  • #9
Bacterius said:
You just remove it from the array and decrease the array's length by 1. Poof, gone. Removing it doesn't affect the heap property of the tree, so it's still a heap after deleting the last element (this isn't true of all nodes, though, of course) and so there's nothing more to be done for that part.

So to delete an arbitrary element (by index) you replace it with the last element of the heap, get rid of the last element of the heap, and then "heapify" the heap by fixing up the tree edges to restore the heap property, which may involve moving nodes around depending on their relative order.

Aha... (Nod)

Bacterius said:
By the way, deleting an arbitrary element is not generally what a heap is for, mainly because you don't often have indices to your heap elements to begin with (and searching in a heap takes linear time in general). Are you sure you really want a heap and not a binary search tree?

Yes, I am asked to delete any element of a heap in time $O(\log m)$.So, should the algorithm look like that? (Thinking)

Code:
Algorithm(A,i){
   A[i]=A[n];
   n=n-1;
   left=2*i;
   right=2*i+1;
   largest=i;
   if (left<=n and A[left]>A[largest]) largest=left;
   if (right<=n and A[right]>A[largest]) largest=right;
   if (largest!=i){
      swap(A[i],A[largest]);
      Algorithm(A,largest);
   }
}
 
  • #10
Or do we need also an other condition? (Thinking)
Because if we have for example this heap:

View attachment 3738

after deleting $50$ and heapifying we get this:

View attachment 3739then we swap $A[2]$ with $A[5]$ and then after some changes we get again the same heap as above... (Thinking)
Or does it have to do with the specific example? (Thinking)
 

Attachments

  • heap3.png
    heap3.png
    2.7 KB · Views: 67
  • heap3.png
    heap3.png
    2.6 KB · Views: 63
Last edited:

FAQ: Understanding Heaps & Deleting Elements

What is a heap?

A heap is a specialized tree-based data structure that satisfies the heap property. It is commonly used to implement priority queues, where the element with the highest priority is always at the root of the heap.

How do you insert elements into a heap?

To insert an element into a heap, it is first added to the bottom level of the heap at the leftmost open space. Then, it is compared with its parent node and swapped if it has a higher priority. This process is repeated until the element is in the correct place according to the heap property.

What is the time complexity for deleting an element from a heap?

The time complexity for deleting an element from a heap is O(log n), where n is the number of elements in the heap. This is because after the element is deleted, the heap needs to be restructured to maintain the heap property, which requires heapify operations that have a time complexity of O(log n).

How do you delete an element from a heap?

To delete an element from a heap, the element is first swapped with the last element in the heap. Then, the last element is deleted and the heap is restructured by comparing the swapped element with its children and swapping it with the higher priority child if necessary. This process is repeated until the heap property is satisfied.

Can you delete any element from a heap?

No, you cannot delete any element from a heap. The element must be located at the root of the heap in order to be deleted, as this maintains the heap property. If you want to delete a specific element from a heap, you must first search for it and then move it to the root before deleting it.

Similar threads

Replies
1
Views
1K
Replies
20
Views
4K
Replies
1
Views
2K
Replies
7
Views
2K
Replies
1
Views
1K
Replies
1
Views
2K
Back
Top