Menu

[Solved]Testjava Public Class Test Public Static Void Main String Args Tree Mytree New Tree Mytree Q37211753

Goal: This lab will teach you about tree operations Part I Download the lab6.jar file from Canvas. Extract the contents of la

The tree is rooted at node 5. Your implementation must have exactly the same result Requirements and Hints: The tree should b

Test.java

public class Test {

public static void main(String [] args) {
Tree<Integer> myTree = new Tree<Integer>();
myTree.insert(5);
myTree.insert(3);
myTree.insert(2);
myTree.insert(4);
myTree.insert(8);
myTree.insert(6);
myTree.insert(7);
myTree.insert(9);
  
System.out.println(myTree.treeBSearch(9));
System.out.println(myTree.treeBSearch(11));
System.out.println(myTree.nNodes());
  
System.out.println(“The Tree Looks Like: “);
myTree.print();
  
Tree<Integer> myTree2 = new Tree<Integer>();
myTree2.insert(4);
myTree2.insert(2);
myTree2.insert(1);
myTree2.insert(3);
  
myTree2.insert(7);
myTree2.insert(5);
  
// myTree2.print();
}
}

Tree.java

import java.lang.Math;

public class Tree<V> {
TreeNode<V> root;
int size;
  
// default constructor
// by default, the tree is empty
public Tree() {
root = null;
size = 0;
}

// search value x in tree rooted at node t
public boolean treeSearch(V x, TreeNode<V> t) {
  
if(t == null) return false;
if(t.getDatum() == x) return true;
return treeSearch(x, t.getLeft()) || treeSearch(x,t.getRight());
}
  
public boolean treeSearch(V x) {
return treeSearch(x, root);
}
  

// binary search value x in tree rooted at node t
public boolean treeBSearch(V x, TreeNode<V> t) {

// implement this method
return false;
}
  
public boolean treeBSearch(V x) {
return treeBSearch(x, root);
}
  
// check node t is leaf
public boolean isLeaf(TreeNode<V> t) {
//implement this method
return false;
}
  
// find the height of the tree rooted at node t
public int height(TreeNode<V> t) {
//implement this method
return false;

}
  
public int height() {
return height(root);
}
  
// find the number of nodes of tree rooted at t
public int nNodes(TreeNode<V> t) {
//implement this method
return 0;
}
  
public int nNodes() {
return nNodes(root);
}
  

// insert value x to the current tree object
public void insert(V x) {
// implement this method
}
  
private void setw(int indent) {
for (int i = 0; i < indent; i++) {
System.out.print(‘ ‘);
}
}

// print out the values of tree rooted at x
// it shows the hierarchy of the tree
// it will be useful for debugging
public void print(TreeNode<V> x, int indent) {
if(x == null) return;
if (x.getRight() != null) {
print(x.getRight(), indent + 4);
}
  
if (indent != 0) {
setw(indent);
}
  
if(x.getRight() != null){
System.out.print(” /n”);
setw(indent);
}
  
System.out.println(x.getDatum());

if (x.getLeft() != null) {
setw(indent);
System.out.print(” n”);
print(x.getLeft(), indent + 4);
}
}

public void print() {
int count = 0;
print(root, count);
}
  
};

TreeNode.java

public class TreeNode<T> {
T datum;
TreeNode<T> left, right;
  
// constructor with datum value, left and right are null
public TreeNode(T x) {
datum = x;
left = null;
right = null;
}
// constructor with datum value, left and right values
public TreeNode(T x, TreeNode<T> lft, TreeNode<T> rgt){
datum = x;
left = lft;
right = rgt;
}
  
// get datum value
public T getDatum() {
return datum;
}
  
// get left pointer
public TreeNode<T> getLeft() {
return left;
}
  
// get right pointer
public TreeNode<T> getRight() {
return right;
}
  
// set the left pointer
public void setLeft(TreeNode<T> p) {
left = p;
}
  
// set the right pointer
public void setRight(TreeNode<T> p) {
right = p;
}
  
};

Goal: This lab will teach you about tree operations Part I Download the lab6.jar file from Canvas. Extract the contents of lab6.jar to your working directory. There are three files in the contents. They are TreeNode.java, Tree.java, and Test.java. Do NOT modify anything in TreeNode.java and Test.java. Your work is to implement the methods in Tree.java. In the Tree.java file, please implement the following methods treeBSearch(Vx, TreeNod<V> t) isLeaf(TreeNode<V>t) . height(TreeNode<V> t) nNodes0 insert(Vx) After finishing the implementation, type the following command to compile the code java Test Class Test tests the implementations of the methods. The execution result is true false The Tree Looks Like: 2 The tree is rooted at node 5. Your implementation must have exactly the same result Requirements and Hints: The tree should be sorted, the values of left subtree are smaller than the root; the values of the right subtree are larger than the root When you insert a node, pretend that you are searching the value, insert the node to the place where it falls off the tree Show transcribed image text Goal: This lab will teach you about tree operations Part I Download the lab6.jar file from Canvas. Extract the contents of lab6.jar to your working directory. There are three files in the contents. They are TreeNode.java, Tree.java, and Test.java. Do NOT modify anything in TreeNode.java and Test.java. Your work is to implement the methods in Tree.java. In the Tree.java file, please implement the following methods treeBSearch(Vx, TreeNod t) isLeaf(TreeNodet) . height(TreeNode t) nNodes0 insert(Vx) After finishing the implementation, type the following command to compile the code java Test Class Test tests the implementations of the methods. The execution result is true false The Tree Looks Like: 2
The tree is rooted at node 5. Your implementation must have exactly the same result Requirements and Hints: The tree should be sorted, the values of left subtree are smaller than the root; the values of the right subtree are larger than the root When you insert a node, pretend that you are searching the value, insert the node to the place where it falls off the tree

Expert Answer


Answer to Test.java public class Test { public static void main(String [] args) { Tree myTree = new Tree(); myTree.insert(5); myT… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *