PLEASE HELP IN JAVA SimpleTreeTesterjava import javaioFileRe
PLEASE HELP IN JAVA
SimpleTreeTester.java
import java.io.FileReader;
import java.io.IOException;
import java.awt.BorderLayout;
DisplayPanel.java
import java.awt.Color;
import java.awt.Dimension;
BinaryTree.java
class BinaryTree
Node.java
input.txt
dog cat lion pig horse cow deer
QUESTION:
Take a look at the various references to BinaryTree\'s insert() method. They all take the form:
{node} = insert ({node}, data);
where {node} refers to the same node for both instances in the syntax. This is unnecessary code duplication. Change the implementation of insert() to eliminate this problem, and have it only refer to {node} once.
Solution
Make changes in SimpleTreeTester class inside if condition and make Node class generic type. and follow the code below:
Node.java
class Node<T>
 {
 T data;
 Node left;
 Node right;
   
 int xpos;
 int ypos;
Node (T val, Node left, Node right)
 {
 this.left = left;
 this.right = right;
 this.data = val;
 }
 }
BinaryTree.java
class BinaryTree
 {
 Node root;
   
 int numNodes;
 int treeHeight;
BinaryTree ()
 {
 root = null;
 numNodes = 0;
 treeHeight = 0;
 }
public int getTreeHeight (Node node)
 {
 if (node == null)
 return -1;
 else
 {
 treeHeight = 1 + max (getTreeHeight (node.left), getTreeHeight (node.right));
 return treeHeight;
 }
 }
   
 public int max (int a, int b) {
 if (a > b)
 return a;
 else
 return b;
 }
public void computeNodePositions () {
 int depth = 1;
 inorderTraversal (root, depth);
 }
// Traverse tree and computes x,y position of each node, and stores it in the node
 private void inorderTraversal (Node node, int depth)
 {
 if (node != null)
 {
 inorderTraversal(node.left, depth + 1);
 node.xpos = numNodes++;
 node.ypos = depth;
 inorderTraversal (node.right, depth + 1);
 }
 }
// Binary Tree insert
 public Node insert (Node root, String data)
 {
 if (root == null)
 {
 root = new Node (data, null, null);
 return root;
 }
 else
 {
 if (data.compareTo ((String) root.data) == 0)
 {
 // duplicate word found - do nothing
 return root;
 }
 else if (data.compareTo ((String) root.data) < 0)
 root.left = insert (root.left, data);
 else
 root.right = insert (root.right, data);
   
 return root;
 }
 }
 }
SimpleTreeTester.java
import java.io.FileReader;
 import java.io.IOException;
 import java.util.Scanner;
public class SimpleTreeTester
 {
 public static void main (String[] args)
 {
 BinaryTree tree = new BinaryTree ();
 String buildString = \"\";
if (args.length == 1) //if you put args.length==0 it will get inside if condition and exit
 {
 System.out.println (\"usage: java SimpleTreeTester textfile\");
 System.exit (0);
 }
try
 {
 Scanner input = new Scanner (new FileReader (\"E:/java/input.txt\"));//put here location according to your system directory
   
 while (input.hasNext ())
 {
 String word = input.next ();
 word = word.toLowerCase ();
 buildString += \" \" + word;
 tree.root = tree.insert (tree.root, word);
 }
   
 input.close();
 }
 catch (IOException e) {
 System.out.println (\"io exception\");
 }
tree.computeNodePositions ();
 tree.getTreeHeight (tree.root);
SimpleTreeGUI gui = new SimpleTreeGUI (tree, buildString);
 }
 }
DisplayPanel.java
import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.Font;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
import javax.swing.JPanel;
class DisplayPanel extends JPanel
 {
 BinaryTree tree;
 String buildString;
 int xs;
 int ys;
int screenWidth = 800;
 int screenHeight = 600;
   
 public DisplayPanel (BinaryTree tree, String buildString)
 {
 super ();
 this.tree = tree;
 this.buildString = buildString;
   
 setBackground (Color.white);
 setForeground (Color.black);
   
 super.setPreferredSize (new Dimension(screenWidth, screenHeight));
 }
protected void paintComponent (Graphics g)
 {
 Graphics2D g2 = (Graphics2D) g;
   
 g2.setColor (getBackground());
 g2.fillRect (0, 0, getWidth(), getHeight());
   
 g2.setColor (getForeground());
 Font MyFont = new Font (\"SansSerif\", Font.PLAIN, 14);
 g2.setFont (MyFont);
   
 //where to start printing on the panel
 xs = 10;
 ys = 20;
   
 g2.drawString (\"Binary Search tree for the input string:\ \", xs, ys);
   
 xs += 10;
 ys += 14;
 g2.drawString (buildString, xs, ys);
   
 MyFont = new Font (\"SansSerif\", Font.BOLD, 18);
 g2.setFont (MyFont);
   
 drawTree (g2, tree.root);
 repaint ();
 }
public void drawTree (Graphics g, Node root)
 {
 Graphics2D g2 = (Graphics2D) g;
   
 int dx, dy, dx2, dy2;
 int XSCALE, YSCALE;
 XSCALE = screenWidth / tree.numNodes;
 YSCALE = (screenHeight-ys) / (tree.treeHeight + 1);
if (root != null) {
 drawTree (g2, root.left);
 dx = root.xpos * XSCALE;
 dy = root.ypos * YSCALE + ys;
   
 String s = (String) root.data;
 g2.drawString (s, dx, dy);
   
 if (root.left != null)
 {
 dx2 = root.left.xpos * XSCALE;
 dy2 = root.left.ypos * YSCALE + ys;
 g2.drawLine (dx, dy, dx2, dy2);
 }
   
 if (root.right != null)
 {
 dx2 = root.right.xpos * XSCALE;
 dy2 = root.right.ypos * YSCALE + ys;
 g2.drawLine (dx, dy, dx2, dy2);
 }
   
 drawTree (g2, root.right);
 }
 }
 }
SimpleTreeGui.java
import java.awt.BorderLayout;
 import javax.swing.JFrame;
 import javax.swing.JScrollPane;
public class SimpleTreeGUI extends JFrame
 {
 public SimpleTreeGUI (BinaryTree tree, String buildString)
 {
 DisplayPanel panel = new DisplayPanel (tree, buildString);
JScrollPane scrollpane = new JScrollPane (panel);
 add (scrollpane, BorderLayout.CENTER);
setSize (600, 500);
 setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
 setVisible (true);
 }
 }





