Exemple #1
0
  public String toString() {
    int size = __getSize();
    int height = __getHeight();
    int widest = __getWidest() + 1;

    IntTreeNode.clearCycleData(20);
    String result = toString(overallRoot, 0, 0, size, height, widest);

    if (overallRoot == null) {
      result = "overallRoot\n   null";
    } else {
      String firstLine = "";
      int nodesLeft = __getSize(overallRoot.left);
      if (overallRoot.left != null && (overallRoot.cycle() || overallRoot.left.cycle())) {
        // nodesLeft = 0;
      }
      int spaces = (nodesLeft * widest) - Math.max(0, "overallRoot".length() - widest + 1) / 2;
      for (int i = 0; i < spaces; i++) {
        firstLine += " ";
      }
      firstLine += "overallRoot\n";

      int len = result.length();
      while (len < firstLine.length()) {
        result = " " + result;
        len += 2;
      }

      result = firstLine + result;
    }

    return result;
  }
Exemple #2
0
 // post: prints in postorder the data fields of the tree with given root
 private void printPostOrder(IntTreeNode root) {
   if (root != null) {
     printPostOrder(root.__gotoLeft());
     printPostOrder(root.__gotoRight());
     System.out.print(" " + root.data);
   }
 }
Exemple #3
0
 private boolean equals(IntTreeNode root1, IntTreeNode root2) {
   if (root1 == null || root2 == null) {
     return root1 == root2;
   } else {
     return root1.data == root2.data
         && (root1.left == root2.left || equals(root1.__gotoLeft(), root2.__gotoLeft()))
         && (root1.right == root2.right || equals(root1.__gotoRight(), root2.__gotoRight()));
   }
 }
Exemple #4
0
 // post: prints in preorder the data fields of the given tree indenting
 //       each line to the given level
 private void printStructure(IntTreeNode root, int level) {
   if (root != null) {
     printStructure(root.__gotoRight(), level + 1);
     for (int i = 0; i < level; i++) {
       System.out.print("    ");
     }
     System.out.println(root.data);
     printStructure(root.__gotoLeft(), level + 1);
   }
 }
Exemple #5
0
 // pre : height >= 0
 // post: constructs and returns a reference to a perfect binary tree
 //       of height h with random data values between 0 and 99 inclusive
 private IntTreeNode randomTree(int h, boolean perfect) {
   if (h == 0) {
     return null;
   } else {
     int n = (int) (Math.random() * 100);
     IntTreeNode node = new IntTreeNode(n);
     if (perfect || Math.random() >= 0.75) {
       node.left = randomTree(h - 1);
     }
     if (perfect || Math.random() >= 0.75) {
       node.right = randomTree(h - 1);
     }
     return node;
   }
 }
Exemple #6
0
 private int __getSizeBetweenRight(IntTreeNode root) {
   if (root == null || root.right == null) {
     return 0;
   } else {
     return __getSize(root.__gotoRight().__gotoLeft());
   }
 }
Exemple #7
0
 public boolean equals(Object o) {
   IntTreeNode.clearCycleData();
   if (o instanceof IntTree) {
     IntTree other = (IntTree) o;
     return this.overallRoot == other.overallRoot || equals(this.overallRoot, other.overallRoot);
   } else {
     return false;
   }
 }
Exemple #8
0
 public IntTreeNode __gotoRight(boolean checkForCycle) {
   if (checkForCycle) {
     if (right != null) {
       if (right.visitsLeft > 0) {
         right.visitsLeft--;
       }
       if (right.cycle()) {
         // throw new IllegalStateException("cycle detected in tree");
       }
     }
   }
   return right;
 }
Exemple #9
0
 private int __getHeight(IntTreeNode root) {
   if (root == null) {
     return 0;
   } else {
     int leftHeight = 0;
     IntTreeNode left = root.__gotoLeft();
     if (left != null && !left.cycle()) {
       leftHeight = __getHeight(left);
     }
     int rightHeight = 0;
     IntTreeNode right = root.__gotoRight();
     if (right != null && !right.cycle()) {
       rightHeight = __getHeight(right);
     }
     return (root.cycle() ? 0 : 1) + Math.max(leftHeight, rightHeight);
   }
 }
Exemple #10
0
 private int __getSize(IntTreeNode root) {
   if (root == null) {
     return 0;
   } else {
     int leftSize = 0;
     IntTreeNode left = root.__gotoLeft();
     if (left != null && !left.cycle()) {
       leftSize = __getSize(left);
     }
     int rightSize = 0;
     IntTreeNode right = root.__gotoRight();
     if (right != null && !right.cycle()) {
       rightSize = __getSize(right);
     }
     return (root.cycle() ? 0 : 1) + leftSize + rightSize;
   }
 }
