public class Node {
public int data;
public Node next;
public Node(int d, Node n) {
data = d; next = n; } }
Please complete the following 5 tasks.
1./** * * @param start
* @param low * @param high * @return the sum of all values starting at start that are * in the range [low...high] (inclusive on both sides). 0 if none.
* start -> 10 -> 70 -> 20 -> 80 -> null * low = 20, high = 70: return 90 (20 + 70) */
public static int sumInRange(Node start, int low, int high) {
return 0; //to be completed }
2. /** * * @param start * @param target * @return true if the value target exists starting at start, false otherwise
* start -> 10 -> 70 -> 20 -> 80 -> null * target = 20: return true * target = 90: return false */
public static boolean contains(Node start, int target) {
return false; //to be completed }
3. /** * * @param start * @return true if all values starting at start are negative, false otherwise
* start -> -10 -> -70 -> -20 -> -80 -> null: return true * start -> -10 -> -70 -> -20 -> 80 -> null: return false
* start -> 0 -> -70 -> -20 -> -80 -> null: return false * start -> null: return true */
public static boolean allNegative(Node start) {
return false; //to be completed }
4. /** * * @param start * @param target * @return true if target exists exactly once in the chain starting at start, false otherwise
* start -> 10 -> 70 -> 20 -> 80 -> null, target = 10: return true * start -> 10 -> 70 -> 20 -> 80 -> null, target = 50: return false
* start -> 10 -> 70 -> 20 -> 70 -> null, target = 70: return false * start -> null, target = 10: return false */
public static boolean occursExactlyOnce(Node start, int target) {
return false; //to be completed }
5. /** * * @param start * @return a reference to the first node starting at start that holds a positive value,
* return null if no such node exists * start -> -3 -> 0 -> 8 -> -5 -> 0 -> 2 -> null:
* return node (n) such that n -> 8 -> -5 -> 0 -> 2 -> null * * start -> -3 -> 0 -> -5 -> null: return null */
public static Node firstPositiveNode(Node start) {
return null; //to be completed }
Here is the completed code for all 5 methods. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
/**
* * * @param start
*
* @param low
* @param high
* *
* @return the sum of all values starting at start that are * in the range
* [low...high] (inclusive on both sides). 0 if none.
*
* start -> 10 -> 70 -> 20 -> 80 -> null * low = 20, high = 70:
* return 90 (20 + 70)
*/
public static int sumInRange(Node start, int low, int high) {
// if start is null, returning 0 (base case)
if (start == null) {
return 0;
}
// if value of node start is between low and high, including start.data
// in the sum, else excluding it
if (start.data >= low && start.data <= high) {
// adding start.data with the value returned by calling sumInRange
// passing the next node and returning this value
return start.data + sumInRange(start.next, low, high);
} else {
// not including start.data and simply calling and returning
// sumInRange passing the next node
return sumInRange(start.next, low, high);
}
}
/**
*
*
* @param start
*
* @param target
*
* @return true if the value target exists starting at start, false
* otherwise
*
* start -> 10 -> 70 -> 20 -> 80 -> null * target = 20: return true
* * target = 90: return false
*/
public static boolean contains(Node start, int target) {
// if start is null, returning false (base case)
if (start == null) {
return false;
}
// if data of current node is target value, returning true
if (start.data == target) {
return true; // found
}
// otherwise, recursively moving to the next node and checking if it
// equals the target value
return contains(start.next, target);
}
/**
* @param start
* @return true if all values starting at start are negative, false
* otherwise
*
* start -> -10 -> -70 -> -20 -> -80 -> null: return true * start ->
* -10 -> -70 -> -20 -> 80 -> null: return false
*
* start -> 0 -> -70 -> -20 -> -80 -> null: return false * start ->
* null: return true
*/
public static boolean allNegative(Node start) {
// if start is null, returning true (base case)
if (start == null) {
return true;
}
// if data of current node is positive, returning false
if (start.data >= 0) {
return false;
}
// otherwise moving and checking next node
return allNegative(start.next);
}
/**
* * * @param start
*
* @param target
* @return true if target exists exactly once in the chain starting at
* start, false otherwise
*
* start -> 10 -> 70 -> 20 -> 80 -> null, target = 10: return true *
* start -> 10 -> 70 -> 20 -> 80 -> null, target = 50: return false
*
* start -> 10 -> 70 -> 20 -> 70 -> null, target = 70: return false
* * start -> null, target = 10: return false
*/
public static boolean occursExactlyOnce(Node start, int target) {
// if start is null, returning false (base case)
if (start == null) {
return false;
}
// checking if data of current node is target
if (start.data == target) {
// using contains method defined previously, checking if target
// value does not occur in the node chain starting from next
// position
if (!contains(start.next, target)) {
// nope! target occurs only once
return true;
} else {
// more than 1 occurrances found
return false;
}
}
// //if data of current node not equals target, moving to analyze next
// node
else {
return occursExactlyOnce(start.next, target);
}
}
/**
* @param start
* @return a reference to the first node starting at start that holds a
* positive value,
*
* return null if no such node exists * start -> -3 -> 0 -> 8 -> -5
* -> 0 -> 2 -> null:
*
* return node (n) such that n -> 8 -> -5 -> 0 -> 2 -> null * *
* start -> -3 -> 0 -> -5 -> null: return null
*/
public static Node firstPositiveNode(Node start) {
// if start is null, returning null (base case)
if (start == null) {
return null;
}
//if data of current node is positive, returning current node
if (start.data > 0) {
return start;
} else {
//moving to next node
return firstPositiveNode(start.next);
}
}
public class Node { public int data; public Node next; public Node(int d, Node n) {...
Here is the IntegerLinkedList_incomplete
class:
public class IntegerLinkedList {
static class Node {
/** The element stored at this node */
private int element; // reference to the element stored at this node
/** A reference to the subsequent node in the list */
private Node next; // reference to the subsequent node in the list
/**
* Creates a node with the given element and next node.
*
* @param e the element to be stored
* @param n...
/** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /** * Return whether k is in list. * Precondition: the elements of list are not null. * @param list the array to be searched. * @param k the number to search for. * @return true if k is an element of list, and false otherwise. */ public static boolean contains(Object[][] list, Object k) { return false; } /** * Create a String that...
Modify the LinkedCollection class to be a SortedLinkedCollecton class and see how that effects our implementation for adding and removing items. You should reference the SortedArrayCollection class provided for how these algorithms should be implemented. What needs to change here? Is it a lot of code or not much? Include a toString method that creates and returns a string that correctly represents the current collection. Include a test driver application that demonstrates your class correctly. //--------------------------------------------------------------------------- // LinkedCollection.java // //...
Hello, I've been working on this for a while and I can't figure the rest out. Need asap if anyone can help. maxBSt,minBST,isBST, and inOrder package lab7; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; public class TreeExercise { /* * Construct BST from preorder traversal */ public static Node<Integer> consBSTfromPreOrder(int[] arr, int start, int end) { if(start > end) return null; Node<Integer> root = new Node<Integer>(arr[start],...
Java Programming: The following is my code: public class KWSingleLinkedList<E> { public void setSize(int size) { this.size = size; } /** Reference to list head. */ private Node<E> head = null; /** The number of items in the list */ private int size = 0; /** Add an item to the front of the list. @param item The item to be added */ public void addFirst(E...
I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...
public class LinkedList { // The LinkedList Node class private class Node{ int data; Node next; Node(int gdata) { this.data = gdata; this.next = null; } } // The LinkedList fields Node head; // Constructor LinkedList(int gdata) { this.head = new Node(gdata); }...
Using the following implementation of Tree class Node { public int iData; // data item (key) public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() // display ourself { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...
Write a function which performs Breadth First Search on the
given graph. Also write in which order the nodes are visited.
Class Node {
public int value;
public Node[] neighbors;
public boolean visited;
public Node(int num) {
neighbors = new Node[5];
visited = False;
data = num
}
}
40 20 50 70 10 30 60
40 20 50 70 10 30 60
In the first exercise, you will override the add method in a subclass in order to improve its performance. In the second exercise, you will implement an Iterator for your new linked list class. Exercise 1 The add method of the linked list class discussed during lecture performs in O(N) time. What can be done to improve its performance to O(1)? What are the boundary cases? Define a new class that inherits from the CS20bLinkedList class introduced during the lecture....