Adapt the provided program to recreate the following tree:
import java.util.ArrayList;
import java.util.List;
public class TreeNode<T>{
public T data = null;
private List<TreeNode> children = new
ArrayList<>();
private TreeNode parent = null;
public TreeNode() {
}
public TreeNode(T data) {
this.data = data;
}
public TreeNode<T> addChild(TreeNode child) {
child.setParent(this);
this.children.add(child);
return child;
}
public TreeNode<T> addChild(T data) {
TreeNode<T> newChild = new TreeNode<>(data);
newChild.setParent(this);
children.add(newChild);
return newChild;
}
public void addChildren(List<TreeNode> children) {
for(TreeNode t : children) {
t.setParent(this);
}
this.children.addAll(children);
}
public List<TreeNode> getChildren() {
return children;
}
private void setParent(TreeNode parent) {
this.parent = parent;
}
public TreeNode getParent() {
return parent;
}
}
Here is the completed code for this problem. 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
Note: I have only added code to make the required tree that you mentioned without making any modifications to the existing code in TreeNode class. If you are looking to create a Binary search tree using TreeNode class, just let me know. I have done that before.
// TreeNode.java
import java.util.ArrayList;
import java.util.List;
public class TreeNode<T> {
public T data = null;
private List<TreeNode> children = new ArrayList<TreeNode>();
private TreeNode parent = null;
public TreeNode() {
}
public TreeNode(T data) {
this.data = data;
}
public TreeNode<T> addChild(TreeNode child) {
child.setParent(this);
this.children.add(child);
return child;
}
public TreeNode<T> addChild(T data) {
TreeNode<T> newChild = new TreeNode<T>(data);
newChild.setParent(this);
children.add(newChild);
return newChild;
}
public void addChildren(List<TreeNode> children) {
for (TreeNode t : children) {
t.setParent(this);
}
this.children.addAll(children);
}
public List<TreeNode> getChildren() {
return children;
}
private void setParent(TreeNode parent) {
this.parent = parent;
}
public TreeNode getParent() {
return parent;
}
// a simple method defined to print a tree recursively
static <T> void printTree(TreeNode<T> node, String indent) {
if (node != null) {
// printing data of node with indent spaces before it
System.out.println(indent + node.data);
// looping through each child of node
for (TreeNode<T> child : node.children) {
// calling recursive method to print child with one extra
// indentation level
printTree(child, indent + " ");
}
}
}
public static void main(String[] args) {
// creating all the TreeNode objects needed
TreeNode<Integer> root = new TreeNode<Integer>(10);
TreeNode<Integer> child1 = new TreeNode<Integer>(6);
TreeNode<Integer> child2 = new TreeNode<Integer>(18);
TreeNode<Integer> child11 = new TreeNode<Integer>(4);
TreeNode<Integer> child12 = new TreeNode<Integer>(8);
TreeNode<Integer> child21 = new TreeNode<Integer>(15);
TreeNode<Integer> child22 = new TreeNode<Integer>(21);
// linking nodes
root.addChild(child1);
root.addChild(child2);
child1.addChild(child11);
child1.addChild(child12);
child2.addChild(child21);
child2.addChild(child22);
// now root points to a tree that is mentioned in the question.
// you can do whatever you need with the tree now. Im just printing the
// tree.
// printing the tree using the helper method defined
printTree(root, "");
}
}
/*OUTPUT*/
Adapt the provided program to recreate the following tree: import java.util.ArrayList; import java.util.List; public class TreeNode<T>{ public T data = null; private List<TreeNode> childr...
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>...
BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> { private TreeNode<E> overallRoot; public BST() { overallRoot = null; } // ************ ADD ************ // public void add(E addThis) { if (overallRoot == null) { overallRoot = new TreeNode<>(addThis); } else { add(overallRoot, addThis); } } private TreeNode<E> add(TreeNode<E> node, E addThis) { if...
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
Does not pass the testcase testDTreeAdvancedReRootchangesParent Java I'm trying to extend the implementation of a general tree with new operations that change the structure of the tree. I've created 5 classes: Node.java, SimpleNode.java, Tree.java, RerootableTree.java and SimpleTree.java(which is the main java class that I need to change). The code does not pass ONE TESTCASE : testDTreeAdvancedReRootchangesParent The code passes all the other testcases except theone mentioned above. This is because in the SimpleTree.java the method "reRoot(Node newRoot)" is most likely...
Assessment O Submissions.. l import java.util.List; 2 import java.util.Arraylist; 4 public class ArrayHeapChecker 7Checks if the given array is a representation of a binary tree * @param entries 10 array of entries to be test * ereturn true if the input array encodes a binary tree, false otherwise 12 13 14 public static <K extends Comparable K, vs boolean isBinaryTree(List Entry<k,v entries) ( 15 16 17 // TODO: implement this return true 18 19 2e 21 * Checks if the...
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
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; }...
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");...
import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private ArrayList<String> words; private HashSet<String> foundWords = new HashSet<String>(); public FindWordInMaze(char[][] grid) { this.grid = grid; this.words = new ArrayList<>(); // add dictionary words words.add("START"); words.add("NOTE"); words.add("SAND"); words.add("STONED"); Collections.sort(words); } public void findWords() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { findWordsFromLocation(i, j); } } for(String w: foundWords) { System.out.println(w); } } private boolean isValidIndex(int i, int j, boolean visited[][]) { return...
Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Provides an implementation of a binary search tree * with no balance constraints, implemented with linked nodes. * * * */ public class Bst<T extends Comparable<T>> implements Iterable<T> { ////////////////////////////////////////////////////////////////// // I M P L E M E N T T H E M I N M E T H O D B E L O W...