Esempio n. 1
0
 private static TreeNode makeTree(int h) {
   if (h == 0) return null;
   else {
     TreeNode res = new TreeNode();
     nodes++;
     res.left = makeTree(h - 1);
     res.right = makeTree(h - 1);
     res.val = h;
     return res;
   }
 }
Esempio n. 2
0
 private static void replaceTreeWork(TreeNode full, TreeNode partial, boolean dir) {
   boolean canGoLeft = full.left != null && full.left.val > partial.val;
   boolean canGoRight = full.right != null && full.right.val > partial.val;
   if (canGoLeft && canGoRight) {
     if (dir) replaceTreeWork(full.left, partial, !dir);
     else replaceTreeWork(full.right, partial, !dir);
   } else if (!canGoLeft && !canGoRight) {
     if (dir) full.left = partial;
     else full.right = partial;
   } else if (!canGoLeft) {
     full.left = partial;
   } else {
     full.right = partial;
   }
 }
Esempio n. 3
0
 private static void oldGenSwapSubtrees() {
   // Randomly pick:
   //   * two tree indices
   //   * A depth
   //   * A path to that depth.
   int index1 = rnd.nextInt(trees.length);
   int index2 = rnd.nextInt(trees.length);
   int depth = rnd.nextInt(treeHeight);
   int path = rnd.nextInt();
   TreeNode tn1 = trees[index1];
   TreeNode tn2 = trees[index2];
   for (int i = 0; i < depth; i++) {
     if ((path & 1) == 0) {
       tn1 = tn1.left;
       tn2 = tn2.left;
     } else {
       tn1 = tn1.right;
       tn2 = tn2.right;
     }
     path >>= 1;
   }
   TreeNode tmp;
   if ((path & 1) == 0) {
     tmp = tn1.left;
     tn1.left = tn2.left;
     tn2.left = tmp;
   } else {
     tmp = tn1.right;
     tn1.right = tn2.right;
     tn2.right = tmp;
   }
   actuallyMut += 2;
 }