I don't understand the process. If you remove node 3, then wouldn't the next added node be the first available left child (of 1)? Why does the next added node go to 9 instead of 1?
FYI, I added a helper method called printTree() to help troubleshoot.
Another question: in all my methods, traversing the tree is basically duplicate code. Is there a way to pull this duplicate code out to another method and still retain the functionality of add(), remove(), getParent(), etc?
package com.codegym.task.task20.task2028;
import java.util.List;
public class Solution {
public static void main(String[] args) {
List<String> list = new CustomTree();
for (int i = 1; i < 16; i++) {
list.add(String.valueOf(i));
}
System.out.println("The list size is " + list.size());
System.out.println("The expected parent is 3. The actual parent is " + ((CustomTree) list).getParent("8"));
System.out.println("The expected parent is null. The actual parent is " + ((CustomTree) list).getParent("20"));
((CustomTree) list).printTree();
list.remove("3");
System.out.println("The expected parent is null. The actual parent is " + ((CustomTree) list).getParent("8"));
((CustomTree) list).printTree();
}
}