Question

In: Computer Science

Recall that an array could be a convenient way of storing the elements of a Heap....

Recall that an array could be a convenient way of storing the elements of a Heap. Give a Pseudocode that determines whether an array H[1..N] is indeed a Heap, i.e., it’s elements satisfy the Heap property.

Solutions

Expert Solution

Array:

  • Array is the part of any programming language like c,c++,python etc.
  • It is define as"collection of similar data types elements".Which means it contain only one type as datatypes in which is declared. Like: int A[ ] (all elemnts are int type), char B[ ] , etc.
  • In array each element are identified by array index.Where is is denoted by A[1],A[1]......................A[n]
  • Array is stored in a way that the position of each element can be computed from its index value by a mathematical formula.
  • Formula: Address of A [ I ] = B + W * ( I – LB )

    B = Base address
    W = Storage Size of one element stored in the array (in byte)
    I = Subscript of element whose address is to be found  

    LB = Lower limit / Lower Bound of subscript, if not specified assume 0 (zero)​​​​

  • Array are used to hold one value at a time.
  • Array Representation:

Int A[6]={33,32,31,43,35,85}

1001   1004 1008 1012 1016 1020

33 32 31 43 35 85

A[0] A[1] A[2] A[3] A[4] A[5]

Where:1001 ,1004......................1020 = address or memory allocatrion

33,32......................85= Elemets of array

A[0] ,A[1]...................A[5] = Index value

Heap:

  • Tree structure is known as Heap.
  • A heap is a binary tree with keys at its nodes.
  • In heap the value of each node is less than or equal to the value of parent node.
  • In Heap the root of the binary tree(first elements) is the largest value or minimum value also it is knowm as max haep and min heap.
  • The parent at each node is ≥ at its children node.

An array could be a convenient way of storing the elements of a Heap in following way:

1. Sequance representation of heap:

  • Sequance representation of heap is same as like array representation.
  • In sequence reprentation ,If the root of any sub-tree at the position n , then it left most child will be at position ( 2n+1) , And right most child at (2n+2) and after that the parent node at the position (n-1)/2 where n≠0.
  • Sequance representation of heap:

1001 1004 1008 1012 1016 1020

33 32 31 43 35 85

A[0] A[1] A[2] A[3] A[4] A[5]

Where:1001 ,1004......................1020 = address or memory allocatrion.

33,32......................85= Elemets of array.

A[0] ,A[1]...................A[5] = Index value.

2. Array representation of Heap's:

  • Store heap’s elements in an array (whose elements indexed, for convenience, 1 to n) in top-down left-to-right order.
  • Example:

Properties of a Heap:

  • Heap’s elements are ordered top down, but they are not ordered left to right.
  • The root contains the largest value.
  • The subtree rooted at any node of a heap is also a heap.
  • A heap can be represented as an array.

Pseudocode that determines whether an array H[1..N] is indeed a Heap:

Here: H[1...n]= array which elemnts tends to 1 to n.

N=represent the number of elments in array H[1....n]

  1. k=2 to N (repeat 2 to 9) /* k= index value of array H[1....n] */
  2. n=k and T=H[k] /* T= temporary variabl */
  3. Root node = n/2 (repeat 5 to 8) /* n position of root node*/
  4. while (i > 1) AND (T >H [root] )
  5. H [ i ] = H [root]
  6. i = root , root=n/2 /* next root node*/  
  7. root > 1 then:
  8. root = 1 End step 4.
  9. H [ i ]= T End step 1. /* copy new element value*/
  10. Return.

Related Solutions

explain the way of storing dates
explain the way of storing dates
An extraction .............. is a convenient way to display each of the steps of a reactive...
An extraction .............. is a convenient way to display each of the steps of a reactive (or acid-base) extraction.
Suppose you have a min-heap in an array a[] where: ● The root isstored at...
Suppose you have a min-heap in an array a[] where: ● The root is stored at index 1 ● There are 15 elements (in a[1]...a[15]) ● There are no duplicates (this is not true in general for heaps; but assume it is true for this problem). Fill in the blanks in the statements below; the resulting statement must be true for any heap obeying the above properties.There are/is at least ____6_______ elements in theheap that are/is larger than a[3] ""There...
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
Related to HeapSort (a) Construct a heap for the following array of numbers:         8 1 2...
Related to HeapSort (a) Construct a heap for the following array of numbers:         8 1 2 3 5 6 4 7 10 9       Show the array after the insertion of each element into the heap. (b) Use your heap to sort the array. Show the resulting heap after the extraction of each maximum.
How to use computer system in a convenient way?? give me some ideas
How to use computer system in a convenient way?? give me some ideas
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements....
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements. Start by creating 2 arrays that can each hold 10 integer values. Then, get values from the user and populate the 1st array. Next, populate the 2nd array with the values from the 1st array in reverse order. Then, average the corresponding elements in the 1st and 2nd arrays to populate a 3rd array (average the 1st element of the 1st array with the...
We can build a heap by repeatedly calling the insert function to insert the elements into...
We can build a heap by repeatedly calling the insert function to insert the elements into the heap. Here is pseudocode: buildHeap(A) h = new empty heap   for each element e in A       h.insert(e)             What is the Big-O runtime of this version of buildHeap? Justify your answer.
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist in the former array. They need not necessarily be in the same order. For example, [1,7,3,4,2] overlaps [1,2,3] because 1,2 and 3 exist in [1,7,3,4,2]. To make the implementation easy, [1,7,3,4,2] overlaps [1,1,2,3] as well. We don’t need to check whether 1 appears twice in the first array. Write a program that lets the user enter two arrays and displays whether the first array...
Explain this code: The structure used is max heap using array. C++ (i is the index...
Explain this code: The structure used is max heap using array. C++ (i is the index of the element to be deleted) void del(int i) {    int left,right,temp;    arr[i]=arr[n-1];    n=n-1;    left=2*i+1; /*left child of i*/    right=2*i+2; /* right child of i*/    while(right < n)    {        if( arr[i]>=arr[left] && arr[i]>=arr[right] )            return;        if( arr[right]<=arr[left] )        {            temp=arr[i];            arr[i]=arr[left];   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT