Logo 
Search:

C++ Programming FAQ

Submit Interview FAQ
Home » Interview FAQ » C++ ProgrammingRSS Feeds

Write an algorithm for creating Heap in dfs (data file structure).

  Shared By: Vilhelm Fischer    Date: Sep 13    Category: C++ Programming    Views: 1290

Answer:

CREATE_HEAP(K,N)
[K is the array and N is the total number of elements in the array]

1. [Build Heap]

Repeat thru step 7 for
Q <-- 2,3,….N.

2. [Initialize construction phase]

I <-- Q
KEY <-- K[Q]

3. [Obtain parent of new record]

J <-- I/2

4. [Place new record in existing heap]

Repeat thru step 6 while I > 1 & KEY > K[J]

5. [Interchange record]

K[I] <-- K[J]

6. [Obtain next parent]

I <-- J
J <-- I/2

7. [Copy new record into its proper place]

K[I] <-- KEY

8. [FINISH]

return.

Share: 
 



Your Comment
  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].


Tagged: