Question

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;

}

public Buildbst getRight() {

return right;

}

public void setRight(Buildbst right) {

this.right = right;

}

}

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;

}

}


1 2 3 4 5 6 7 8 9 10 11 12 13 14 after rerrave: 1 3 4 5 6 7 8 9 10 11 12 13 14 public class Testost { public static void main

The text file should output just like what I got in the console.

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

//Java program

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;

}

}

class BinarySearchTree{
   private Buildbst root;
  
   public BinarySearchTree() {
       root = null;
   }
  
   private boolean search(Buildbst root, int key)
   {
        if (root==null)return false;
        if (root.getData()==key) return true;
  
        if (root.getData() > key)
            return search(root.getLeft(), key);
     
        return search(root.getRight(), key);
   }

   public boolean callSearch(int key) {
       return search(root , key);
   }
  
   private Buildbst insert(Buildbst root,int data) {
       if(root==null)return new Buildbst(data);
       if(root.getData() > data)
           root.setLeft( insert(root.getLeft(),data));
      
       if(root.getData() < data)
           root.setRight(insert(root.getRight(),data));
       return root;
   }
   public void callInsert( int key ){
       root = insert(root , key);
   }
  
   public void callInOrderRecursive() {
       inOrder(root);
      }

   private void inOrder(Buildbst root) {
       if(root == null) {
           return;
       }
       inOrder(root.getLeft());
       System.out.print(root.getData() + " ");
       inOrder(root.getRight());
   }
   private Buildbst delete(Buildbst root,int key) {  
       if(root==null)return root;
       else if(root.getData()< key)
           root.setRight(delete (root.getRight(),key));
       else if(root.getData() > key)
           root.setLeft(delete(root.getLeft(),key));
       else {
           if(root.getLeft()==null)
               return root.getRight();
           if(root.getRight()==null)
               return root.getLeft();
          
           Buildbst min=findmin(root.getRight());
           root.setData(min.getData());
           root.setRight( delete(root.getRight(),min.getData()));
          
       }
       return root;
      
   }
   private Buildbst findmin(Buildbst root) {
       while(root.getLeft()!=null)
           root=root.getLeft();
       return root;
   }

   public void callDelete( int key ){
       root = delete(root , key);
   }
}

public class Testbst{
   public static void main(String [] args){
       BinarySearchTree tree = new BinarySearchTree();
       tree.callInsert(8);
       tree.callInsert(4);
       tree.callInsert(12);
       tree.callInsert(8);
       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");
      
      
   }
}


//sample output

inorder: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 after remove 1 3 4 5 6 7 8 9 10 11 12 13 14 false cputime: 105506ns

Add a comment
Know the answer?
Add Answer to:
Have to write the tree into a text file? JAVA CODE Binary search tree This is...
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
  • 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; } }...

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

  • Create a communication information system for a company with using Red-Black Tree Method.

    PLEASE USE THE HEADER FILE THAT I ADDED TO THE END OF THE QUESTIONWrite this program with using C/C++, C#, Java or Python. Explain your code with comments. Create a communication information system for a company with using Red-Black Tree Method. Inthis system the users are allowed to search an employee from employee's registration number(id)and retreive the department number(id) and that employee's internal phone number. Also, if theemployee they are looking for is not in the tree, they can add it....

  • Create a communication information system for a company with using Red-Black Tree Method

    • Write this program with using C/C++, C#, Java or Python. Explain your code with comments. Create a communication information system for a company with using Red-Black Tree Method. Inthis system the users are allowed to search an employee from employee's registration number(id)and retreive the department number(id) and that employee's internal phone number. Also, if theemployee they are looking for is not in the tree, they can add it. (in accordance with the red-blacktree properties)a) Implement the Employee class:- Registration number...

  • Test Data would be as follows: Using java language. Build an expression tree. • When you...

    Test Data would be as follows: Using java language. Build an expression tree. • When you see a number: o you create a new node with the number as the data. o Then you push the new node to the stack. . When you see a binary operator: 0 You create a new Node for the operator. Then you pop and set the right child. Then you pop and set the left child o Then you push the new node...

  • Java binary search tree Add the following print method to the binary search tree class created...

    Java binary search tree Add the following print method to the binary search tree class created in class (on D2L). This method should print all the nodes in the tree in level order (root first, then all children of root, then all children of those). Ensure your method runs in O(N), include comments to show how it conforms to this rule. Method header: public void printInLevelOrder() public class BinarySearchTree<E extends Comparable<? super E>> { private Node root; public BinarySearchTree() {...

  • COMP 1406- Winter 2019 Sample Midterm #3 D: Binary Trees 5 marks Consider a binary tree implement...

    i need help pls COMP 1406- Winter 2019 Sample Midterm #3 D: Binary Trees 5 marks Consider a binary tree implemented using the following Node class. : public class Nodef a public int data; s public Node left; 4 public Node right; s public Node(int data, Node left, Node right) 6 this.data data; this.left left; this.right right; s public Node(int data)I this(data, null, null: Draw the tree that the following code generates. If a node is null, you do NOT...

  • Professionally and thoroughly comment on this code. //BinarySearchTree.java package Project.bst; //imports required import java.io.BufferedReader; import java.io.FileNotFoundException;...

    Professionally and thoroughly comment on this code. //BinarySearchTree.java package Project.bst; //imports required import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; class BSTTreeNode{    BSTTreeNode left, right;    String data; public BSTTreeNode(){    left = null;    right = null;    data = null; } public BSTTreeNode(String n){    left = null;    right = null;    data = n; } public void setLeft(BSTTreeNode n){    left = n; } public void setRight(BSTTreeNode n){   ...

  • Please help me with me. I did the first part to write the operations but in...

    Please help me with me. I did the first part to write the operations but in the driver its shows me errors, can't fix it. Help me. Need help with this to run the application. /** * BinaryTreeNode represents a node in a binary tree with a left and * right child. * * @author Java Foundations * @version 4.0 */ public class BinaryTreeNode { protected T element; protected BinaryTreeNode left, right; /** * Creates a new tree node with...

  • PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need...

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

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