Need help with these two questions
String outputBreadthFirstSearch(): returns a string represenng a breadth first traversal. So, for example, for the tree shown in Figure 1, the method should output the string "bcaahttaetersse" 4. String outputDepthFirstSearch(): returns a string represenng a pre order depth first traversal. So, for example, for the tree shown in Figure 1, the method should output the string "batcathateersse
This is my code so far
public class Trie { final TrieNode root; public Trie() { this.root = new TrieNode(); } boolean add(String key) { TrieNode node = root; for (int i = 0; i < key.length(); i++) { int index = key.charAt(i); if (node.getChildren()[index] == null) { node.getChildren()[index] = new TrieNode(); } node = node.getChildren()[index]; } node.setIsComplete(true); return node.getIsComplete(); } boolean contains(String key) { TrieNode node = root; for(int i = 0; i < key.length(); i++){ int index = key.charAt(i)-'a'; if(node.getChildren()[index] == null) return false; node = node.getChildren()[index]; } return node.getIsComplete(); }

public class TrieNode { private char ch; private TrieNode[] children; private boolean isComplete; public TrieNode(){} public TrieNode(char ch) { this.ch = ch; this.children = new TrieNode[26]; this.isComplete = false; } public char getChar() { return ch; } public TrieNode[] getChildren() { return children; } public TrieNode findChildPosition(char c){ return children[c-'a']; } public boolean getIsComplete() { return isComplete; } void setIsComplete(boolean isComp) { this.isComplete = isComp; } }
The breadth-first search involves a search through a tree one level at a time. The code entered can be refined as follows:
Need help with these two questions String outputBreadthFirstSearch(): returns a string represenng a breadth first traversal....
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...
Please implement a right rotation funtion: private Node rightRotate(Node root) { } Remember to return the new root of the subtree to the parent so the parent can set it to be its child package trees; public class BinaryTree> { private Node root; //private int size; public static int sumTree(Node root) { if(root== null) { return 0; } int center = 0; if( root.item % 2 == 0) { center = root.item; } int left = sumTree(root.left); int right =...
Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children....
PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need help. import java.util.LinkedList; public class TwoDTree { private TwoDTreeNode root; private int size; public TwoDTree() { clear(); } /** * Returns true if a point is already in the tree. * Returns false otherwise. * * The traversal remains the same. Start at the root, if the tree * is not empty, and compare the x-coordinates of the point passed * in and...
Return a method as an expression tree
Hi guys.
I need to return a method as an expression tree, it's currently
returning null.
public static ExpressionTree getExpressionTree(String
expression)
throws Exception {
char[] charArray = expression.toCharArray();
Node root = et.constructTree(charArray);
System.out.println("infix expression is");
et.inorder(root);
return et;
}
In the above method, et needs to have a value.
-- Take a look at the complete class below.
Kindly assist.
; public class ExpressionTree extends BinaryTree { private static final String DELIMITERS...
package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values * * Complete each function below. * Write each function as a separate recursive definition (do not use more than one helper per function). * Depth of root==0. * Height of leaf==0. * Size of empty tree==0. * Height of empty tree=-1. * * TODO: complete the functions in this file. * DO NOT change the Node class. * DO NOT change the name...
1. Write a function in Tree class which returns true if and only if the tree satisfies the binary search tree property. The function’s header line is public boolean isValidBST() And in the attached code, you just need to finish the function after the comment: “//Instructor hint: please write your code here:” Make sure you execute your code, and the result in the main function after calling your function should be same as the prompt message I write. Clearly you...
Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children. Don’t worry if the tree is unbalanced. Note that...
i need help to modify this Tree class to include a method leavesCount that returns the number of leaves in a binary tree. (NOTE: Complete leavesCount and leavesCountHelper methods in the Tree class). I already have the test class. java class TreeNode< T extends Comparable< T > > { TreeNode< T > leftNode; T data; TreeNode< T > rightNode; public TreeNode( T nodeData ) { data = nodeData; leftNode = rightNode = null; } public void insert( T insertValue )...
Writing traversal methods You will create two files in the csc403 package: one called A3SimplerBST.java and the other TestA3.java. Details on these programs appears below. Copy the file SimplerBST.java from the week 1 examples package and rename the file and class A3SimplerBST. Add two public methods: one named twoChildrenCount that takes no parameters and that, when called, traverses every node of the tree to count how many nodes have two children. one named sameValueCount that takes a paremeter of generic...