/**
* Informatik II - FS2009
* Uebungsserie 3, Aufgabe 4
* Template for LKD.java
*
* LKD verifies the syntactical correctness of a binary tree given
* in left-parenthesis representation
*
* @author Philipp Bolliger
*/
public class LKD {
//the private class field tree contains the
//left-parenthesis representation of a binary tree
private char tree[];
/**
* Constructor initializes the class field tree.
*
* @param tree binary tree in left-parenthesis representation
*/
public LKD(char[] treeInLKDForm) {
tree = treeInLKDForm;
}
/**
* Prints an array of chars and marks the last element with "^"
* (You can use this method to indicate the element actually causing a syntax error)
*
* @param testIndex the index of the last correct element in the tree
*/
public void invalidPosition( int testIndex ) {
System.out.println( tree );
for(int j=0; j < testIndex; j++ ) {
System.out.print(" ");
}
System.out.println("^");
}
/**
* Verifies the syntactical correctness of the left-parenthesis
* representation of a binary tree
*/
public void syntaxChecker() {
int testIndex = 0;
//ADD YOUR CODE HERE
//MODIFY THE TEMPLATE IF NECESSARY OR APPROPRIATE!
//DEFINE AND IMPLEMENT ADEQUATE METHODS TO PERFORM THE SYNTAX CHECK!
//testIndex = ....
if( testIndex == tree.length ) {
//correct left-parenthesis representation
System.out.println("Valid left-parenthesis representation :-) ");
}
else {
System.out.println( "ERROR: "+
"Error type 007 detected: ");
invalidPosition( testIndex );
}
}
public static void main(String[] args) {
//Parse the input
if( args.length != 1 ) {
System.out.println( "Invalid input!");
System.out.println( "Example of a correct call to LKD.java: " +
"java LKD \"A(B(C,B(A)))\"" );
System.exit( 1 );
}
//Convert input string to an array of chars (char[]):
char[] inputLKD = args[0].toCharArray();
//New instance of the LKD class initialized with the
//left-parenthesis representation given as input
LKD check = new LKD(inputLKD);
//Syntax-checker:
check.syntaxChecker();
}
}