Java Program Instructions This is a challenging assignment For this assignment y
ID: 3690254 • Letter: J
Question
Java Program
Instructions
This is a challenging assignment
For this assignment you are to develop a program that creates a binary search tree. You are to then read the text file into the binary tree. Finally you are supposed to output the in-order traversal of the binary tree. This should show a sorted list of the 50 state abbreviations in order.
You program will be incorrect if you do not program the binary tree and instead use tree collection.
Use the package name treeStates.
Hints:
You may want to create a Node class to define a tree node
You may want to create a utility class to define how to print tree nodes
You may want to create a BinaryTree class that has a node member
The file they want us to use is named states.txt
The contents of this file is as follows
Explanation / Answer
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Test{ public static void main(String[] args) { BufferedReader br = null; Node binaryTree = null; try { String state; br = new BufferedReader(new FileReader("states.txt")); binaryTree = new Node(br.readLine()); while ((state = br.readLine()) != null) { binaryTree.addNode(state); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null)br.close(); } catch (IOException ex) { ex.printStackTrace(); } } System.out.println("In order traversal : "); binaryTree.traverseInOrder(); } } class Node { private String data; private Node left; private Node right; public Node(String data) { this.data = data; this.left = null; this.right = null; } public void addNode(String data) { if (data.compareTo(this.data)Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.