Я работаю над проектом Java по использованию квадродеревьев.
В этом представлении квадродерева узел с интенсивностью -1 имеет 4 дочерних элемента, а узел с любой другой интенсивностью не имеет дочерних элементов. Мне дают предварительный обход квадродерева и просят преобразовать его в само дерево. Обход предварительного заказа предоставляется мне в файле (каждая строка содержит одно число). Цель состоит в том, чтобы отобразить изображение в конце.
код правильно выводит количество узлов и количество конечных узлов, но не распечатывает фотографию правильно, и я не могу обнаружить ошибку, но я думаю, что ошибка, вероятно, в методе getImageArray.
это мой код. (Обратите внимание, что изображение печатается с использованием отдельного класса, который был указан в том, что в этом классе нет ошибок)
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class ImgQuadTree {
private QTNode root;
public ImgQuadTree(String filename) {
try {
// Specify the file path
ArrayList list = new ArrayList();
Integer[] intensities;
File file = new File(filename);
// Create Scanner to read from the file
Scanner input = new Scanner(file);
// Read and print each line from the file
while (input.hasNext()) {
list.add(input.nextInt());
}
intensities = list.toArray(new Integer[list.size()]);
// Close the Scanner
input.close();
createQT(intensities);
} catch (FileNotFoundException e) {
// Handle file not found exception
System.err.println("File cannot be opened");
}
}
public void createQT(Integer[] intensities) {
// Create a queue to hold QTNode objects
Queue queue = new ArrayDeque();
// Create QTNode objects with intensities and add them to the queue
for (int intensity : intensities) {
QTNode node = new QTNode(intensity);
queue.add(node);
}
root = queue.poll();
Stack stack = new Stack();
stack.push(root);
createQT(queue, stack);
}
private void createQT(Queue queue, Stack stack) {
while (!queue.isEmpty()) {
// Get the current parent node
QTNode parent = stack.peek();
// Process the next node from the queue
QTNode node = queue.poll();
// Add the node as a child of the current parent
parent.addChild(node);
if (parent.isFull()) {
stack.pop();
}
// If the intensity of the node is -1, push it onto the stack to maintain the
// path
if (node.getIntensity() == -1) {
stack.push(node);
}
}
}
public int getNumNodes() {
// If the tree is empty, return 0
if (root == null)
return 0;
// Recursively count nodes in the left and right subtrees
int NW = getNumNodes(root.NW);
int NE = getNumNodes(root.NE);
int SW = getNumNodes(root.SW);
int SE = getNumNodes(root.SE);
// Return the total count of nodes including the root
return 1 + NW + NE + SW + SE;
}
private int getNumNodes(QTNode node) {
// If the current node is null, return 0
if (node == null)
return 0;
// Recursively count nodes in the NW, NE, SW, and SE quadrants
int NW = getNumNodes(node.NW);
int NE = getNumNodes(node.NE);
int SW = getNumNodes(node.SW);
int SE = getNumNodes(node.SE);
// Return the total count of nodes including the current node
return 1 + NW + NE + SW + SE;
}
public int getNumLeaves() {
return countLeaves(root);
}
private int countLeaves(QTNode node) {
// If the current node is null, return 0
if (node == null)
return 0;
// If the node is a leaf node (has no children), return 1
if (node.isLeaf())
return 1;
// Recursively count leaf nodes in the NW, NE, SW, and SE quadrants
int NWLeaves = countLeaves(node.NW);
int NELeaves = countLeaves(node.NE);
int SWLeaves = countLeaves(node.SW);
int SELeaves = countLeaves(node.SE);
// Return the total count of leaf nodes including the current node
return NWLeaves + NELeaves + SWLeaves + SELeaves;
}
public int[][] getImageArray() {
int[][] array = new int[256][256];
int minHeight = 0;
int maxHeight = 255;
int minWidth = 0;
int maxWidth = 255;
getImageArray(root, array, minHeight, maxHeight, minWidth, maxWidth);
return array;
}
private void getImageArray(QTNode node, int[][] array, int minHeight, int maxHeight, int minWidth, int maxWidth) {
if (node.getIntensity() != -1) {
fillQuadrant(array, maxWidth, maxHeight, node);
} else {
int midHeight = (minHeight + maxHeight) / 2;
int midWidth = (minWidth + maxWidth) / 2;
getImageArray(node.NW, array, minHeight, midHeight, minWidth, midWidth);
getImageArray(node.NE, array, minHeight, midHeight, midWidth + 1, maxWidth);
getImageArray(node.SW, array, midHeight + 1, maxHeight, minWidth, midWidth);
getImageArray(node.SE, array, midHeight + 1, maxHeight, midWidth + 1, maxWidth);
}
}
public void fillQuadrant(int[][] array, int width, int height, QTNode node) {
for (int i = 0; i
Подробнее здесь: https://stackoverflow.com/questions/783 ... age-proces