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

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

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

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

  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

  • Java : This function is to search through a binary tree left and right and return...

    Java : This function is to search through a binary tree left and right and return a count of the nodes above depth k. This is what I have so far, but I'm getting a Null pointer exception. public class MyIntSET {    private Node root;    private static class Node {        public final int key;        public Node left, right;        public Node(int key) { this.key = key; }    }    public int sizeAboveDepth(int...

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