I am working on a Java project to involving quadtrees.
In this representation of the quadtree, the node with intensity -1 has 4 children and the node with any other intensity does not have children. I am given the preorder traversal of the quadtree and asked to convert it into the tree itself. The preorder traversal is provided to me in a file (each line containing one number). I keep getting into a stack overflow error because in my method I am using recursion and the file has 79,917 numbers.
This is the method I wrote. It works on smaller files/ But crashes on larger files.
In this method it receives an array containing the preorder traversal of the quadtree
public void createQT(Integer[] numbers){
createQT(numbers, 1, root);
}
private void createQT(Integer[] numbers, int index, QTNode currentParent) {
if (index >= numbers.length || numbers[index] == null) {
return;
}
QTNode node = new QTNode();
if (numbers[index] != -1) {
node.setIntensity(numbers[index]);
currentParent.addChild(node);
createQT(numbers, index + 1, currentParent);
} else {
currentParent.addChild(node);
index++;
for (int i = 0; i < 4; i++) {
createQT(numbers, index, node);
index++;
}
}
}
numbers. How do you initialiseroot?