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!
Assessment O Submissions.. l import java.util.List; 2 import java.util.Arraylist; 4 public class ArrayHeapChecker 7Checks if the...
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 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 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 { /** * @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 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) { 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 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 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 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 { /** * 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...