Question

Assessment O Submissions.. l import java.util.List; 2 import java.util.Arraylist; 4 public class ArrayHeapChecker 7Checks if the given array is a representation of a binary tree * @param entries 10 array of entries to be test * ereturn true if the input array encodes a binary tree, false otherwise 12 13 14 public static <K extends Comparable K, vs boolean isBinaryTree(List Entry<k,v entries) ( 15 16 17 // TODO: implement this return true 18 19 2e 21 * Checks if the given array is a complete binary tr + eparam entries 23 24 25 26 array of entries to be tested ereturn true if the input array encodes a complete binary tree, false otherwise os entries) 27 public static <k extends Comparablecki, vi boolean iscompletesinarytree(List Entryx, v if (!isBinaryTree (entries)) ( 28 29 return false; 31 /I TODO: implement this All changes saved Run Submit File: /home/ArrayHeapChecker java 1:1 Console Feedback Terminal
media%2F610%2F610a0d96-5093-4f7f-99a5-dc
media%2Fddc%2Fddca120f-97d4-499e-9297-cf
media%2F427%2F4271728c-a5cf-4198-9909-5a
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.List;

public class ArrayHeapChecker {

    public static <K extends Comparable<K>, V> boolean isBinaryTree(List<Entry<K, V>> entries) {
        boolean isBinaryTree = true;

        // check the node except the root node.. if there is just root node
        // it is always a binary tree
        if(entries.size() > 1) {
            for(int i = 1; i < entries.size(); i++) {
                if(entries.get(i) != null) {
                    int parentIndex = Math.floorDiv((i-1), 2);
                    if(entries.get(parentIndex) == null) {
                        return false;
                    }
                }
            }
        }

        return isBinaryTree;
    }

    public static <K extends Comparable<K>, V> boolean isCompleteBinaryTree(List<Entry<K, V>> entries) {
        if(!isBinaryTree(entries)) {
            return false;
        }

        if(entries.size() == 0)
            return true;

        int treeHeight = arrayBinaryTreeHeight(0, entries);

        if(treeHeight == 0)
            return true;

        int lowestLevelStart = (int) Math.pow(2, treeHeight) - 1;
        boolean allNull = true;
        boolean someNull = false;

        for(int i = 0; i < lowestLevelStart; i++){
            if(entries.get(i) == null)
                someNull = true;

            if(entries.get(i) != null)
                allNull = false;
        }

        if(!allNull && someNull)
            return false;


        someNull = false;
        for(int i = lowestLevelStart; i < entries.size(); i++){
            if(entries.get(i) == null)
                someNull = true;

            if(entries.get(i) != null && someNull)
                return false;
        }

        return true;
    }

    public static <K extends Comparable<K>, V> int arrayBinaryTreeHeight(int index, List<Entry<K, V>> entries) {
        if(index >= entries.size())
            return 0;
        if(entries.get(index) == null)
            return 0;

        Entry<K, V> left;
        Entry<K, V> right;

        if(index*2 + 1 >= entries.size() || index*2 + 2 >= entries.size()){
            left = right = null;
        } else {
            left = entries.get(index*2 + 1);
            right = entries.get(index*2 + 2);
        }

        if(left == null && right == null){
            return 0;
        }

        int h = 0;
        if(left != null)
            h = Math.max(h, arrayBinaryTreeHeight((index*2 + 1), entries));

        if(right != null)
            h = Math.max(h, arrayBinaryTreeHeight((index*2 + 2), entries));

        return 1 + h;
    }



    public static <K extends Comparable<K>, V> boolean isMinHeap(List<Entry<K, V>> entries) {
        if(!isCompleteBinaryTree(entries)) {
            return false;
        }
        return isMinHeap(entries, 0);
    }

    public static <K extends Comparable<K>, V> boolean isMinHeap(List<Entry<K, V>> entries, int index) {
        if(index >= entries.size())
            return true;

        Entry<K, V> parentVal = entries.get(index);
        int leftIndex = index*2 + 1;
        int rightIndex = index*2 + 2;

        if(rightIndex < entries.size()){
            Entry<K, V> leftVal = entries.get(leftIndex);
            Entry<K, V> rightVal = entries.get(rightIndex);

            if(leftVal != null && rightVal != null){
                if ((leftVal.getKey().compareTo(parentVal.getKey()) < 0) || (rightVal.getKey().compareTo(parentVal.getKey()) < 0)) {
                    return false;
                } else {
                    return isMinHeap(entries, leftIndex) && isMinHeap(entries, rightIndex);
                }
            }

            if(leftVal != null){
                return true && isMinHeap(entries, leftIndex);
            } else {
                return true;
            }
        } else {
            return true;
        }
    }

    public static class Entry<K extends Comparable<K>, V> {
        private K key;
        private V value;

        public Entry(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public K getKey() {
            return key;
        }

        public void setKey(K key) {
            this.key = key;
        }

        public V getValue() {
            return value;
        }

        public void setValue(V value) {
            this.value = value;
        }
    }

    public static void main(String args[]) {

    }
}

============

Hi. please find the answer above.. i have given comments so that it is very easy for you to understand the flow. In case of any doubts, please ask in comments. If the answer helps you, please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
Assessment O Submissions.. l import java.util.List; 2 import java.util.Arraylist; 4 public class ArrayHeapChecker 7Checks if 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
  • complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

    complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */    public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null;...

  • complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

    complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */ public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null; //TODO...

  • What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class...

    What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test implements Comparable<Test> private String[] cast; public Test( String[] st) cast = st; public int compareTo( Test t) if ( cast.length >t.cast.length ) t return +1; else if cast.length < t.cast.length return -1; else return 0; public static void main( String[] args String[] a"Peter", "Paul", "Mary" String[] b_ { "Мое", ''Larry", "Curly", String [ ] c = { ·Mickey", "Donald" }; "Shemp" }; List<Test>...

  • /** * */ package groceries; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ShoppingList {   ...

    /** * */ package groceries; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ShoppingList {    /**    * @param args    */    public static void main(String[] args) {        List groceryList = new ArrayList<>();        groceryList.add("rice");        groceryList.add("chicken");        groceryList.add("cumin");        groceryList.add("tomato");        groceryList.add("cilantro");        groceryList.add("lime juice");        groceryList.add("peppers");               groceryList.remove("cilantro");               for (int i = 0; i            if (groceryList.get(i).equals("peppers")) {...

  • import java.util.ArrayList; import java.util.List; public abstract class AbstractBoxPacking { protected List input; protected int boxSize; public...

    import java.util.ArrayList; import java.util.List; public abstract class AbstractBoxPacking { protected List input; protected int boxSize; public AbstractBoxPacking(List input, int boxSize){ this.input = input; this.boxSize = boxSize; } public abstract int getOutput(); public List deepCopy(List boxes){ ArrayList copy = new ArrayList(); for(int i = 0; i < boxes.size(); i++){ copy.add(boxes.get(i).deepCopy()); } return copy; } } I need Help fixing the errors in my java code

  • import java.util.ArrayList; import java.util.List; import java.util.Stack; public class Main { public static void main(String[] args) {...

    import java.util.ArrayList; import java.util.List; import java.util.Stack; public class Main { public static void main(String[] args) { int programCounter = 0; List<String> program = new ArrayList<String>(); Stack<String> stack = new Stack<String>(); // TODO string probably not best program.add("GOTO start<<1>>"); program.add("LABEL Read"); program.add("LINE -1"); program.add("FUNCTION Read -1 -1"); program.add("READ"); program.add("RETURN "); program.add("LABEL Write"); program.add("LINE -1"); program.add("FUNCTION Write -1 -1"); program.add("FORMAL dummyFormal 0"); program.add("LOAD 0 dummyFormal"); program.add("WRITE"); program.add("RETURN "); program.add("LABEL start<<1>>"); program.add("LINE 1"); program.add("FUNCTION main 1 4"); program.add("LIT 0 i"); program.add("LIT 0 j");...

  • Please help!!!!!! import java.util.Random; public class CSE205_Assignment04 { private static Random rnd = new Random(); public...

    Please help!!!!!! import java.util.Random; public class CSE205_Assignment04 { private static Random rnd = new Random(); public static void main(String[] args) { Tree_ctor_test(); Tree_insert1_test(); Tree_insert3_test(); Tree_insertMany1_test(); Tree_insertMany2_test(); Tree_insertMany3_test(); Tree_insertMany4_test(); } public static void Tree_ctor_test() { Tree<Integer> t = new BSTree<Integer>(); assertEqual("", t.toString(), "Tree_ctor_test: toString", false); assertEqual(false, t.contains(0), "Tree_ctor_test: contains", false); } public static void Tree_insert1_test() { Tree<Integer> t = new BSTree<Integer>(); t.insert(1); assertEqual("1 ", t.toString(), "Tree_insert1_test: toString", false); assertEqual(false, t.contains(0), "Tree_insert1_test: contains", false); assertEqual(true, t.contains(1), "Tree_insert1_test: contains", false); } public static...

  • Implement the missing methods in java! Thanks! import java.util.Iterator; import java.util.NoSuchElementException; public class HashTableOpenAddressing<K, V> implements...

    Implement the missing methods in java! Thanks! import java.util.Iterator; import java.util.NoSuchElementException; public class HashTableOpenAddressing<K, V> implements DictionaryInterface<K, V> { private int numEntries; private static final int DEFAULT_CAPACITY = 5; private static final int MAX_CAPACITY = 10000; private TableEntry<K, V>[] table; private double loadFactor; private static final double DEFAULT_LOAD_FACTOR = 0.75; public HashTableOpenAddressing() { this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR); } public HashTableOpenAddressing(int initialCapacity, double loadFactorIn) { numEntries = 0; if (loadFactorIn <= 0 || initialCapacity <= 0) { throw new IllegalArgumentException("Initial capacity and load...

  • import java.util.ArrayList; import java.util.Set; public class Project { /*    * Input is sorted (ascending) to...

    import java.util.ArrayList; import java.util.Set; public class Project { /*    * Input is sorted (ascending) to within k positions i.e. each value in the    * array is within k positions of where it should be.    * For example:    * k=1 input=[2, 1, 3, 4, 5]    * k=2 input=[3, 2, 1, 4, 5]    * k=3 input=[4, 2, 3, 1, 5]    * More than one element might be off by k! For example:    * k=2...

  • JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {    ...

    JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {     /**      * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList      * containing all the indexes of Strings in arr that match String s. For this method, two Strings,      * p and q, match when p.equals(q) returns true or if both of the compared references are null.      *      * @param s The string...

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