Question

public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary...

public class Buildbst {

private int data;

private Buildbst left;

private Buildbst right;

//Set the binary search tree

public Buildbst(int data) {

this.data = data;

this.left = null;

this.right =null;

}

public int getData() {

return data;

}

public void setData(int data) {

this.data = data;

}

public Buildbst getLeft() {

return left;

}

public void setLeft(Buildbst left) {

this.left = left;

}

public Buildbst getRight() {

return right;

}

public void setRight(Buildbst right) {

this.right = right;

}

}

package Assignment5;

public class BinarySearchTree {

private Buildbst root;

public BinarySearchTree() {

this.root = null;

}

//Search

Buildbst search(Buildbst root, int data) {

if (root == null)

return null;

if (data < root.getData())

return search(root.getLeft(),data);

else if (data > root.getData())

return search(root.getRight(),data);

else

return root;

}

Buildbst insert(Buildbst root, int data) {

if (root == null)

root = new Buildbst(data);

else {

if (data < root.getData())

root.setLeft(insert(root.getLeft(),data));

else if (data > root.getData())

root.setRight(insert(root.getRight(),data));

}

return root;

}

//To find the smallest root

Buildbst findMin(Buildbst root) {

if (root == null)

return null;

else if (root.getLeft() == null)

return root;

else

return findMin(root.getLeft());

}

//delete the root that the user want

Buildbst delete(Buildbst root, int data) {

Buildbst temp;

if (root == null)

System.out.println("isempty");

else if (data < root.getData())

root.setLeft(delete(root.getLeft(),data));

else if (data > root.getData())

root.setRight(delete(root.getRight(),data));

else {

if (root.getLeft() != null && root.getRight() != null) {

temp = findMin(root.getRight());

root.setData(temp.getData());

root.setRight(delete(root.getRight(),root.getData()));

}else {

temp = root;

if (root.getLeft() == null)

root = root.getRight();

else if (root.getRight() == null)

root = root.getLeft();

temp = null;

}

}

return root;

}

  

void InOrderRecursive(Buildbst root) {

if(root == null)

return;

InOrderRecursive(root.getLeft());

System.out.print(root.getData() + " ");

InOrderRecursive(root.getRight());

}

//return a boolean number

public boolean callSearch(int data) {

root = search(root, data);

if (root != null) {

return true;

}

else {

return false;

}

}

  

public void callInsert(int data) {

root = insert(root, data);

}

public void callDelete(int data) {

root = delete(root, data);

}

public void callInOrderRecursive() {

InOrderRecursive(root);

}}

package Assignment5;

public class Testbst {

public static void main(String[] args) {

  

BinarySearchTree tree = new BinarySearchTree();

tree.callInsert(8);

tree.callInsert(4);

tree.callInsert(12);

tree.callInsert(2);

tree.callInsert(6);

tree.callInsert(10);

tree.callInsert(14);

tree.callInsert(1);

tree.callInsert(3);

tree.callInsert(5);

tree.callInsert(7);

tree.callInsert(9);

tree.callInsert(11);

tree.callInsert(13);

tree.callInsert(1);

System.out.println("inorder:\n");

tree.callInOrderRecursive();

tree.callDelete(2);

System.out.println("\nafter remove:");

tree.callInOrderRecursive();

long startTime=System.nanoTime();

System.out.println("\n");

System.out.print(tree.callSearch(15));

long endTime=System.nanoTime();

System.out.println("\n");

System.out.println("cputime:"+(endTime-startTime)+"ns");

}

}

}

How to write the result into a txt file?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Just place the below code at the under of the main function.

This works perfectly and it has no error.

Code:

try {
   long diff=endTime-startTime;

   String result="CPU TIME : "+Long.toString(diff);
FileWriter myWriter = new FileWriter("C:\\Users\\Public\\result.txt");
myWriter.write(result);
myWriter.close();
  
}
  
   catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}

Add a comment
Know the answer?
Add Answer to:
public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Have to write the tree into a text file? JAVA CODE Binary search tree This is...

    Have to write the tree into a text file? JAVA CODE Binary search tree This is the tree public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary search tree public Buildbst(int data) { this.data = data; this.left = null; this.right =null; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Buildbst getLeft() { return left; } public void setLeft(Buildbst left) { this.left = left;...

  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    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; }...

  • BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> {    private TreeNode<E>...

    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...

  • Take the following code for a binary source tree and make it include the following operations....

    Take the following code for a binary source tree and make it include the following operations. bool replace(const Comparable & item, const Comparable & replacementItem); int getNumberOfNodes() const; (a) The replace method searches for the node that contains item in a binary search tree, if it is found, it replaces item with replacementItem. The binary tree should remain as a binary search tree after the replacement is done. Add the replace operation to the BinarySearchTree class. Test your replace using...

  • Preliminaries Download the template class and the driver file. Objective Learn how to traverse a binary...

    Preliminaries Download the template class and the driver file. Objective Learn how to traverse a binary search tree in order. Description For the template class BinarySearchTree, fill in the following methods: insert - Inserts values greater than the parent to the right, and values lesser than the parent to the left.parameters elem - The new element to be inserted into the tree. printInOrder - Prints the values stored in the tree in ascending order. Hint: Use a recursive method to...

  • Removing Nodes from a Binary Tree in Java This section requires you to complete the following...

    Removing Nodes from a Binary Tree in Java This section requires you to complete the following method within BinaryTree.java: public void remove(K key) { } The remove method should remove the key from the binary tree and modify the tree accordingly to maintain the Binary Search Tree Principle. If the key does not exist in the binary tree, no nodes should be removed. In case there is a non-null left child, it should take the place of the removed node....

  • TreeNode.java public class TreeNode {    public int key;    public TreeNode p;    public TreeNode...

    TreeNode.java public class TreeNode {    public int key;    public TreeNode p;    public TreeNode left;    public TreeNode right;       public TreeNode () {        p = left = right = null;    }       public TreeNode (int k) {        key = k;        p = left = right = null;    } } BinarySearchTree.java public class BinarySearchTree {    public TreeNode root;       public BinarySearchTree () {        root...

  • In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write...

    In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...

  • 3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that...

    3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that returns the height of the tree. The height of the tree is the number of levels it contains. Demonstrate the function in a driver program. CPP FILE CODE: #include "BinaryTree.h" #include <iostream> using namespace std; BinaryTree::BinaryTree() { root = NULL; } BinaryTree::~BinaryTree() { destroy(root); } bool BinaryTree::search(int data) { return search(data, root); } void BinaryTree::insert(int data) { insert(data, root); } void BinaryTree::traverseInOrder() { traverseInOrder(root);...

  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT