Example #1
0
  @Test
  public void depthFirst() throws Exception {

    assertEquals(
        Collections.<Integer>emptyList(),
        SortingNode.<Integer>emptyRoot().depthFirst().collect(toList()));

    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();

    assertEquals(
        asList(100, 200, 210, 220, 300, 400, 410, 420, 430, 431),
        tree.depthFirst().collect(toList()));

    SortingNode<Integer> n400 = tree.getChildren().get(3);
    assertEquals(asList(400, 410, 420, 430, 431), n400.depthFirst().collect(toList()));
  }
Example #2
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());
  }