Homework Help Question & Answers

Write a java application to read in an attached CSV file and store the data items within a Binary...

Write a java application to read in an attached CSV file and store the data items within a Binary Search Tree.

0 0
Next > < Previous
ReportAnswer #1

Code:

import java.util.*;
import java.io.*;

//File name CSVToBST.java
public class CSVToBST
{

   public static void main(String[] args) {
       // Creates an Object to BST Class
       BST b = new BST();

       //You can take file name from the user using
       /**
           Scanner in = new Scanner(System.in);
           File file = nextLine();
           */
      File file = new File("example.csv");
      //Creates a scanenr for file
      Scanner input;

      try{
          //assign the Scanner with file.
          input = new Scanner(file);
          //Until the end of file do the following.
          while(input.hasNext())
          {
              //Read a line
              String line= input.next();
              //Split the line at , which returns a Array of strings
              String[] values = line.split(",");
              //For all strings insert into the BST.
              for (String i : values) {
                  //While inserting convert the given string to integer.
                  b.insert(Integer.parseInt(i));
              }
          }
          //Close the input stream.
          input.close();
      }
      //If exception raises handle it.
      catch(FileNotFoundException e)
      {
          System.out.println("No such file found!!");
      }
      //To display the constructed BST.
      System.out.println("Obtained BST is");
      b.display();
   }
}

//To hold the BST data
class Node
{
   //properties data to hold data
   //left , right to holds addresses of left and right nodes.
   int data;
   Node left,right;

   //initialize the node with the given data.
   public Node(int data)
   {
       this.data=data;
       left=right=null;
   }
}


//Class to construct BST
class BST
{

   //A root to hold the root address
   private Node root;

   //Constructer to initialize root.
   public BST()
   {
       root = null;
   }

   //method insert to insert the passed argumetn x.
   public void insert(int x)
   {
       //use the utility function to insert the value.
       root = insert(this.root,x);
   }

   //a private utility function to insert the value.
   private Node insert(Node cur,int x)
   {
       //if the current node is null create new node and return.
       if(cur == null)
       {
           return new Node(x);
       }
       //if the value in current node is grater then the x insert in the left subtree.
       if(cur.data > x)
       {
           cur.left = insert(cur.left, x);
       }
       //in other case insert in the right subtree.
       else
       {
           cur.right = insert(cur.right, x);
       }
       //finally return the current (cur).
       return cur;
   }

   //function to display the tree inorder
   public void display()
   {
       if(this.root == null)
       {
           System.out.println("No tree found");
       }
       System.out.print("Inorder:");
       inorder(this.root);
       System.out.println();
       System.out.print("Preorder:");
       preorder(this.root);
   }

   //A private utility function to display Tree inorder.
   private void inorder(Node cur)
   {
       if(cur != null)
       {
           inorder(cur.left);
           System.out.print (cur.data+" ");
           inorder(cur.right);
       }
   }

   //A private utility function to display Tree preorder.
   private void preorder(Node cur)
   {
       if(cur != null)
       {
           System.out.print (cur.data+" ");
           preorder(cur.left);
           preorder(cur.right);
       }
   }
}

Input:

example.csv:

1,5,3,6,7,2 9,10,4,e,

Output:

Obtained BST is Inorder:0 1 2 3 4 5 6 7 8 9 18 Preorder:1 0 5 3 2 467 9 8 10

Hope your problem solved.

Feel Free to comment doubts in the comment section.

Add Homework Help Answer
Add Answer of:
Write a java application to read in an attached CSV file and store the data items within a Binary...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
More Homework Help Questions Additional questions in this topic.
Need Online Homework Help?
Ask a Question
Get FREE EXPERT Answers
WITHIN MINUTES
Related Questions