Esempio n. 1
0
 // ------------------------------------------------------------
 @Test
 public void testRotation() {
   int nbOfRotations = 10000;
   for (int i = 0; i < nbOfRotations; i++) {
     BTree<Integer> t;
     int size = rnd.nextInt(10) + 6;
     do t = rndTree(size);
     while (itrWithRights.isEmpty());
     int ri = rnd.nextInt(itrWithRights.size());
     BTreeItr<Integer> ti = ((itrWithRights.get(ri)));
     String s0 = t.toReadableString();
     if (i == 0) System.out.println("original" + s0);
     if (i == 0) System.out.println("rotated left on " + ti.consult());
     ti.rotateLeft();
     String s1 = t.toReadableString();
     if (i == 0) System.out.println(s1);
     ti.rotateRight();
     String s2 = t.toReadableString();
     if (i == 0) System.out.println("then right again" + s2);
     assertTrue(s0.equals(s2));
     assertFalse(s0.equals(s1));
   }
 }
Esempio n. 2
0
 // ------------------------------------------------------------
 public BTree<Integer> rndTree(int size) {
   itrWithRights = new Vector<BTreeItr<Integer>>();
   int i = 0;
   BTree<Integer> t = new BTree<Integer>();
   BTreeItr<Integer> ti;
   int[] tab = new int[size];
   for (i = 0; i < size; i++) tab[i] = i;
   shuffle(tab, rnd);
   for (i = 0; i < size; i++) {
     ti = t.root();
     while (!ti.isBottom()) {
       if (rnd.nextInt(2) == 0) ti = ti.left();
       else ti = ti.right();
     }
     ti.insert(new Integer(tab[i]));
     if (!ti.isRoot() && !ti.isLeftArc()) itrWithRights.add(ti.up());
   }
   return t;
 }