@Test public void deepSort() throws Exception { SortingNode<Integer> tree = new SortingNode.Builder<Integer>() .childNode(200) .siblingNode(100) .childNode(110) .siblingNode(120) .parent() .siblingNode(400) .childNode(410) .siblingNode(430) .childNode(431) .parent() .siblingNode(420) .parent() .siblingNode(300) .build(); tree = tree.deepSort((a, b) -> b.getValue() - a.getValue()); String expected = "" + "*\n" + " 400\n" + " 430\n" + " 431\n" + " 420\n" + " 410\n" + " 300\n" + " 200\n" + " 100\n" + " 120\n" + " 110"; assertEquals(expected, tree.prettyPrint()); }
public SortingNode<T> deepSort(Comparator<SortingNode<T>> comparator) { if (!hasChild()) { return this; } ImmutableList<SortingNode<T>> sorted = children.stream() .map(child -> child.deepSort(comparator)) .sorted(comparator) .collect(MoreCollectors.toImmutableList()); return new SortingNode<>(value, parent, sorted); }
SortingNode { public SortingNode<T> deepSort(Comparator<SortingNode<T>> comparator) { if (!hasChild()) { return this; } ImmutableList<SortingNode<T>> sorted = children.stream() .map(child -> child.deepSort(comparator)) .sorted(comparator) .collect(MoreCollectors.toImmutableList()); return new SortingNode<>(value, parent, sorted); } }
SortingNode { public SortingNode<T> deepSort(Comparator<SortingNode<T>> comparator) { if (!hasChild()) { return this; } ImmutableList<SortingNode<T>> sorted = children.stream() .map(child -> child.deepSort(comparator)) .sorted(comparator) .collect(MoreCollectors.toImmutableList()); return new SortingNode<>(value, parent, sorted); } private  SortingNode(@Nullable T value,
      @Nullable SortingNode<T> parent,
      Stream<UnaryOperator<SortingNode<T>>> childrenFactory); private  SortingNode(@Nullable T value,
      @Nullable SortingNode<T> parent,
      ImmutableList<SortingNode<T>> children); }
SortingNode { public SortingNode<T> deepSort(Comparator<SortingNode<T>> comparator) { if (!hasChild()) { return this; } ImmutableList<SortingNode<T>> sorted = children.stream() .map(child -> child.deepSort(comparator)) .sorted(comparator) .collect(MoreCollectors.toImmutableList()); return new SortingNode<>(value, parent, sorted); } private  SortingNode(@Nullable T value,
      @Nullable SortingNode<T> parent,
      Stream<UnaryOperator<SortingNode<T>>> childrenFactory); private  SortingNode(@Nullable T value,
      @Nullable SortingNode<T> parent,
      ImmutableList<SortingNode<T>> children); @SuppressWarnings("unchecked") static SortingNode<T> emptyRoot(); @Override String toString(); @Nullable T getValue(); @Nullable SortingNode<T> getParent(); boolean hasParent(); boolean isRoot(); List<SortingNode<T>> getChildren(); boolean hasChild(); SortingNode<T> deepSort(Comparator<SortingNode<T>> comparator); String prettyPrint(); Stream<T> depthFirst(); Stream<T> breathFirst(); }
SortingNode { public SortingNode<T> deepSort(Comparator<SortingNode<T>> comparator) { if (!hasChild()) { return this; } ImmutableList<SortingNode<T>> sorted = children.stream() .map(child -> child.deepSort(comparator)) .sorted(comparator) .collect(MoreCollectors.toImmutableList()); return new SortingNode<>(value, parent, sorted); } private  SortingNode(@Nullable T value,
      @Nullable SortingNode<T> parent,
      Stream<UnaryOperator<SortingNode<T>>> childrenFactory); private  SortingNode(@Nullable T value,
      @Nullable SortingNode<T> parent,
      ImmutableList<SortingNode<T>> children); @SuppressWarnings("unchecked") static SortingNode<T> emptyRoot(); @Override String toString(); @Nullable T getValue(); @Nullable SortingNode<T> getParent(); boolean hasParent(); boolean isRoot(); List<SortingNode<T>> getChildren(); boolean hasChild(); SortingNode<T> deepSort(Comparator<SortingNode<T>> comparator); String prettyPrint(); Stream<T> depthFirst(); Stream<T> breathFirst();  }