Exemple #11
0
 private int __getWidest(IntTreeNode root) {
   if (root == null) {
     return 0;
   } else {
     int width = root.toString().length();
     int widestLeft = 0;
     IntTreeNode left = root.__gotoLeft();
     if (left != null && !left.cycle()) {
       widestLeft = __getWidest(left);
     }
     int widestRight = 0;
     IntTreeNode right = root.__gotoRight();
     if (right != null && !right.cycle()) {
       widestRight = __getWidest(right);
     }
     return Math.max(width, Math.max(widestLeft, widestRight));
   }
 }
Exemple #12
0
 // post: prints the data fields of the tree using a preorder traversal
 public void printPreOrder() {
   IntTreeNode.clearCycleData();
   System.out.print("preorder:");
   printPreOrder(overallRoot);
   System.out.println();
 }
Exemple #13
0
  private String toString(
      IntTreeNode root, int sizeAboveLeft, int level, int size, int height, int width) {
    // System.out.println("toString(root, " + sizeAboveLeft + ", " + level + ", " + size + ", " +
    // height + ", " + width + ")");
    if (root == null) {
      return "";
    } else {
      String result = "";
      int sizeBelowLeft = __getSize(root.left);
      if (root.cycle()) {
        // sizeBelowLeft = 0;
      }
      int sizeLeft = sizeAboveLeft + sizeBelowLeft;

      // create line for this element
      // (must potentially put leading __ marks for left/right pointers)
      String thisElementLine = "";
      String nextLine = "";

      if (root.left == null) {
        // indent this node
        for (int i = 0; i < width * sizeAboveLeft; i++) {
          thisElementLine += " ";
          if (root.right != null) {
            nextLine += " ";
          }
        }
      } else {
        // indent this node, but insert / and _____ for left pointer
        int widthOfLeft = root.left.toString().length();
        int dWidthLeft = width - widthOfLeft;

        int betweenLeft = __getSizeBetweenLeft(root);
        if (root.cycle()) {
          // betweenLeft = 0;
        }
        for (int i = 0; i < width * (sizeLeft - betweenLeft) - dWidthLeft; i++) {
          thisElementLine += " ";
          nextLine += " ";
        }
        thisElementLine += " ";
        nextLine += "/";
        for (int i = 0; i < width * betweenLeft - 1 + dWidthLeft; i++) {
          thisElementLine += "_";
          if (root.right != null) {
            nextLine += " ";
          }
        }
      }

      thisElementLine += root;

      if (root.right != null) {
        // insert _____ and \ for right pointer
        for (int i = 0; i < root.toString().length(); i++) {
          nextLine += " ";
        }

        for (int i = 0; i < width - root.toString().length() - 1; i++) {
          thisElementLine += "_";
          nextLine += " ";
        }

        int betweenRight = root.cycle() ? 0 : __getSizeBetweenRight(root);
        for (int i = 0; i < width * betweenRight; i++) {
          thisElementLine += "_";
          nextLine += " ";
        }
        nextLine += "\\";
      }

      if (root.left == null && root.right == null) {
        result += thisElementLine;
      } else {
        thisElementLine += "\n";
        nextLine += "\n";

        // append all left elements
        String leftLines =
            root.cycle()
                ? ""
                : toString(root.__gotoLeft(), sizeAboveLeft, level + 1, size, height, width);

        // append all right elements
        String rightLines =
            root.cycle()
                ? ""
                : toString(root.__gotoRight(), sizeLeft + 1, level + 1, size, height, width);

        result += thisElementLine + nextLine + mergeLines(leftLines, rightLines);
      }

      return result;
    }
  }
Exemple #14
0
 private int __getSize() {
   IntTreeNode.clearCycleData();
   return __getSize(overallRoot);
 }
Exemple #15
0
 // post: prints the data fields of the tree, one per line, following
 //       an inorder traversal and using indentation to indicate node depth;
 //       prints right to left so that it looks correct when the output is
 //       rotated.
 public void printStructure() {
   IntTreeNode.clearCycleData();
   printStructure(overallRoot, 0);
 }
Exemple #16
0
 private int __getWidest() {
   IntTreeNode.clearCycleData();
   return __getWidest(overallRoot);
 }
Exemple #17
0
 public static void clearCycleData(int visitsAllowed) {
   for (IntTreeNode node : ALL_NODES) {
     node.visitsLeft = visitsAllowed;
   }
 }