Ejemplo n.º 1
0
  @Test
  public void test() {
    List<Integer> lst = new ArrayList<Integer>();
    TreeUtil treeUtil = new TreeUtil();
    for (int k = 0; k < 50; k++) {
      lst.clear();
      // generate sorted list of different size and value.
      for (int i = 0; i < Math.random() * 100 + 1; i++) {
        lst.add((int) (Math.random() * 10));
      }
      Collections.sort(lst);
      BNode head = new BNode(), p = head;
      for (Integer num : lst) {
        BNode nd = new BNode();
        nd.value = num;
        nd.prev = p;
        p.next = nd;
        p = nd;
      }

      String s1 = head.toListStr();

      BNode tree = sortedListToBST(head, lst.size());
      Assert.assertTrue(treeUtil.isBST2(tree));
      // String s2 = tree.toTreeStr();

      BNode list = BSTToList(tree);
      String s3 = list.toListStr();

      Assert.assertEquals(s1, s3);
    }
  }
Ejemplo n.º 2
0
  @Test
  public void test() {
    TreeUtil treeUtil = new TreeUtil();

    for (String d[] :
        new String[][] { //
          //
          {"4(2(1,3),6(5,7)))", "4,2,1,3,5,7,6"}, //
          {"1(7(2,6(5,11)),8(,9(4,)))", "1,7,2,5,11,4,9,8"}, //
          {"1(2(4(8,9),5),3(6,7))", "1,2,4,8,9,5,6,7,3"}, //
          {"8(4(2(,3),5(,7)),12(9(,10),14))", "8,4,2,3,7,10,14,12"}, //
          {"0(1(,4(9,10)),2(5(11,12),))", "0,1,4,9,10,11,12,5,2"}, //
        }) {
      BNode tree = treeUtil.makeTree(d[0]);
      antiClockPrint2(tree);
      String result = StringUtils.join(prt, ",");
      System.out.println(result);
      Assert.assertEquals(d[1], result);
    }
  }