Concept explainers
Explanation of Solution
Step 1: Define a static comparator class “DecOrder”.
Step 2: Inside that function declare a method “compare” to compare two strings.
Step 3: Inside the “compare” method, check if the length of string “s” is equal to the length of the string “t”. If it is true, then return “s.compareTo (t)”.
Step 4: If it is not true, then return “s.length () – t.length()”.
Function to count the number of leaf in a binary tree:
//Define the static comparator class
static class DecOrder implements Comparator<String>
{
//Define a method named "compare"
public int compare(String s, String t)
{
//Check if the length of "s" is equal to length of "t"
if (s.length() == t.length())
//Return the value that results from "compareTo" operation
return s...
Want to see the full answer?
Check out a sample textbook solutionChapter 21 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- cpparrow_forwardJava Programming: Show the output and there must be no errors at all. Create a Parser class that must have a constructor that accepts your collection of Tokens. We will be treating the collection of tokens as a queue – taking off the front. It isn’t necessary to use a Java Queue, but you may. We will add three helper functions to parser. These should be private: matchAndRemove – accepts a token type. Looks at the next token in the collection: If the passed in token type matches the next token’s type, remove that token and return it. If the passed in token type DOES NOT match the next token’s type (or there are no more tokens) return null. expectEndsOfLine – uses matchAndRemove to match and discard one or more ENDOFLINE tokens. Throw a SyntaxErrorException if no ENDOFLINE was found. peek – accepts an integer and looks ahead that many tokens and returns that token. Returns null if there aren’t enough tokens to fulfill the request. Create a public parse method. There are no parameters,…arrow_forwardIn classes we define the set of linked lists on the natural ones LN as the smallest set that meets the following rules: 1. Ø E LN. 2. Yes LE LN and kEN, then L→ k E LN. The operator of concatenation of lists, denoted by , receives two lists and returns the list that results from adding all the numbers from the second list to the end of the first. For instance: (→3 →4 → 10) °(→2 →6) =→3 →4 →10 →2 →6 We then define the operator •: Ln × LN → Ln inductively like: 1. L•Ø = L, with LE LN. 2. L•(L-→ k) = (L • L} → k, with L, L'E Ln. Notice that the concatenation operator is associative: (L1 • L2) • L3 = L1 •(L2 • L3). On the other hand, the operator back ()r: Ln → Ln receives a list and returns the list that results from reversing the order of its elements. For instance: (→3 →4 → 10 →7),=→7 →10 →4 →3 a) Inductively define the reverse operator. b) Show by structural induction that given two lists L1 and L2 It is true that (L1 • L2)r= Lr 2 ° Lr1arrow_forward
- Maze Runner Function - Implementation of this function is done in a1_partd.py We describe a maze as having row x col cells. For example if row was 3, and col was 4, then we would have a grid of cells as follows. We describe a wall by the two cell numbers the wall separates. If every single wall existed, there would be (row-1)(col) + (col-1)(row) walls. 0 | 1 | 2 | 3 4 | 5 | 6 | 7 8 | 9 | 10 | 11 A Maze class (which you do not need to implement) describes a maze as mentioned above. This class is defined in maze.py. It has methods that you can use to travel through the maze (i.e. figure out where you are, find a neighbour cell etc.) use a recursive maze runner function: def find_path(maze, from_cell, to_cell); The find_path function will find a path from cell number from_cell to cell number to_cell and will return it as a list containing all the cell numbers along the path, from the from_cell to the to_cell. You are allowed to use this function as a wrapper to a recursive function that…arrow_forwardThe union of two sets A and B is a set that contains all the elements that are in A together with all the elements that are in B. The intersection of A and B is the set that contains elements that are in both A and B. The difference of A and B is the set that contains all the elements of A except for those elements that are also in B.Suppose that A and B are variables of type set in Java. The mathematical operations on A and B can be computed using methods from the Set interface. In particular: A.addAll(B) computes the union of A and B; A.retainAll(B) computes the intersection of A and B; and A.removeAll(B) computes the difference of A and B. Write a program that can be used as a "set calculator" for simple operations on sets of non-negative integers. (Negative integers are not allowed.) A set of such integers will be represented as a list of integers, separated by commas and, optionally, spaces and enclosed in square brackets. For example: [1,2,3] or [17, 42, 9, 53,108]. The…arrow_forwardWrite a subroutine that takes a simple sentence in noun-verb-object formand parses the sentence into its different parts. For example, the sentence“Mary walked the dog” is parsed into this:Noun: MaryVerb: walkedObject: the dogThis function should work with both String objects and StringBuilderobjects.arrow_forward
- Computer sciencearrow_forwardagents: An agent is trying to eat all the food in a maze that contains obstacles, but he now has the help of his friends! An agent cannot occupy a square that has an obstacle. There are initially k pieces of food (represented by dots), at positions (f1,...,fk). There are also n agents at positions (p1,...,pn). Initially, all agents start at random locations in the maze. Consider a search problem in which all agents move simultaneously ;that is, in each step each agent moves into some adjacent position (N, S, E, or W, or STOP). Note that any number of agents may occupy the same position. Figure 1: A maze with 3 agents For each of the following heuristics, indicate (yes/no) whether or not it is h1: The number of dots (representing food) remaining. [ True, False ]. h2(s)=0, where s is a state node. [ True, False ]. h3(s)=1, where s is a state node. [ True, False ].arrow_forwardagents: An agent is trying to eat all the food in a maze that contains obstacles, but he now has the help of his friends! An agent cannot occupy a square that has an obstacle. There are initially k pieces of food (represented by dots), at positions (f1,...,fk). There are also n agents at positions (p1,...,pn). Initially, all agents start at random locations in the maze. Consider a search problem in which all agents move simultaneously ;that is, in each step each agent moves into some adjacent position (N, S, E, or W, or STOP). Note that any number of agents may occupy the same position. Give a search formulation to the problem of looking for both gold and diamond in a Knowing that you have M squares in the maze that do not have an What is the maximum size of the state space. What is the maximum branching For each of the following heuristics, indicate (yes/no) whether or not it is h1: The number of dots (representing food) remaining. [ True, False ]. h2(s)=0, where s is a…arrow_forward
- Edsgar Dijkstra is known for his work on programming languages. Hecame up with a neat problem that he called the Dutch National Flagproblem: given a list of strings, each of which is either 'red', 'green', or 'blue'(each is repeated several times in the list), rearrange the list so that thestrings are in the order of the Dutch national flag—all the 'red' stringsfirst, then all the 'green' strings, then all the 'blue' strings.Write a function called dutch_flag that takes a list and solves this problem.arrow_forwardConsider the instance variables and constructors. Given Instance Variables and Constructors: public class SimpleLinkedList<E> implements SimpleList<E>, Iterable<E>, Serializable { // First Node of the List private Node<E> first; // Last Node of the List private Node<E> last; // Number of List Elements private int count; // Total Number of Modifications (Add and Remove Calls) private int modCount; /** * Creates an empty SimpleLinkedList. */ publicSimpleLinkedList(){ first = null; last = null; count = 0; modCount = 0; } ... Assume the class contains the following methods that work correctly: public boolean isEmpty() public int size() public boolean add(E e) public E get(int index) private void validateIndex(int index, int end) Complete the following methods based on the given information from above. /** * Adds an element to the list at the…arrow_forwardJAVA PROGRAM SEE ATTACHED PHOTO FOR THE PROBLEMarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education