Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7003637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:05:41+00:00 2026-05-27T21:05:41+00:00

I understand how to delete the root node from a max heap but is

  • 0

I understand how to delete the root node from a max heap but is the procedure for deleting a node from the middle to remove and replace the root repeatedly until the desired node is deleted?

  1. Is O(log n) the optimal complexity for this procedure?

  2. Does this affect the big O complexity since other nodes must be deleted in order to delete a specific node?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:05:42+00:00Added an answer on May 27, 2026 at 9:05 pm

    Actually, you can remove an item from the middle of a heap without trouble.

    The idea is to take the last item in the heap and, starting from the current position (i.e. the position that held the item you deleted), sift it up if the new item is greater than the parent of the old item. If it’s not greater than the parent, then sift it down.

    That’s the procedure for a max heap. For a min heap, of course, you’d reverse the greater and less cases.

    Finding an item in a heap is an O(n) operation, but if you already know where it is in the heap, removing it is O(log n).

    I published a heap-based priority queue for DevSource a few years back. The full source is at http://www.mischel.com/pubs/priqueue.zip

    Update

    Several have asked if it’s possible to move up after moving the last node in the heap to replace the deleted node. Consider this heap:

            1
        6       2
      7   8   3
    

    If you delete the node with value 7, the value 3 replaces it:

            1
        6       2
      3   8
    

    You now have to move it up to make a valid heap:

            1
        3       2
      6   8
    

    The key here is that if the item you’re replacing is in a different subtree than the last item in the heap, it’s possible that the replacement node will be smaller than the parent of the replaced node.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone help me understand what's wrong with this query: DELETE FROM noteproject INNER
I understand that delete returns memory to the heap that was allocated of the
I understand I can map a delete stored procedure to the delete method for
I am trying to figure out how to remove a node from a binary
I understand what System.WeakReference does, but what I can't seem to grasp is a
I understand the main function of the lock key word from MSDN lock Statement
I understand the difference between String and StringBuilder ( StringBuilder being mutable) but is
I understand that they are both supposed to be small, but what are the
In C++, I understand that the delete operator, when used with an array, 'destroys'
I don't understand why this code won't only delete the pizza_id with the given

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.