Java Binary Tree Methods for World Entity Management

Classified in Computers

Written on in English with a size of 4.28 KB

Practica 6: World Class Implementation

This implementation focuses on managing entities within a World data structure using a binary tree approach.

Method: insert

The insert method handles the placement of an Entity at a specific address.

public void insert(String address, Entity e) {
    if (content == null) escribirContenido(content, e, address);
    if (address.equals("")) escribirContenido(content, e, address);
    else {
        if (address.substring(0, 1).equals("L")) {
            address = address.substring(1, address.length());
            travelLeft().insert(address, e);
        } else {
            address = address.substring(1, address.length());
            travelRight().insert(address, e);
        }
    }
}

Method: escribirContenido

A private helper method to write content into the LinkedList of entities.

private void escribirContenido(LinkedList<Entity> listaContent, Entity e, String address) {
    int contador = 0;
    if (listaContent == null) {
        LinkedList<Entity> contenido = new LinkedList<Entity>();
        if (!address.equals("")) contenido.add(Entity.forests(1));
        this.setContent(contenido);
        setLeft(new World());
        setRight(new World());
    } else {
        for (int i = 0; i < listaContent.size(); i++) {
            if (listaContent.get(i).getType() == e.getType()) {
                listaContent.get(i).setCount(listaContent.get(i).getCount() + e.getCount());
                contador++;
            }
        }
        if (contador == 0) this.content.add(e);
    }
}

Method: countCastlesCloserThan

Calculates the number of castles within a specified distance.

public long countCastlesCloserThan(long distance) {
    int posicion;
    if (isEmpty() || distance < 0) return 0;
    if (content.contains(Entity.castles(1))) {
        posicion = content.indexOf(Entity.castles(1));
        long contador = content.get(posicion).getCount();
        return contador + travelLeft().countCastlesCloserThanRecIzda(--distance) + travelRight().countCastlesCloserThanRecDcha(--distance);
    } else {
        return travelLeft().countCastlesCloserThanRecIzda(--distance) + travelRight().countCastlesCloserThanRecDcha(--distance);
    }
}

Recursive Helper: countCastlesCloserThanRecIzda

private long countCastlesCloserThanRecIzda(long distance) {
    int posicion;
    if (isEmpty() || distance < 0) return 0;
    else {
        if (content.contains(Entity.castles(1))) {
            posicion = content.indexOf(Entity.castles(1));
            long contador = content.get(posicion).getCount();
            return contador + travelLeft().countCastlesCloserThanRecIzda(--distance) + travelRight().countCastlesCloserThanRecDcha(--distance);
        } else {
            return travelLeft().countCastlesCloserThanRecIzda(--distance) + travelRight().countCastlesCloserThanRecDcha(--distance);
        }
    }
}

Recursive Helper: countCastlesCloserThanRecDcha

private long countCastlesCloserThanRecDcha(long distance) {
    int posicion;
    if (isEmpty() || distance < 0) return 0;
    else {
        if (content.contains(Entity.castles(1))) {
            posicion = content.indexOf(Entity.castles(1));
            long contador = content.get(posicion).getCount();
            return contador + travelLeft().countCastlesCloserThanRecIzda(--distance) + travelRight().countCastlesCloserThanRecDcha(--distance);
        } else {
            return travelLeft().countCastlesCloserThanRecIzda(--distance) + travelRight().countCastlesCloserThanRecDcha(--distance);
        }
    }
}

Method: countAtLevel

Counts the total number of entities of a specific type at a given level (n).

public long countAtLevel(int type, int n) {
    int contador = 0;
    if (isEmpty()) return 0;
    for (int i = 0; i < content.size(); i++) {
        if (content.get(i).getType() == type) contador += content.get(i).getCount();
    }
    contador += travelLeft().countAtLevel(type, --n) + travelRight().countAtLevel(type, --n);
    return contador;
}

Related entries: