PlanetMath (more info)
 Math for the people, by the people.
Encyclopedia | Requests | Forums | Docs | Wiki | Random | RSS  
Login
create new user
name:
pass:
forget your password?
Main Menu
Owner confidence rating: High Entry average rating: No information on entry rating
heap insertion algorithm (Algorithm)

The heap insertion algorithm inserts a new value into a heap, maintaining the heap property. Let $ H$ be a heap, storing $ n$ elements over which the relation $ \preceq$ imposes a total ordering. Insertion of a value $ x$ consists of initially adding $ x$ to the bottom of the tree, and then sifting it upwards until the heap property is regained.

Sifting consists of comparing $ x$ to its parent $ y$. If $ x \preceq y$ holds, then the heap property is violated. If this is the case, $ x$ and $ y$ are swapped and the operation is repeated for the new parent of $ x$.

Since $ H$ is a balanced binary tree, it has a maximum depth of $ \lceil\log_2n\rceil+1$. Since the maximum number of times that the sift operation can occur is constrained by the depth of the tree, the worst case time complexity for heap insertion is $ \mathcal{O}(\log n)$. This means that a heap can be built from scratch to hold a multiset of $ n$ values in $ \mathcal{O}(n\log n)$ time.

What follows is the pseudocode for implementing a heap insertion. For the given pseudocode, we presume that the heap is actually represented implicitly in an array (see the binary tree entry for details).


\begin{program} \mathrm{HeapInsert}(H, n, \preceq, x) \text{{\bf Input}: A heap ... ... parent \gets parent \textrm{ div } 2 \ELSE parent \gets 0 \FI \OD \end{program}



Anyone with an account can edit this entry. Please help improve it!

"heap insertion algorithm" is owned by mps. [ full author list (4) | owner history (3) ]
(view preamble)

View style:

See Also: heap, heap removal algorithm, heapsort

Log in to rate this entry.
(view current ratings)

Cross-references: child, binary tree, multiset, time complexity, sift, number, depth, balanced binary tree, operation, parent, tree, total ordering, heap property, heap
There are 2 references to this entry.

This is version 6 of heap insertion algorithm, born on 2002-02-26, modified 2006-07-21.
Object id is 2715, canonical name is HeapInsertionAlgorithm.
Accessed 9569 times total.

Classification:
AMS MSC68P05 (Computer science :: Theory of data :: Data structures)
 68P10 (Computer science :: Theory of data :: Searching and sorting)
 68P20 (Computer science :: Theory of data :: Information storage and retrieval)

Pending Errata and Addenda
None.
[ View all 2 ]
Discussion
Style: Expand: Order:
forum policy

No messages.

Interact
post | correct | update request | add example | add (any)