예제 #1
0
  public void testPreorder() {
    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("a b g k c d e f h j i ", tree.preorder());
  }
예제 #2
0
  public void testFind() {
    RegBST<Integer, String> tree = genRemove();

    assertEquals(null, tree.find(100));
    assertEquals(null, tree.find(1));
    assertEquals("h", tree.find(10));
    assertEquals("g", tree.find(3));
    assertEquals("a", tree.find(30));
    assertEquals("d", tree.find(5));
    assertEquals("e", tree.find(45));
    assertEquals("k", tree.find(60));
    assertEquals("q", tree.find(62));
  }
예제 #3
0
  public RegBST<Integer, String> genRemove() {
    RegBST<Integer, String> tree = new RegBST<Integer, String>();

    BSTNode<Integer, String> a = new BSTNode<Integer, String>(30, "a");
    BSTNode<Integer, String> b = new BSTNode<Integer, String>(17, "b");
    BSTNode<Integer, String> c = new BSTNode<Integer, String>(55, "c");
    BSTNode<Integer, String> d = new BSTNode<Integer, String>(5, "d");
    BSTNode<Integer, String> e = new BSTNode<Integer, String>(45, "e");
    BSTNode<Integer, String> f = new BSTNode<Integer, String>(67, "f");
    BSTNode<Integer, String> g = new BSTNode<Integer, String>(3, "g");
    BSTNode<Integer, String> h = new BSTNode<Integer, String>(10, "h");
    BSTNode<Integer, String> i = new BSTNode<Integer, String>(31, "i");
    BSTNode<Integer, String> j = new BSTNode<Integer, String>(47, "j");
    BSTNode<Integer, String> k = new BSTNode<Integer, String>(60, "k");
    BSTNode<Integer, String> l = new BSTNode<Integer, String>(4, "l");
    BSTNode<Integer, String> m = new BSTNode<Integer, String>(9, "m");
    BSTNode<Integer, String> n = new BSTNode<Integer, String>(46, "n");
    BSTNode<Integer, String> o = new BSTNode<Integer, String>(58, "o");
    BSTNode<Integer, String> p = new BSTNode<Integer, String>(63, "p");
    BSTNode<Integer, String> q = new BSTNode<Integer, String>(62, "q");

    a.setLeft(b);
    a.setRight(c);
    b.setLeft(d);
    c.setLeft(e);
    c.setRight(f);
    d.setLeft(g);
    d.setRight(h);
    e.setLeft(i);
    e.setRight(j);
    f.setLeft(k);
    g.setRight(l);
    h.setLeft(m);
    j.setLeft(n);
    k.setLeft(o);
    k.setRight(p);
    p.setLeft(q);

    tree.setRoot(a);

    return tree;
  }
예제 #4
0
  public void testGetSucc() {
    RegBST<Integer, String> tree = genRemove();

    assertEquals("31 ", tree.getSucc(tree.getRoot()).toString());

    // Removing 58
    tree.getRoot().getRight().getRight().getLeft().setLeft(null);

    assertEquals("60 ", tree.getSucc(tree.getRoot().getRight()).toString());
  }
예제 #5
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());
  }
예제 #6
0
  public void testFindWithRemove() {
    RegBST<Integer, String> tree = genRemove();

    tree.remove(55);

    assertEquals(null, tree.find(55));
    assertEquals("o", tree.find(58));
    assertEquals("o", tree.getRoot().getRight().getValue());

    tree.remove(58);

    assertEquals(null, tree.find(55));
    assertEquals(null, tree.find(58));
    assertEquals("k", tree.find(60));
    assertEquals("k", tree.getRoot().getRight().getValue());

    tree.remove(17);

    assertEquals(null, tree.find(17));
    assertEquals("d", tree.find(5));
    assertEquals("d", tree.getRoot().getLeft().getValue());
  }
예제 #7
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());
  }