// exchange a[i] and a[j] private static void draw(String[] a, int row, int ith, int min) { StdDraw.setPenColor(StdDraw.BLACK); StdDraw.text(-2.50, row, ith + ""); StdDraw.text(-1.25, row, min + ""); for (int i = 0; i < a.length; i++) { if (i == min) StdDraw.setPenColor(StdDraw.BOOK_RED); else if (i < ith) StdDraw.setPenColor(StdDraw.LIGHT_GRAY); else StdDraw.setPenColor(StdDraw.BLACK); StdDraw.text(i, row, a[i] + ""); } }
private static void show(double[] a, int i, int j) { StdDraw.setYscale(-a.length + i + 1, i); StdDraw.setPenColor(StdDraw.LIGHT_GRAY); for (int k = 0; k < j; k++) StdDraw.line(k, 0, k, a[k] * .6); StdDraw.setPenColor(StdDraw.BOOK_RED); StdDraw.line(j, 0, j, a[j] * .6); StdDraw.setPenColor(StdDraw.BLACK); for (int k = j + 1; k < i; k++) StdDraw.line(k, 0, k, a[k] * .6); StdDraw.setPenColor(StdDraw.LIGHT_GRAY); for (int k = i + 1; k < a.length; k++) StdDraw.line(k, 0, k, a[k] * .6); }
// display header private static void header(String[] a) { int N = a.length; StdDraw.setPenColor(StdDraw.BLACK); StdDraw.text(N / 2.0, -3, "a[ ]"); for (int i = 0; i < N; i++) StdDraw.text(i, -2, i + ""); StdDraw.text(-2.50, -2, "i"); StdDraw.text(-1.25, -2, "min"); StdDraw.setPenColor(StdDraw.BOOK_RED); StdDraw.line(-3, -1.65, N - .5, -1.65); StdDraw.setPenColor(StdDraw.BLACK); for (int i = 0; i < a.length; i++) StdDraw.text(i, -1, a[i]); }
/** * Draws the subtree rooted at node. * * @param y y coordinate at which to draw node. * @param yDecrement amount to decrease y coordinate for each level of the tree. * @param left left boundary of drawing space for this subtree. * @param right right boundary of drawing space for this subtree. */ protected void drawSubtree(Node node, double y, double yDecrement, double left, double right) { double mid = (left + right) / 2; double leftTip = (mid + left) / 2; double rightTip = (mid + right) / 2; double bottom = y - yDecrement; line(mid, y, leftTip, bottom); line(mid, y, rightTip, bottom); setPenColor(WHITE); filledCircle(mid, y, CIRCLE_RADIUS); filledCircle(leftTip, bottom, CIRCLE_RADIUS); filledCircle(rightTip, bottom, CIRCLE_RADIUS); setPenColor(); circle(mid, y, CIRCLE_RADIUS); circle(leftTip, bottom, CIRCLE_RADIUS); circle(rightTip, bottom, CIRCLE_RADIUS); if (node == game.getCurrentNode()) { setPenColor(RED); filledCircle(mid, y, CIRCLE_RADIUS + 0.002); setPenColor(); } if (!node.getLeft().isLeaf()) { drawSubtree(node.getLeft(), bottom, yDecrement, left, mid); } else if (node.getLeft() == game.getCurrentNode()) { setPenColor(RED); filledCircle(leftTip, bottom, CIRCLE_RADIUS + 0.002); setPenColor(); } if (!node.getRight().isLeaf()) { drawSubtree(node.getRight(), bottom, yDecrement, mid, right); } else if (node.getRight() == game.getCurrentNode()) { setPenColor(RED); filledCircle(rightTip, bottom, CIRCLE_RADIUS + 0.002); setPenColor(); } }
// display footer private static void footer(String[] a) { int N = a.length; StdDraw.setPenColor(StdDraw.BLACK); for (int i = 0; i < a.length; i++) StdDraw.text(i, N, a[i]); }