Question

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 a heap, and false if it is not a heap. A leaf node is the root of a heap with only one node.

Note that the first method – which is a public wrapper method for the method that you will write – makes the initial call to your method with a value of 0 for i, which means that it will determine if the complete tree represented by the full array is a heap. You should not modify this wrapper method.

In implementing your recursive method, you may find it helpful to review:

  • the arithmetic rules for navigating through a complete tree that is stored in an array

  • how the compareTo() method can be used to compare two objects.

There is also a main() method with one unit test. You should add at least two other tests.

public class theHeap {

    public static <T extends Comparable<T>> boolean isHeap(T[] arr) {

        if (arr == null || arr.length == 0) {

            return false;

        }

        return isHeapTree(arr, 0);

    }

    private static <T extends Comparable<T>> boolean isHeapTree(T[] arr, int i) {

        // Implement this method.

    }

    public static void main(String[] args) {

        System.out.println("--- testing isHeapTree() ---");

        System.out.println();

        System.out.println("(0) an array of integers that is a heap");

        try {

            // Note that we need to use the wrapper class (Integer)

            // instead of the primitive type (int).

            Integer[] arr1 = { 50, 30, 35, 25, 23, 13, 18, 8, 20, 16 };

            boolean results = isHeap(arr1);

            System.out.println("actual results:");

            System.out.println(results);

            System.out.println("expected results:");

            System.out.println(true);

            System.out.print("MATCHES EXPECTED RESULTS?: ");

            System.out.println(results == true);

        } catch (Exception e) {

            System.out.println("INCORRECTLY THREW AN EXCEPTION: " + e);

        }

        System.out.println(); // include a blank line between tests

        /*

         * Add at least two additional unit tests. Follow the same format that we have

         * used above.

         */

    }

}

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

Answer:

Note:

  • The fourth test case requires an addition class. It performs the testcase based on the user defined class called Person.
  • So, before executing, please make sure the provided Person is also placed in the same folder as that of the theHeap class.
  • If not, the fourth test case and Person class can be removed.

Program code screen shot:

Add a comment
Know the answer?
Add Answer to:
Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods...
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 assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

  • I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\...

    I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\ package Tree; public class BinaryTree { private TreeNode root; // head of the list    //constructor - create an empty binary tree public BinaryTree() { root = null;    }    //isEmpty() - return true if tree is empty, false otherwise public boolean isEmpty() { return (root == null); }    //deleteTree() - remove all items from tree public void deleteList() { root =...

  • Return a method as an expression tree Hi guys. I need to return a method as...

    Return a method as an expression tree Hi guys. I need to return a method as an expression tree, it's currently returning null. public static ExpressionTree getExpressionTree(String expression) throws Exception {       char[] charArray = expression.toCharArray(); Node root = et.constructTree(charArray); System.out.println("infix expression is"); et.inorder(root); return et; } In the above method, et needs to have a value. -- Take a look at the complete class below. Kindly assist. ; public class ExpressionTree extends BinaryTree { private static final String DELIMITERS...

  • JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective:...

    JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective: javafx GUI program for BST Animation. BSTAnimation.java, AbstractTree.java and Tree.java. Modify the BSTAnimation.java  (1) Add Show Preoreder and Show Postorder button. Insert these two buttons next to the Show Inorder button to display tree in selected order. (2) Currently removing node method is to replace the removed node by the highest node on left-subtree. Modify the method by using lowest node on the right-subtree instead....

  • Complete HeapPriorityQueue (7 points). In lecture we implemented HeapPriorityQueue using an array-based representation of a heap...

    Complete HeapPriorityQueue (7 points). In lecture we implemented HeapPriorityQueue using an array-based representation of a heap (a complete binary tree whose entries satisfy the heap-order property). For this problem, complete the included HeapPriorityQueue class by using the LinkedBinaryTree class to represent a heap. Hint: the most challenging part of this problem is identifying the last Position in the heap and the next available Position in the heap. It is suggested that you review the array-based heap to better understand how...

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode...

    The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode root) { } public static void main(String[] args) { TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(2); TreeNode c = new TreeNode(3); a.left = b; a.right = c; System.out.println(isValidBST(a)); TreeNode d = new TreeNode(2); TreeNode e = new TreeNode(1); TreeNode f = new TreeNode(3); d.left = e; d.right = f; System.out.println(isValidBST(d)); } } TreeNode.java class TreeNode { int val; TreeNode left; TreeNode...

  • // CMPS 390 // MinHeap.java // Complete 4 methods: getMin, add, removeMin, reheap public class MinHeap...

    // CMPS 390 // MinHeap.java // Complete 4 methods: getMin, add, removeMin, reheap public class MinHeap { private Comparable a heap; // array of heap entries private static final int DEFAULT MAX SIZE = 100; private int lastIndex; // index of last entry public MinHeap() { heap = new Comparable (DEFAULT_MAX_SIZE]; lastIndex = 0; } // end default constructor public MinHeap (int maxSize) { heap = new Comparable (maxSize); lastIndex = 0; } // end constructor public MinHeap (Comparable[] entries)...

  • 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...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

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