In Java I am trying to create an operand tree for a boolean
In Java I am trying to create an operand tree for a boolean expression. It should take input from the user such as ttO and creates a tree where t stands for true:
O
/ \\
t t
and would be able to evalute the tree so that it returns true
Solution
import java.io.*;
public class booleanexpressionTree {
// we will first declare and define the Instance Variables
private Object data;
//First we will declare the left sub-tree
private booleanexpressionTree left;
//Secondly we will declare The right sub-tree
private booleanexpressionTree right;
//we will now declare the class Variables
// the stream token will take the input ttO
Static StreamTokenizer
private static StreamTokenizer lexical = \"ttO\";
//
The next step is to create an expression tree that corresponds to a Boolean
expression that consists of a single <i>variable</i>.
The parameter variableone is the variable to be placed in the tree.
public booleanexpressionTree( String variableone ) {
data = variableone;
left = right = null;
}
// The next step is to Create an expression tree that corresponds to a Boolean expression
of the form: we will have 3 arguements for the same
The first arguement is binaryOperatorone which is the operator to be placed at the root
The second arguement leftOperandone which is the tree corresponding to the left operand
The third arguement is rightOperandone which is the tree corresponding to the right operand
public booleanexpressionTree( char binaryOperatorone,
booleanexpressionTree leftOperandone,
booleanexpressionTree rightOperandone ) {
data = new Character( binaryOperatorone );
left = leftOperandone;
right = rightOperandone;
}
// Now we will print an inorder (LVR) traversal of this tree to standard output using the inorder function().
public void inOrder() {
// Firstly we will traverse the left tree
if ( left != null ) {
if ( right != null ) {
System.out.print( \"(\" );
}
left.inOrder();
}
// The next step is to process current node
if ( left == null && right != null ) {
System.out.print( data.toString() );
}
else if ( left == null && right == null ) {
System.out.print( data.toString() );
}
else if ( left != null && right != null ) {
System.out.print( \" \" + data.toString() + \" \" );
}
// Finally we traverse the the right tree
if ( right != null ) {
right.inOrder();
if ( left != null ) {
System.out.print( \")\" );
}
}
}
// The next function will return return true if the current node contains a binary operator and false otherwise.
private boolean tocheckkisBinaryOperator () {
return (left != null && right != null);
}
static public booleanexpressionTree read() throws booleanexpressionTreeException,
IOException {
if ( lexical == null ) {
lexical = new StreamTokenizer(
new InputStreamReader( System.in ) );
}
// Finally we will have the the return value
booleanexpressionTree createtree = null;
if ( lexical.nextToken() == StreamTokenizer.ttO_EOF ) {
return null;
}
switch ( lexical.ttype ) {
case \'&\':
createtree = new booleanexpressionTree( \'&\', read(), read() );
break;
case \'|\':
createtree = new booleanexpressionTree( \'|\', read(), read() );
break;
case \'!\':
createtree = new booleanexpressionTree( \'!\', read() );
break;
case StreamTokenizer.ttO_WORD:
createtree = new booleanexpressionTree( lexical.sval );
break;
default:
break;
}


