Algorithm to insert element to a max-heap sequentially, Data Structure & Algorithms

Q. Write  down the  algorithm  to  insert  an  element  to  a  max-heap  which  is  represented sequentially.          

Ans:

The algorithm to insert an element "newkey" to a max heap of size i-1 where i>=1:

s=i;

parent=s div 2;

key[s]=newkey;

while(s<>0 && key[parent]<=key[s])

{

/*interchange parent and child*/

temp=key[parent]; key[parent]=key[s]; key[s]=temp;

s=parent;

parent=s div 2;

}

Posted Date: 7/10/2012 6:24:51 AM | Location : United States







Related Discussions:- Algorithm to insert element to a max-heap sequentially, Assignment Help, Ask Question on Algorithm to insert element to a max-heap sequentially, Get Answer, Expert's Help, Algorithm to insert element to a max-heap sequentially Discussions

Write discussion on Algorithm to insert element to a max-heap sequentially
Your posts are moderated
Related Questions
One of the best known methods for external sorting on tapes is the polyphase sort. Principle: The basic strategy of this sort is to distribute ordered initial runs of predetermi

You have to design a framework of a Genetic Algorithm (GA) with basic functionality. The basic functionality includes representation, recombination operators, tness function and se


Illustrates the program segment for Quick sort. It uses recursion. Program 1: Quick Sort Quicksort(A,m,n) int A[ ],m,n { int i, j, k; if m { i=m; j=n+1; k

1.Create a flowchart to show the process that will allow the implementation of Stack, Push, and Pop operations. 2.Create a flowchart to show the process that will allow the impleme

Handout 15 COMP 264: Introduction to Computer Systems (Section 001) Spring 2013 R. I. Greenberg Computer Science Department Loyola University Water TowerCampus, Lewis Towers 524 82

write aprogram for random -search to implement if a[i]=x;then terminate other wise continue the search by picking new randon inex into a

Determine in brief the Painter Algorithm a) The farthest polygon, namely the rectangle PQRS, is stored first. (b) The next farthest, the quadrilateral ABCD, is superpo

ALGORITHM (Insertion of element into a linked list) Step 1 Begin the program Step 2 if the list is empty or any new element comes before the start (head) element, then add t

what do you understand by structured programming?explain with eg. top down and bottem up programming technique