示例#1
0
  @Test
  public void buildNodeTree() throws Exception {

    SortingNode<String> root = new SortingNode.Builder<String>().build();
    assertFalse(root.hasChild());
    assertTrue(root.isRoot());
    assertFalse(root.hasParent());

    SortingNode<Integer> tree =
        new SortingNode.Builder<Integer>() //
            .childNode(100)
            .siblingNode(200)
            .childNode(210)
            .siblingNode(220)
            .parent()
            .siblingNode(300)
            .siblingNode(400)
            .childNode(410)
            .siblingNode(420)
            .siblingNode(430)
            .childNode(431)
            .build();

    assertFalse(tree.hasParent());
    assertTrue(tree.isRoot());
    assertFalse(tree.hasParent());

    String expected =
        ""
            + "*\n"
            + "  100\n"
            + "  200\n"
            + "    210\n"
            + "    220\n"
            + "  300\n"
            + "  400\n"
            + "    410\n"
            + "    420\n"
            + "    430\n"
            + "      431";
    assertEquals(expected, tree.prettyPrint());

    SortingNode<Integer> n410 = tree.getChildren().get(3).getChildren().get(0);
    assertFalse(n410.hasChild());
    assertTrue(n410.hasParent());
    assertFalse(n410.isRoot());

    assertEquals("410", n410.prettyPrint());
  }
示例#2
0
  @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());
  }