Please develop a Java program to: 1. Read input records from qz1Data.txt into ja
ID: 3709422 • Letter: P
Question
Please develop a Java program to:
1. Read input records from qz1Data.txt into java.util.ArrayList.
2. Display the CPU time used in Step 1.
3. Display the memory used by ArrayList data structure.
4. Read input records from qz1Data.txt into a binary tree data structure.
5. Display the CPU time used in Step 4.
6. Display the memory used by the binary tree data structure.
Objective:
Compare the Memory efficiency and CPU efficiency between java.util.ArrayList data structure and the binary tree data structure.
Hint: This program generates 2 or 4 lines of output
Input file: qz1Data.txt (containing a set of integer numbers) (The same data file used in Quiz #1)
30999 18345 23639 37731 42853
30931 45139 41830 46484 31295
38176 20117 46939 13399 27712
Assumption: The number of records in qz1Data.txt is unknown
One java file please
Explanation / Answer
I have commented out both the section. Uncomment a section at a time and see the results. Also create a file to your local system and change the path in the code.
package com.chegg.bst;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
class BTNode
{
BTNode left, right;
int data;
/* Constructor */
BTNode()
{
left = null;
right = null;
data = 0;
}
/* Constructor */
BTNode(int n)
{
left = null;
right = null;
data = n;
}
/* Function to set left node */
public void setLeft(BTNode n)
{
left = n;
}
/* Function to set right node */
public void setRight(BTNode n)
{
right = n;
}
/* Function to get left node */
public BTNode getLeft()
{
return left;
}
/* Function to get right node */
public BTNode getRight()
{
return right;
}
/* Function to set data to node */
public void setData(int d)
{
data = d;
}
/* Function to get data from node */
public int getData()
{
return data;
}
}
class BT
{
private BTNode root;
/* Constructor */
BT()
{
root = null;
}
/* Function to check if tree is empty */
public boolean isEmpty()
{
return root == null;
}
/* Functions to insert data */
public void insert(int data)
{
root = insert(root, data);
}
/* Function to insert data recursively */
private BTNode insert(BTNode node, int data)
{
if (node == null)
node = new BTNode(data);
else
{
if (node.getRight() == null)
node.right = insert(node.right, data);
else
node.left = insert(node.left, data);
}
return node;
}
/* Function to count number of nodes */
public int countNodes()
{
return countNodes(root);
}
/* Function to count number of nodes recursively */
private int countNodes(BTNode r)
{
if (r == null)
return 0;
else
{
int l = 1;
l += countNodes(r.getLeft());
l += countNodes(r.getRight());
return l;
}
}
/* Function to search for an element */
public boolean search(int val)
{
return search(root, val);
}
/* Function to search for an element recursively */
private boolean search(BTNode r, int val)
{
if (r.getData() == val)
return true;
if (r.getLeft() != null)
if (search(r.getLeft(), val))
return true;
if (r.getRight() != null)
if (search(r.getRight(), val))
return true;
return false;
}
/* Function for inorder traversal */
public void inorder()
{
inorder(root);
}
private void inorder(BTNode r)
{
if (r != null)
{
inorder(r.getLeft());
System.out.print(r.getData() +" ");
inorder(r.getRight());
}
}
/* Function for preorder traversal */
public void preorder()
{
preorder(root);
}
private void preorder(BTNode r)
{
if (r != null)
{
System.out.print(r.getData() +" ");
preorder(r.getLeft());
preorder(r.getRight());
}
}
/* Function for postorder traversal */
public void postorder()
{
postorder(root);
}
private void postorder(BTNode r)
{
if (r != null)
{
postorder(r.getLeft());
postorder(r.getRight());
System.out.print(r.getData() +" ");
}
}
}
public class Test {
public static void main(String[] args) throws Exception {
File file = new File("C:\Users\Abhishek Sardana\inFile.txt");
File file1 = new File("C:\Users\Abhishek Sardana\inFile1.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
BufferedReader br1 = new BufferedReader(new FileReader(file1));
List<Integer> list = new ArrayList<Integer>();
BT tree = new BT();
try {
/*String st;
long beforeUsedMem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
long millisStart = System.currentTimeMillis() % 1000;
while ((st = br.readLine()) != null) {
String arr[] = st.split(" ");
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null && arr[i].trim() != "") {
list.add(Integer.parseInt(arr[i]));
}
}
}
long millisEnd = System.currentTimeMillis() % 1000;
long afterUsedMem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
System.out.println("Time taken " + (millisEnd - millisStart));
System.out.println("Memory Used " + (afterUsedMem - beforeUsedMem));*/
/* String st1;
long beforeUsedMem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
long millisStart1 = System.currentTimeMillis() % 1000;
while ((st1 = br1.readLine()) != null) {
String arr[] = st1.split(" ");
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null && arr[i].trim() != "") {
tree.insert(Integer.parseInt(arr[i]));
}
}
}
long millisEnd1 = System.currentTimeMillis() % 1000;
long afterUsedMem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
System.out.println("BT : Time taken " + (millisEnd1 - millisStart1));
System.out.println("BT : Memory Used " + (afterUsedMem1 - beforeUsedMem1));*/
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (br1 != null)
br1.close();
} catch (Exception ex) {
System.out.println("Error in closing the BufferedWriter" + ex);
}
}
}
}