예제 #1
0
  public void testInorder() {
    BSTNode<String, String> one = new BSTNode<String, String>("a", "1");
    BSTNode<String, String> two = new BSTNode<String, String>("b", "2");
    BSTNode<String, String> three = new BSTNode<String, String>("c", "3");
    BSTNode<String, String> four = new BSTNode<String, String>("d", "4");
    BSTNode<String, String> five = new BSTNode<String, String>("e", "5");
    BSTNode<String, String> six = new BSTNode<String, String>("f", "6");
    BSTNode<String, String> seven = new BSTNode<String, String>("g", "7");
    BSTNode<String, String> eight = new BSTNode<String, String>("h", "8");
    BSTNode<String, String> nine = new BSTNode<String, String>("i", "9");
    BSTNode<String, String> ten = new BSTNode<String, String>("j", "10");
    BSTNode<String, String> eleven = new BSTNode<String, String>("k", "11");

    one.setLeft(two);
    one.setRight(three);
    two.setRight(seven);
    three.setLeft(four);
    three.setRight(six);
    four.setRight(five);
    six.setLeft(eight);
    six.setRight(nine);
    seven.setLeft(eleven);
    eight.setLeft(ten);

    RegBST<String, String> tree = new RegBST<String, String>();
    tree.setRoot(one);

    assertEquals("b k g a d e c j h f i ", tree.inorder());
  }
예제 #2
0
  public void testInsert() {
    RegBST<Integer, String> tree = new RegBST<Integer, String>();

    tree.insert(30, "a");
    tree.insert(17, "b");
    tree.insert(55, "c");
    tree.insert(5, "d");
    tree.insert(45, "e");
    tree.insert(67, "f");
    tree.insert(3, "g");
    tree.insert(10, "h");
    tree.insert(31, "i");
    tree.insert(47, "j");
    tree.insert(60, "k");
    tree.insert(4, "l");
    tree.insert(9, "m");
    tree.insert(46, "n");
    tree.insert(58, "o");
    tree.insert(63, "p");
    tree.insert(62, "q");

    assertEquals("30 17 5 3 4 10 9 55 45 31 47 46 67 60 58 63 62 ", tree.preorder());
    assertEquals("3 4 5 9 10 17 30 31 45 46 47 55 58 60 62 63 67 ", tree.inorder());
    assertEquals("4 3 9 10 5 17 31 46 47 45 58 62 63 60 67 55 30 ", tree.postorder());
  }
예제 #3
0
  public void testRemove() {
    RegBST<Integer, String> tree = genRemove();
    tree.remove(17);

    assertEquals("30 5 3 4 10 9 55 45 31 47 46 67 60 58 63 62 ", tree.preorder());
    assertEquals("3 4 5 9 10 30 31 45 46 47 55 58 60 62 63 67 ", tree.inorder());
    assertEquals("4 3 9 10 5 31 46 47 45 58 62 63 60 67 55 30 ", tree.postorder());

    tree = genRemove();
    tree.remove(100);

    assertEquals("30 17 5 3 4 10 9 55 45 31 47 46 67 60 58 63 62 ", tree.preorder());
    assertEquals("3 4 5 9 10 17 30 31 45 46 47 55 58 60 62 63 67 ", tree.inorder());
    assertEquals("4 3 9 10 5 17 31 46 47 45 58 62 63 60 67 55 30 ", tree.postorder());

    tree = genRemove();
    tree.remove(67);

    assertEquals("30 17 5 3 4 10 9 55 45 31 47 46 60 58 63 62 ", tree.preorder());
    assertEquals("3 4 5 9 10 17 30 31 45 46 47 55 58 60 62 63 ", tree.inorder());
    assertEquals("4 3 9 10 5 17 31 46 47 45 58 62 63 60 55 30 ", tree.postorder());

    tree = genRemove();
    tree.remove(3);
    tree.remove(4);
    tree.remove(5);

    assertEquals("30 17 10 9 55 45 31 47 46 67 60 58 63 62 ", tree.preorder());
    assertEquals("9 10 17 30 31 45 46 47 55 58 60 62 63 67 ", tree.inorder());
    assertEquals("9 10 17 31 46 47 45 58 62 63 60 67 55 30 ", tree.postorder());

    tree = genRemove();
    tree.remove(30);

    assertEquals("31 17 5 3 4 10 9 55 45 47 46 67 60 58 63 62 ", tree.preorder());
    assertEquals("3 4 5 9 10 17 31 45 46 47 55 58 60 62 63 67 ", tree.inorder());
    assertEquals("4 3 9 10 5 17 46 47 45 58 62 63 60 67 55 31 ", tree.postorder());

    tree = genRemove();
    tree.remove(55);
    tree.remove(58);

    assertEquals("30 17 5 3 4 10 9 60 45 31 47 46 67 63 62 ", tree.preorder());
    assertEquals("3 4 5 9 10 17 30 31 45 46 47 60 62 63 67 ", tree.inorder());
    assertEquals("4 3 9 10 5 17 31 46 47 45 62 63 67 60 30 ", tree.postorder());
  }