Question

In this question you are working with a max heap of integers, where the integer represents a priority, with larger integers b

java language
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Code Screenshot ;

1 import java.util.Arrays; 2 3 class Main 4 5 a 6 7 public static boolean isMaxHeap(int arr[], int n){ // Loop all nodes for(

Sample Output :

Run: Main C:\Program Files\Java\jdk-13\bin\java.exe [9, 8, 6, 5, 2, 1] : true 5 Process finished with exit code

Program Code to Copy

import java.util.Arrays;

class Main{

    public static boolean isMaxHeap(int arr[], int n){
        //Loop all nodes
        for(int i=0;i<n;i++){
            //Check if left value is gretaer
            if(2*i+1<n && arr[2*i+1]>arr[i]){
                return false;
            }
            //Check if right value is greater
            if(2*i+2<n && arr[2*i+2]>arr[i]){
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int heap[] = {9,8,6,5,2,1};
        System.out.println(Arrays.toString(heap)+" : "+isMaxHeap(heap,heap.length));
    }
}
Add a comment
Know the answer?
Add Answer to:
java language In this question you are working with a max heap of integers, where the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this question you are working with a max heap of integers, where the integer represents...

    In this question you are working with a max heap of integers, where the integer represents a priority, with larger integers being higher priority. Write a method that will test if a given heap array is correctly ordered (i.e. write a method that tests if an array is a valid heap). The method should accept (i) an array of integers (the heap), and (ii) the current number of items in the heap, and should return a boolean.

  • Write a java class, MaxHeap, to implement a max-heap of values of type double. Use an...

    Write a java class, MaxHeap, to implement a max-heap of values of type double. Use an array and be prepared to grow the array. The array implementation will probably be more efficient. Next, write three java sorting methods: a) One should be the heapsort algorithm. b) the second should sort the array by inserting all the elements from the array into a heap defined by the MaxHeap class, and then removing all the items from the heap and putting them...

  • Write a Java program, In this project, you are going to build a max-heap using array...

    Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...

  • Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the...

    Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the max-heap data structure using an integer array of 10 cells. (Do not use Java in-built PriorityQueue class.) [In a max-heap, the root node and the intermediate node vales are always greater than their children.] First, take 10 integer values from the user and insert them in the max-priority queue. Then print the elements of the queue. After that, delete two elements from the queue...

  • Task: A ternary heap is like a binary heap, except instead of each node having a max of two children, each node has a max of three children. Given a ternary min heap, return true if it is a valid hea...

    Task: A ternary heap is like a binary heap, except instead of each node having a max of two children, each node has a max of three children. Given a ternary min heap, return true if it is a valid heap. That is, it satisfies the heap property (the parent is less than all of its children) Requirements: Implement an isValidHeap function to determine whether a particular array of integers constitutes a valid ternary min-heap. bool isValidHeap(int arr[])// example declaration...

  • Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods...

    Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...

  • Using Java In this project, you are going to build a max-heap. You will use an...

    Using Java In this project, you are going to build a max-heap. You will use an array to implement the heap. Your program should: ? Allow the user to select one of the following two choices (Note that your program needs to implement both choices): o (1) test your program with 100 randomly generated integers (no duplicates, positive numbers with proper range); o (2) test your program with the following 100 fixed values from 1, 2, 3, ..., and 100....

  • Please finish all the questions,Thanks Following is Appendix assume the definition of Java class Heap given in the App...

    Please finish all the questions,Thanks Following is Appendix assume the definition of Java class Heap given in the Appendix For this question, a. (2 marks Consider the following heap 30 12 20 19 6 10 18 Given the array representation of a heap discussed in class, what is the array that corre sponds to this heap? b. (5 marks) Successively insert into the heap of part (a.) 22, 35 and 11, in that order. Use the standard heap insertion algorithm....

  • Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} ...

    Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} consisting of all strings that contain two consecutive c's and end with b.                   Your FSM program should include the following three static methods (Java) or functions (C):                                           a.    int nextState(int state, char symbol)                                  A state-transition function that returns the next state based on the                                  current state and an input symbol. This function should also return                                  -1 when an invalid input character is detected.                                  State...

  • 5. A three-heap with n elements can be stored in an array A, where A[O] contains...

    5. A three-heap with n elements can be stored in an array A, where A[O] contains the root of the tree. a) Draw the three-heap that results from inserting 5, 2, 8, 3, 6, 4, 9, 7, 1 in that order into an initially empty three-heap. You do not need to show the array representation of the heap. You are only required to show the final tree, although if you draw intermediate trees. b) Assuming that elements are placed in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT