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
EBK STARTING OUT W/JAVA:...DATA...
- In this project, you will develop algorithms that find road routes through the bridges to travel between islands. The input is a text file containing data about the given map. Each file begins with the number of rows and columns in the map considered as maximum latitudes and maximum longitudes respectively on the map. The character "X" in the file represents the water that means if a cell contains "X" then the traveler is not allowed to occupy that cell as this car is not drivable on water. The character "0" in the file represents the road connected island. That means if a cell contains "0" then the traveler is allowed to occupy that cell as this car can drive on roads. The traveler starts at the island located at latitude = 0 and longitude = 0 (i.e., (0,0)) in the upper left comer, and the goal is to drive to the island located at (MaxLattitude-1, MaxLongitudes-1) in the lower right corner. A legal move from an island is to move left, right, up, or down to an immediately adjacent cell…arrow_forwardJava Code: Create a Parser class. Much like the Lexer, it has a constructor that accepts a LinkedList of Token and creates a TokenManager that is a private member. The next thing that we will build is a helper method – boolean AcceptSeperators(). One thing that is always tricky in parsing languages is that people can put empty lines anywhere they want in their code. Since the parser expects specific tokens in specific places, it happens frequently that we want to say, “there HAS to be a “;” or a new line, but there can be more than one”. That’s what this function does – it accepts any number of separators (newline or semi-colon) and returns true if it finds at least one. Create a Parse method that returns a ProgramNode. While there are more tokens in the TokenManager, it should loop calling two other methods – ParseFunction() and ParseAction(). If neither one is true, it should throw an exception. bool ParseFunction(ProgramNode) bool ParseAction(ProgramNode) -Creates ProgramNode,…arrow_forwardplease complete the following in JAVA Implement the graph ADT using the adjacency list structure. thanks! also posting a similar question for adjacency matrix. have a good day!arrow_forward
- Write a program that implements the algorithm delimiterMatching() seen in class.arrow_forward1. A polynomial can be represented as a linked list, where each node called a polyNode contains the coefficient and the exponent of a term of the polynomial. For example, the polynomial 4x³ + 3x² - 5 would be represented as the linked list: 43 3 4x3 I 2 % 3x² Write a Polynomial class that has methods for creating a polynomial, reading and writing a polynomial, and adding a pair of polymomials. -50 -5x° In order to add 2 polynomials, traverse both lists. If a particular exponent value is present in either one, it should also be present in the resulting polynomial unless its coefficient is zero.arrow_forwardRewrite the set of productions below in Extended Backus-Naur Form (EBNF). Set of Productions: P01: FN → FN–HEAD FN–BODY P02: FN–HEAD → TYPE id ( PARAM–LIST ) P03: TYPE → char P04: TYPE → int P05: TYPE → real P06: PARAM–LIST → TYPE id P07: PARAM–LIST → PARAM–LIST , TYPE id P08: FN–BODY → { VAR–DECL STMT return ( EXPRESN ) ; } P09: VAR–DECL → λ P10: VAR–DECL → TYPE ID–LIST ; P11: VAR–DECL → VAR–DECL TYPE ID–LIST ; P12: ID–LIST → id P13: ID–LIST → ID–LIST , id P14: STMT → λ P15: STMT → SIMPLE–STMT P16: STMT → SELECT–STMT P17: STMT → REPEAT–STMT P18: STMT → SEQUENCE–STMT P19: SIMPLE–STMT → ASSIGN–STMT P20: SIMPLE–STMT → FN–CALL–STMT P21: ASSIGN–STMT → var = EXPRESN ; P22: EXPRESN → ARITH–EXP P23: EXPRESN → BOOL–EXP P24: ARITH–EXP → TERM P25: ARITH–EXP → ARITH–EXP ADD–OP TERM P26: ADD–OP → + P27: ADD–OP → – P28: TERM → FAC P29: TERM → TERM MUL–OP FAC P30: MUL–OP → *…arrow_forward
- A uwuified sentence is sentence that has been transformed using a made-up Internet language in which some of the letters in the words are replaced by something else. The exact scheme is described below: Any uppercase/lowercase R or L is replaced by w/w, respectively. • If we encounter an o/o in a word, check if the previous letter (if it exists) is an M/m or N/n. If the previous letter is one of these, insert the lowercase letter y in between them, regardless of the capitalization of the other letters. • All other characters are left unchanged. Some examples: Professor will be converted to Pwofessow (There are two r's that are replaced by w's. Since the two o's aren't proceeded by an M/n or N/n, no y will be inserted.) LLunoacyo will be converted to wwunyoacyo (The two L's will be replaced with two ws according to the first rule. Then the first o will have a y inserted in front of it between then and the o according to the second rule. The last o won't have a y inserted in between…arrow_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_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_forward
- Research “Priority Queues with Comparators”. Write a program that uses a priority queue of string objects and gives them priority such that priority is assigned from shortest to longest string.arrow_forwardAssume we have an IntBST class, which implements a binary search tree of integers. The field of the class is a Node variable called root that refers to the root element of the tree. 1) Write a recursive method for this class that computes and returns the sum of all integers less than the root element. Assume the tree is not empty and there is at least one element less than the root. 2) Write a recursive method that prints all of the leaves, and only the leaves, of a binary search tree. 3) Write a method, using recursion or a loop, that returns the smallest element in the tree.arrow_forward2- Explain the Adjacency-list graph representation code public class Graph private final int V; private Bag[] adj; public Graph(int V) { this.V = V; adj = (Bag[]) new Bag[V]; for (int v = 0; v (); } public void addEdge(int v, int w) { adj[v].add(w); adj[w].add(v); public Iterable adj(int v) { return adj[v]; } }arrow_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