public class TreeNode { /******** Instance Data *************/ int item; //Data
ID: 3697620 • Letter: P
Question
public class TreeNode
{
/******** Instance Data *************/
int item; //Data field
TreeNode leftChild; //Left Pointer
TreeNode rightChild; //Right Pointer
/******** Constructors **************/
public TreeNode()
{
//Initializes an empty node
item = 0;
leftChild = null;
rightChild = null;
} // end constructor
public TreeNode(int newItem)
{
//Initializes tree node with item and no children.
item = newItem;
leftChild = null;
rightChild = null;
} // end constructor
public TreeNode(int newItem,TreeNode left, TreeNode right)
{
// Initializes tree node with item and
// the left and right children references.
item = newItem;
leftChild = left;
rightChild = right;
} // end constructor
/************ Methods *******************/
public int getItem()
{
// Returns the item field.
return item;
} // end getItem
public void setItem(int newItem)
{
// Sets the item field to the new value newItem.
item = newItem;
} // end setItem
public TreeNode getLeft()
{
// Returns the reference to the left child.
return leftChild;
} // end getLeft
public void setLeft(TreeNode left)
{
// Sets the left child reference to left.
leftChild = left;
} // end setLeft
public TreeNode getRight()
{
// Returns the reference to the right child.
return rightChild;
} // end getRight
public void setRight(TreeNode right)
{
// Sets the right child reference to right.
rightChild = right;
} // end setRight
} // end TreeNode
import java.io.*;
public class BinaryTree
{
TreeNode node; //new nodes
TreeNode root; //root of Tree
boolean isEmpty; //is the tree empty?
/*********** Constructors *******************/
public BinaryTree()
{
//create an empty tree
isEmpty = true; //the tree starts out empty
root = null; //root of tree is null
}// end constructor
/*************** Methods ********************/
public void insertValue(int data)
{
//initiate call to recursive insert function
root = insert(root, data); //updates root if tree is empty
}
public TreeNode insert(TreeNode newNode, int data)
{
//recursive insert function
if (newNode == null) //base case - reached null pointer
{
newNode = new TreeNode(data); //insert node
}
else //general case - find position
{
if (data <= newNode.getItem())
{
//if data is less than current node - move to left
newNode.setLeft(insert(newNode.getLeft(), data));
}
else
{
//if data is greater than current node - move to right
newNode.setRight(insert(newNode.getRight(), data));
}
}
return(newNode); // in any case, return the new pointer to the caller
}
public void display()
{
if(root == null) //don't print if tree is empty
{
System.out.println("The tree is empty.");
}
else //initiate call to recursive print function
{
node = root; //get root of tree
displayInOrder(node); //starting with root, display tree
}
}//end display
public void displayInOrder(TreeNode node)
{
//recursive display in order
//if there is data to the left
if (node.getLeft() != null)
{
//keep moving to the left until no more data
displayInOrder(node.getLeft());
}
//print out node
System.out.println(node.getItem());
//now move to the right
if (node.getRight() != null)
{
//keep traveling right until no more data
displayInOrder(node.getRight());
}
}//end display Node
public void inorder()
{
inorder(root);
}
private void inorder(TreeNode node)
{
if (node != null)
{
inorder(node.leftChild);
System.out.print(node.item +" ");
inorder(node.rightChild);
}
}
public void preorder()
{
preorder(root);
}
private void preorder(TreeNode node)
{
if (node != null)
{
System.out.print(node.item +" ");
preorder(node.leftChild);
preorder(node.rightChild);
}
}
public void postorder()
{
postorder(root);
}
private void postorder(TreeNode node)
{
if (node != null)
{
postorder(node.leftChild);
postorder(node.rightChild);
System.out.print(node.item +" ");
}
}
public boolean search(int val)
{
return search(root, val);
}
//Function to search for an element recursively
private boolean search(TreeNode r, int val)
{
boolean found = false;
while ((r != null) && !found)
{
int rval = r.getItem();
if (val < rval)
r = r.getLeft();
else if
(val > rval)
r = r.getRight();
else
{
found = true;
break;
}
found = search(r, val);
}
return found;
}
} // end BinaryTree
import java.util.Scanner;
import java.io.*;
public class BSTTreeTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
BinaryTree bst = new BinaryTree();
char ch;
int choice;
int[]item = null;
TreeNode node = null;
TreeNode node1 = null;
do
{
System.out.println(" Menu ");
System.out.println(" 1. Read data from external file and inserts into a tree");
System.out.println(" 2. Insert data from keyboard into a tree");
System.out.println(" 3. Searches through tree to find a data element");
System.out.println(" 4. save tree to a separate external file");
System.out.println(" 5. Display data in both files");
choice = scan.nextInt();
switch (choice)
{
case 1 :
item = getFromFile("input.txt");
for(int d : item)
bst.insert(node, d);
break;
case 2 :
while(true)
{
System.out.println("Enter integer element to insert: ");
int d = scan.nextInt();
if(d == -1)
break;
bst.insert(node1, d);
}
break;
case 3 :
System.out.println("Enter integer element to search: ");
System.out.println("Search result : "+ bst.search(scan.nextInt()));
break;
case 4 :
saveToFile(item);
break;
case 5 :
System.out.println("Input file data : "+getFromFile("input.txt"));
System.out.println("Output file data : "+getFromFile("output.txt"));
break;
default :
System.out.println("Wrong Entry ");
break;
}
System.out.print(" Post order : ");
bst.postorder();
System.out.print(" Pre order : ");
bst.preorder();
System.out.print(" In order : ");
bst.inorder();
System.out.println(" Do you want to continue(y or n) : ");
ch = scan.next().charAt(0);
}
while (ch == 'Y'|| ch == 'y');
}
public static int[] getFromFile(String fileName)
{
int[] item = null;
BufferedReader br = null;
int i=0;
String line;
try
{
br = new BufferedReader(new FileReader(fileName));
line = br.readLine();
String nums[] = line.split(",");
item = new int[nums.length];
for(String s : nums)
{
item[i++] = Integer.parseInt(s);
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)br.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
return item;
}
public static void saveToFile(int[] item)
{
try
{
File file = new File("output.txt");
if(!file.exists())
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
for(int i : item)
bw.write(i+",");
bw.close();
System.out.println("Saved..");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
Explanation / Answer
Hi, You have to give the valid input and out file paths. I have highlighted lines which need to update by you. Please provie valid file pat h in that areas then program will execute for sure.
BSTTreeTest.java
import java.util.Scanner;
import java.io.*;
public class BSTTreeTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
BinaryTree bst = new BinaryTree();
char ch;
int choice;
int[]item = null;
TreeNode node = null;
TreeNode node1 = null;
do
{
System.out.println(" Menu ");
System.out.println(" 1. Read data from external file and inserts into a tree");
System.out.println(" 2. Insert data from keyboard into a tree");
System.out.println(" 3. Searches through tree to find a data element");
System.out.println(" 4. save tree to a separate external file");
System.out.println(" 5. Display data in both files");
choice = scan.nextInt();
switch (choice)
{
case 1 :
item = getFromFile("D:\input.txt");
for(int d : item)
bst.insert(node, d);
break;
case 2 :
while(true)
{
System.out.println("Enter integer element to insert: ");
int d = scan.nextInt();
if(d == -1)
break;
bst.insert(node1, d);
}
break;
case 3 :
System.out.println("Enter integer element to search: ");
System.out.println("Search result : "+ bst.search(scan.nextInt()));
break;
case 4 :
saveToFile(item);
break;
case 5 :
System.out.println("Input file data : "+getFromFile("D:\input.txt"));
System.out.println("Output file data : "+getFromFile("D:\output.txt"));
break;
default :
System.out.println("Wrong Entry ");
break;
}
System.out.print(" Post order : ");
bst.postorder();
System.out.print(" Pre order : ");
bst.preorder();
System.out.print(" In order : ");
bst.inorder();
System.out.println(" Do you want to continue(y or n) : ");
ch = scan.next().charAt(0);
}
while (ch == 'Y'|| ch == 'y');
}
public static int[] getFromFile(String fileName)
{
int[] item = null;
BufferedReader br = null;
int i=0;
String line;
try
{
br = new BufferedReader(new FileReader(fileName));
line = br.readLine();
String nums[] = line.split(",");
item = new int[nums.length];
for(String s : nums)
{
item[i++] = Integer.parseInt(s);
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)br.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
return item;
}
public static void saveToFile(int[] item)
{
try
{
File file = new File("output.txt");
if(!file.exists())
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
for(int i : item)
bw.write(i+",");
bw.close();
System.out.println("Saved..");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
Output:
Menu
1. Read data from external file and inserts into a tree
2. Insert data from keyboard into a tree
3. Searches through tree to find a data element
4. save tree to a separate external file
5. Display data in both files
1
Post order :
Pre order :
In order :
Do you want to continue(y or n) :
y
Menu
1. Read data from external file and inserts into a tree
2. Insert data from keyboard into a tree
3. Searches through tree to find a data element
4. save tree to a separate external file
5. Display data in both files
2
Enter integer element to insert:
4
Enter integer element to insert:
5
Enter integer element to insert:
6
Enter integer element to insert:
-1
Post order :
Pre order :
In order :
Do you want to continue(y or n) :
y
Menu
1. Read data from external file and inserts into a tree
2. Insert data from keyboard into a tree
3. Searches through tree to find a data element
4. save tree to a separate external file
5. Display data in both files
3
Enter integer element to search:
2
Search result : false
Post order :
Pre order :
In order :
Do you want to continue(y or n) :
y
Menu
1. Read data from external file and inserts into a tree
2. Insert data from keyboard into a tree
3. Searches through tree to find a data element
4. save tree to a separate external file
5. Display data in both files
4
Saved..
Post order :
Pre order :
In order :
Do you want to continue(y or n) :
y
Menu
1. Read data from external file and inserts into a tree
2. Insert data from keyboard into a tree
3. Searches through tree to find a data element
4. save tree to a separate external file
5. Display data in both files
5
Input file data : [I@33dd66fd
Output file data : [I@c9486a5
Post order :
Pre order :
In order :
Do you want to continue(y or n) :
n
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.