So this is part of my AVL tree implementation, namely the infix representation of the tree. Since I wanted to use a recursive method to stay close to the original algorithm and want to return a list, I have to store the values in a parameter.
The smelly part is in getInfix when I reference that parameter to return the list. I am not even sure what's the name of this code smell but it feels wrong. I am literally only calling getInfixInternal for its side effect! But I can't simply return a list either...
How can I make this better?
private List<T> getInfix() {
List<T> result = new ArrayList<>();
getInfixInternal(root, result);
return result;
}
private void getInfixInternal(Node node, List<T> infixValues) {
if (node != null) {
getInfixInternal(node.getLeft(), infixValues);
infixValues.add((T) node.getValue());
getInfixInternal(node.getRight(), infixValues);
}
}
My Node class (excluding the getters and setters for reasons of space):
public class Node <T extends Comparable> {
private T value;
private Node leftChild;
private Node rightChild;
private Node parent;
}