I'm currently working on a schoolproject that will parse text files that contains different instruction of how to move. These instructions are then placed out in a tree structure that always bransch to the right with an new instruction and to the left with how much each instruction can go.
It will looks something like this:
FORW
/ \
2 LEFT
/ \
90 Rep
/ \
FORW FORW
/ /
4 2
Anyways without going into to much more details on the specifics of my program my question to you is how I can improve this part of the program to make it become faster?
As you can see I have tried using the stringbuilder to speed up the process but it didn't do much different and at this point I'm kind of out of ideas so any help would be greatly appreciated.
This program is tested with alot of test cases and if any of them takes longer than 7 seconds it fails and that is now the case.
import java.util.ArrayList;
// Ett syntaxträd
public class CreateLines{
private boolean penDown; // 1 om pennan är nere 0 om inte.
private double x1, x2, y1, y2; // x,y-koordinat
private int degrees; // vinkel v
private String color; // nuvarande färg
private double piDegrees;
private double decimals;
private ArrayList<String> result;
public CreateLines() {
penDown = false;
x1 = 0;
x2 = 0;
y1 = 0;
y2 = 0;
degrees = 0;
color = "#0000FF";
// For optimization.
decimals = 100000;
}
public StringBuilder treeSearch (Operation tree) {
// Some variables we use down here:
double x1 = this.x1;
double y1 = this.y1;
double x2;
double y2;
int numberNode;
StringBuilder str = new StringBuilder();
switch (tree.operation) {
case FORW:
piDegrees = Math.PI*degrees/180;
numberNode = tree.evaluate();
x2 = x1 + (numberNode * Math.cos(piDegrees));
y2 = y1 + (numberNode * Math.sin(piDegrees));
x2 = (double)Math.rint(x2 * decimals) / decimals;
y2 = (double)Math.rint(y2 * decimals) / decimals;
this.x1 = x2;
this.x2 = x2;
this.y1 = y2;
this.y2 = y2;
// If penDown then we print otherwise not.
if (penDown){
str.append(color + " " + x1 + " " + y1 + " " + x2 + " " + y2 + "\n");
if(tree.right == null){
return str;
}
return str.append(treeSearch((Operation) tree.right));
}
else{
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
case BACK:
piDegrees = Math.PI*degrees/180;
numberNode = tree.evaluate();
x2 = x1 - (numberNode * Math.cos(piDegrees));
y2 = y1 - (numberNode * Math.sin(piDegrees));
x2 = (double)Math.rint(x2 * decimals) / decimals;
y2 = (double)Math.rint(y2 * decimals) / decimals;
this.x1 = x2;
this.x2 = x2;
this.y1 = y2;
this.y2 = y2;
// If penDown then we print otherwise not.
if (penDown){
str.append(color + " " + x1 + " " + y1 + " " + x2 + " " + y2 + "\n");
if(tree.right == null){
return str;
}
return str.append(treeSearch((Operation) tree.right));
}
else{
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
case LEFT:
numberNode = tree.evaluate();
this.degrees = degrees+numberNode;
if (penDown){
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
else{
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
case RIGHT:
numberNode = tree.evaluate();
this.degrees = degrees-numberNode;
if (penDown){
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
else{
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
case DOWN:
this.penDown = true;
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
case UP:
this.penDown = false;
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
case COLOR:
this.color = tree.color.toUpperCase();
if (penDown){
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
else{
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
case REP:
// if we got a rep instruction to the left we
if(tree.right == null){
for(int i = 0; i < tree.rep; i++){
str.append(treeSearch((Operation) tree.left));
}
return str;
}
else {
for(int i = 0; i < tree.rep; i++){
str.append(treeSearch((Operation) tree.left));
}
return str.append(treeSearch((Operation)tree.right));
}
}
assert false; // borde aldrig kunna hända
return null;
}
}