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
SPARSE MATRICES Matrices along with good number of zero entries are called sparse matrices. Refer the following matrices of Figure (a)

What is a Binary Search Tree (BST)? A binary search tree B is a binary tree every node of which satisfies the three conditions: 1.  The value of the left-subtree of 'x' is le

State the example of pre- and post-conditions Suppose that function f(x) should have a non-zero argument and return a positive value. We can document these pre- and post-condit


Depth-first traversal A depth-first traversal of a tree visit a node and then recursively visits the subtrees of that node. Likewise, depth-first traversal of a graph visits

One of the main problems with the linear queue is the lack of appropriate utilization of space. Assume that the queue can store 100 elements & the complete queue is full. Thus, it

how to reduce the number of passes in selection sort

Create a Money data structure that is made up of amount and currency. (a) Write a constructor for this data structure (b) Create accessors for this data structure (c) Writ

Question 1 What is a data structure? Discuss briefly on types of data structures Question 2 Explain the insertion and deletion operation of linked list in detail Qu

What is bubble sort? Bubble Sort: The basic idea in bubble sort is to scan the array to be sorted sequentially various times. Every pass puts the largest element in its corr