When I compile my 3 Java classes ( RedBlackTree1.java , Node1.java, Main1.java )
ID: 3732401 • Letter: W
Question
When I compile my 3 Java classes ( RedBlackTree1.java , Node1.java, Main1.java ). My Main1.java has some errors. Can someone please help me fix my program errors.
RedBlackTree1.java
public class RedBlackTree1<E extends Comparable<E>> {
private Node1<E> current;
private Node1<E> parent;
private Node1<E> grand;
private Node1<E> great;
private Node1<E> header;
private static Node1 nullNode;
/* static initializer for nullNode */
static
{
nullNode = new Node1(0);
nullNode.leftChild = nullNode;
nullNode.rightChild = nullNode;
}
/* Black - 1 RED - 0 */
static final boolean BLACK = true;
static final boolean RED = false;
public String preOrder = "";
/* Constructor */
public RedBlackTree1(int negInf)
{
header = new Node1(negInf);
header.leftChild = nullNode;
header.rightChild = nullNode;
}
public boolean insert(E item ) throws NullPointerException
{
boolean result = true;
if(item == null) {
throw new NullPointerException();
}
current = parent = grand = header;
nullNode.element = item;
while (current.element != item)
{
great = grand;
grand = parent;
parent = current;
current = item.compareTo(current.element) < 0 ? current.leftChild : current.rightChild;
// Check if two red children and fix if so
if (current.leftChild.color == RED && current.rightChild.color == RED)
handleReorient( item );
}
// Insertion fails if already present
if (current != nullNode)
return false;
current = new Node1(item, nullNode, nullNode);
// Attach to parent
if (item.compareTo(parent.element) < 0)
parent.leftChild = current;
else
parent.rightChild = current;
handleReorient( item );
return result;
}
public Node1<E> getCurrent(){
return current;
}
private void handleReorient(E item)
{
// Do the color flip
current.color = RED;
current.leftChild.color = BLACK;
current.rightChild.color = BLACK;
if (parent.color == RED)
{
// Have to rotate
grand.color = RED;
if (item.compareTo(grand.element) < -1 != item.compareTo(parent.element) < -1)
parent = rotate( item, grand ); // Start dbl rotate
current = rotate(item, great );
current.color = BLACK;
}
// Make root black
header.rightChild.color = BLACK;
}
private Node1 rotate(E item, Node1 parent)
{
if(item.compareTo((E) parent.element) < 0)
return parent.leftChild = item.compareTo((E) parent.leftChild.element) < 0 ? rotateWithleftChildChild(parent.leftChild) : rotateWithrightChildChild(parent.leftChild) ;
else
return parent.rightChild = item.compareTo((E) parent.rightChild.element) < 0 ? rotateWithleftChildChild(parent.rightChild) : rotateWithrightChildChild(parent.rightChild);
}
/* Rotate binary tree node with leftChild child */
private Node1 rotateWithleftChildChild(Node1 k2)
{
Node1 k1 = k2.leftChild;
k2.leftChild = k1.rightChild;
k1.rightChild = k2;
return k1;
}
/* Rotate binary tree node with rightChild child */
private Node1 rotateWithrightChildChild(Node1 k1)
{
Node1 k2 = k1.rightChild;
k1.rightChild = k2.leftChild;
k2.leftChild = k1;
return k2;
}
/**
* Find an item in the tree.
* @param x the item to search for.
* @return the matching item or null if not found.
*/
public boolean contains(E x ) {
if(x == null) {
return false;
}
nullNode.element = x;
current = header.rightChild;
for( ; ; ) {
if( x.compareTo( current.element ) < 0 )
current = current.leftChild;
else if( x.compareTo( current.element ) > 0 )
current = current.rightChild;
else if( current != nullNode )
return true;
else
return false;
}
}
public void toStringUtil(Node1 t) {
if( t != nullNode ) {
if(t.color == RED) {
preOrder.concat("*");
}
preOrder.concat(t.element + " ");
toStringUtil( t.leftChild );
toStringUtil( t.rightChild );
}
}
@Override
public String toString() {
toStringUtil(header);
return preOrder;
}
}
Node1.java
public class Node1<E extends Comparable<E>>{
E element ;
Node1 leftChild;
Node1 rightChild;
Node1 parent;
boolean color;
Node1(E element){
this.element = element;
}
Node1(E element,Node1 left, Node1 right){
this.element = element;
this.leftChild = left;
this.rightChild = right;
}
}
Main1.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main1 {
public static void main(String[] args)
{
if(args[0] == null || args[1] == null)
{
System.out.println("Please give input/output file name");
return ;
}
// The name of the file to open.
String ipfileName = args[0];
String opfileName = args[1];
String line;
try {
// FileReader reads text files in the default encoding.
FileReader ipfileReader =
new FileReader(ipfileName);
FileWriter opfileWriter =
new FileWriter(opfileName);
// Always wrap FileReader in BufferedReader.
BufferedReader ipbufferedReader =
new BufferedReader(ipfileReader);
BufferedWriter opbufferedWriter =
new BufferedWriter(opfileWriter);
line = ipbufferedReader.readLine();
if(line.equals("Integer")) {
RedBlackTree1<Integer> rbTree = new RedBlackTree1<Integer>(Integer.MIN_VALUE);
while((line = ipbufferedReader.readLine()) != null) {
if(line.startsWith("Insert")) {
opbufferedWriter.write(Boolean.toString((rbTree.insert(Integer.parseInt(line.split(":")[1])))));
}else if(line.startsWith("Contains")) {
opbufferedWriter.write(Boolean.toString((rbTree.contains(Integer.parseInt(line.split(":")[1])))));
}else if(line.startsWith("PrintTree")) {
opbufferedWriter.write(rbTree.toString());
}
}
}else if(line.equals("String")){
RedBlackTree1<String> rbTree = new RedBlackTree1<String>(Integer.MIN_VALUE);
while((line = ipbufferedReader.readLine()) != null) {
if(line.startsWith("Insert")) {
opbufferedWriter.write(Boolean.toString((rbTree.insert((line.split(":")[1])))));
}else if(line.startsWith("Contains")) {
opbufferedWriter.write(Boolean.toString((rbTree.contains((line.split(":")[1])))));
}else if(line.startsWith("PrintTree")) {
opbufferedWriter.write(rbTree.toString());
}
}
}
// Always close files.
ipbufferedReader.close();
opbufferedWriter.close();
}
catch(FileNotFoundException ex) {
ex.printStackTrace();
}
catch(IOException ex) {
ex.printStackTrace();
}
}
}
Explanation / Answer
Dear, i compiled all the 3 .java files, they got compiled very well, there is no compilation error in the program's.
First thing i would like you to check, do you have jdk(java software) installed in your system or not with proper path and classpath setup.
After that you should keep all the 3 .java files in the same directory or folder.
Compile Node1.java file first, RedBlackTree1.java file second and Main1.java last.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.