
Concept explainers
Make the BinaryTree class generic so that it uses a generic type for value rather than String. Override the toString() method to return the toString() value of its value. Implement the _inOrder(), _preOrder(), and _postOrder() methods so they are recursive.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
/*
* Expected output:
*
* [4, 2, 5, 1, 3]
* [1, 2, 4, 5, 3]
* [4, 5, 2, 3, 1]
*/
public static void main(String[] args) {
BinaryTree<String> one = new BinaryTree<>("1");
BinaryTree<String> two = new BinaryTree<>("2");
BinaryTree<String> three = new BinaryTree<>("3");
BinaryTree<String> four = new BinaryTree<>("4");
BinaryTree<String> five = new BinaryTree<>("5");
one.setLeft(two);
two.setLeft(four);
two.setRight(five);
one.setRight(three);
List<String> values = one.inOrder();
System.out.println(values);
values = one.preOrder();
System.out.println(values);
values = one.postOrder();
System.out.println(values);
}
}
_____________________________________
import java.util.ArrayList;
import java.util.List;
public class BinaryTree {
private String value;
private BinaryTree left;
private BinaryTree right;
public BinaryTree(String value) {
this.value = value;
left = null;
right = null;
}
public BinaryTree(String value, BinaryTree left, BinaryTree right) {
this.value = value;
this.left = left;
this.right = right;
}
public BinaryTree getLeft() {
return left;
}
public void setLeft(BinaryTree left) {
this.left = left;
}
public BinaryTree getRight() {
return right;
}
public void setRight(BinaryTree right) {
this.right = right;
}
public String getValue() {
return value;
}
public void setVale(String value) {
this.value = value;
}
public List<String> inOrder() {
List<String> values = new ArrayList<String>();
_inOrder(this, values);
return values;
}
private void _inOrder(BinaryTree tree, List<String> values) {
}
public List<String> preOrder() {
List<String> values = new ArrayList<String>();
_preOrder(this, values);
return values;
}
private void _preOrder(BinaryTree tree, List<String> values) {
}
public List<String> postOrder() {
List<String> values = new ArrayList<String>();
_postOrder(this, values);
return values;
}
private void _postOrder(BinaryTree tree, List<String> values) {
}
}

Step by stepSolved in 4 steps with 2 images

- In python. Write a LinkedList class that has recursive implementations of the add and remove methods. It should also have recursive implementations of the contains, insert, and reverse methods. The reverse method should not change the data value each node holds - it must rearrange the order of the nodes in the linked list (by changing the next value each node holds). It should have a recursive method named to_plain_list that takes no parameters (unless they have default arguments) and returns a regular Python list that has the same values (from the data attribute of the Node objects), in the same order, as the current state of the linked list. The head data member of the LinkedList class must be private and have a get method defined (named get_head). It should return the first Node in the list (not the value inside it). As in the iterative LinkedList in the exploration, the data members of the Node class don't have to be private. The reason for that is because Node is a trivial class…arrow_forwardUse a SinglyLinked List to implement a Queuea. Define a Queue interface.b. Define a LinkedQueue class (Node class)..c. Define a Test class to test all methods please help me make a test class first code package LinkedQueue; import linkedstack.Node; public class SLinkedList<E> { private Node <E> head = null; private Node <E> tail = null; private int size = 0; public SLinkedList() { head = null; tail = null; size = 0; } public int getSize() { return size; } public boolean isEmpty() { return size == 0; } public E getFirst() { if(isEmpty()) { return null; } return head.getElement(); } public E getLast() { if(isEmpty()) { return null; } return tail.getElement(); } public void addFirst(E e) { Node<E> newest = new Node<>(e, null); newest.setNext(head); head = newest; if(getSize()==0) { tail = head; } size++; } public void addLast(E e) { Node<E> newest = new Node<>(e, null); if(isEmpty()) { head = newest; } else {…arrow_forwardUse a SinglyLinked List to implement a Queuea. Define a Queue interface.b. Define a LinkedQueue class (Node class)..c. Define a Test class to test all methods help me make a test class first code package LinkedQueue; import linkedstack.Node; public class SLinkedList<E> { private Node <E> head = null; private Node <E> tail = null; private int size = 0; public SLinkedList() { head = null; tail = null; size = 0; } public int getSize() { return size; } public boolean isEmpty() { return size == 0; } public E getFirst() { if(isEmpty()) { return null; } return head.getElement(); } public E getLast() { if(isEmpty()) { return null; } return tail.getElement(); } public void addFirst(E e) { Node<E> newest = new Node<>(e, null); newest.setNext(head); head = newest; if(getSize()==0) { tail = head; } size++; } public void addLast(E e) { Node<E> newest = new Node<>(e, null); if(isEmpty()) { head = newest; } else {…arrow_forward
- Describe the meaning of the essential methods add(x), deleteMin(), and size() that are supported by the priority queue interface.Implement those methods using a singly-linked list. Analyze the running time of the add(x) and deletMin() operations based on this implementationarrow_forwardGiven main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 import java.util.Scanner; public class SortedList { public static void main (String[] args) {Scanner scnr = new Scanner(System.in);IntList intList = new IntList();IntNode curNode;int num; num = scnr.nextInt(); while (num != -1) {// Insert into linked list in descending order curNode = new IntNode(num);intList.insertInDescendingOrder(curNode);num = scnr.nextInt();}intList.printIntList();}}arrow_forwardIn Java, Complete the incomplete method of ExpressionTree.java. The incomplete methods are private Node parsePrefix(Scanner input) // Required public void parseInfix(String string) // Optional public void parsePostfix(String string) // Optional Implementations of parseInfix and parsePostfix require use of a stack. Implementation of parsePrefix does not. Read all of the ExpressionTree.java file before starting your implementation, so that you see the helper methods that are provided and get an idea of the context for your implementation. Although not needed for your implementation, you should be sure you understand how the toStringPrefix, toStringInfix, and toStringPostfix methods work. Note: The main() method accepts a single String as its argument. The String should be a prefix, infix, or postfix mathematical expression with no characters other than operators (+, -, *, and /) and operands (single characters a-z). As written, the main() method calls parsePrefix() to create an…arrow_forward
- provided code: import java.util.Comparator;import java.util.List;import java.util.LinkedList;import java.util.Queue; /** * Your implementation of various divide & conquer sorting algorithms. */ public class Sorting { /** * Implement merge sort. * * It should be: * out-of-place * stable * not adaptive * * Have a worst case running time of: O(n log n) * And a best case running time of: O(n log n) * * You can create more arrays to run merge sort, but at the end, everything * should be merged back into the original T[] which was passed in. * * When splitting the array, if there is an odd number of elements, put the * extra data on the right side. * * Hint: You may need to create a helper method that merges two arrays * back into the original T[] array. If two data are equal when merging, * think about which subarray you should pull from first. * * You may assume that the passed in array and…arrow_forwardGiven main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInAscendingOrder() method that inserts a new IntNode into the IntList in ascending order. Ex. If the input is: 8 3 6 2 5 9 4 1 7 -1 the output is: 1 2 3 4 5 6 7 8 9 import java.util.Scanner; public class SortedList {public static void main (String[] args) {Scanner scnr = new Scanner(System.in);IntList intList = new IntList();IntNode curNode;int num;num = scnr.nextInt();// Read in until -1while (num != -1) {// Insert into linked listcurNode = new IntNode(num);intList.insertInAscendingOrder(curNode);num = scnr.nextInt();}intList.printIntList();}}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





