📄 bintree.java
字号:
tmp2X += (tmp3X - src2)/5; redraw(); } tmp1Str = new String(); tmp2Str = new String(); tmp3Str = new String(""+total); redraw(); } // highlightLeftRightSubtree() public void tree2Matrices(int left, int right) { if (drawingPanel.getNoAnim()) return; int dest1X = drawingPanel.getOffset() + (left+1)*(horizSpace+2); int dest1Y = drawingPanel.getOffset() + 10 + (right+1)*(vertSpace+2); int dest2X = drawingPanel.getOffset() + 380 + (left+1)*(horizSpace+2); int dest2Y = drawingPanel.getOffset() + 10 + (right+1)*(vertSpace+2); tmp3X -= 10; redraw(); tmp3X -= 10; redraw(); tmp3X -= 10; redraw(); tmp3X -= 10; redraw(); tmp1Str = new String(((Node)nodeList.firstElement()).getLabel()); tmp1X = tmp3X; tmp1Y = tmp3Y; tmp1X += 10; redraw(); tmp1X += 10; redraw(); tmp1X += 10; redraw(); tmp1X += 10; redraw(); int src1X = tmp3X; int src1Y = tmp3Y; int src2X = tmp1X; int src2Y = tmp1Y; for (int i = 0; i < 10; i++) { tmp3X += (dest1X - src1X)/10; tmp3Y += (dest1Y - src1Y)/10; tmp1X += (dest2X - src2X)/10; tmp1Y += (dest2Y - src2Y)/10; redraw(); } tmp3X = dest1X; tmp3Y = dest1Y; tmp1X = dest2X; tmp1Y = dest2Y; redraw(); tmp3Str = new String(); tmp1Str = new String(); } /* ------------------------ Utils -------------------- */ /** * Performs the exponent of <code>num</code> to the power of * <code>pow</code>. * @param num Base. * @param pow Exponent. */ public int power(int num, int pow) { int result = 1; for (int i = 0; i < pow; i++) result *= num; return result; } /** * @return The position of the parent of node i. * @param i The node, whose parent is of interest. */ public int parent(int i) { return (i/2); } /** * Returns the left child of node i. */ public int left(int i) { return (2*i); } /** * Returns the right child of node i. */ public int right(int i) { return (2*i + 1); } /* ------------------------ Graphical Primitives ------------------ */ /** * Draws all graphical objects within this class. * @param g Graphical context. */ public void draw(Graphics g) { drawTree(g); } /** * Draws all graphical objects within this class. * @param g Graphical context. */ public void drawTree(Graphics g) { if (tmp1Str.length() > 0) drawBox(g, tmp1X, tmp1Y, tmp1Str, Color.white, Color.black, fixFont); if (tmp2Str.length() > 0) drawBox(g, tmp2X, tmp2Y, tmp2Str, Color.white, Color.black, fixFont); if (tmp3Str.length() > 0) drawBox(g, tmp3X, tmp3Y, tmp3Str, Color.white, Color.black, fixFont); for (int i = 0; i < nodeList.size(); i++) { Node node = (Node)nodeList.elementAt(i); drawNode(g, node); } if (movingNode.getWeight() > -1) drawNode(g, movingNode); } public void drawBox(Graphics g, int x, int y, String str, Color fg, Color bg, Font font) { String blank = new String(); for (int i = 0; i < 4-str.length(); i++) blank = blank.concat(" "); g.setColor(bg); g.fillRect(x, y, horizSpace, vertSpace); g.setColor(Color.black); g.drawRect(x, y, horizSpace, vertSpace); g.setColor(fg); g.setFont(font); g.drawString(blank + str, x + 2, y + vertSpace - 4); } /** * Draw an arrowed line segment using the array of points. * @param g Graphical context * @param points The vortices of the line segment. * An arrow head will be drawn using the last point in the array at the tip. */ public void drawArrow(Graphics g, Point[] points) { // remember: no drawPolyline in jdk1.0.* int[] axPoints = new int[3], ayPoints = new int[3]; g.setColor( Color.magenta ); if (points.length < 3) { Point endArrow = points[1], startArrow = points[0]; if (endArrow.y == startArrow.y) { g.drawLine(startArrow.x, startArrow.y, endArrow.x, endArrow.y); axPoints[0] = endArrow.x; ayPoints[0] = endArrow.y; int unit = (endArrow.x - startArrow.x)/ Math.abs(endArrow.x - startArrow.x); axPoints[1] = endArrow.x - unit*2; ayPoints[1] = endArrow.y - 2; axPoints[2] = endArrow.x - unit*2; ayPoints[2] = endArrow.y + 2; g.fillPolygon(axPoints, ayPoints, 3); } else if (endArrow.x == startArrow.x) { g.drawLine(startArrow.x, startArrow.y, endArrow.x, endArrow.y); axPoints[0] = endArrow.x; ayPoints[0] = endArrow.y; int unit = (endArrow.y - startArrow.y)/ Math.abs(endArrow.y - startArrow.y); axPoints[1] = endArrow.x - 1; ayPoints[1] = endArrow.y - unit*2; axPoints[2] = endArrow.x + 2; ayPoints[2] = endArrow.y - unit*2; g.fillPolygon(axPoints, ayPoints, 3); } } else if (points.length == 3) { // draw a line for the first and second points then use // drawArrow to draw the remaining segment g.drawLine(points[0].x, points[0].y, points[1].x, points[1].y); Point[] line = new Point[2]; line[0] = points[1]; line[1] = points[2]; drawArrow(g, line); } } /** * @param node The root node of the tree * @return The width of the tree in the number of pixels. */ public int treeWidth(Node node) { return (rightMostPosn(node) - leftMostPosn(node)); } /** * @param node The root node of the tree * @return The left most position of the tree */ public int leftMostPosn(Node node) { if (node.isLeaf()) return node.getX(); else return leftMostPosn(node.getLeftNode()); } /** * @param node The root node of the tree * @return The right most position of the tree */ public int rightMostPosn(Node node) { if (node.isLeaf()) return (node.getX() + 20); else return rightMostPosn(node.getRightNode()); } /** * @param node The root node of the tree * @return The height of the tree in the number of pixels. */ public int treeHeight(Node node) { return (bottomMostPosn(node) - node.getY()); } /** * @param node The root node of the tree * @return The bottom most position of the tree */ public int bottomMostPosn(Node node) { if (node == null) return 0; if (node.isLeaf()) return (node.getY() + 30); else { int rightBottom = bottomMostPosn(node.getRightNode()); int leftBottom = bottomMostPosn(node.getLeftNode()); return (rightBottom > leftBottom ? rightBottom : leftBottom); } } /** * Drawing a leaf node of the heap. * @param g Graphical context * @param node The node to be drawn */ public void drawLeafNode(Graphics g, Node node) { int x = node.getX(); int y = node.getY(); int weight = node.getWeight(); if (node.highlight) g.setColor(Color.black); else g.setColor( Color.blue ); g.fillRect(x, y, 20, 30); g.setColor(Color.black); g.drawRect(x, y, 20, 30); if (node.getLabel().length() > 0) { g.setColor( Color.yellow ); g.setFont( hugeFont ); g.drawString(node.getLabel(), node.getX() + 5, node.getY() + 12); } g.setColor( Color.white ); g.setFont( bigFont ); g.drawString(""+node.getWeight(), node.getX() + 2, node.getY()+27); } /** * Draw a node of the array representation of the heap. * @param g Graphical context * @param x The x coordinate of the top left corner of the node * @param y The y coordinate of the top left corner of the node * @param node The node to be drawn */ public void drawArrayNode(Graphics g, int x, int y, Node node) { int weight = node.getWeight(); if (node.highlight) g.setColor(Color.black); else g.setColor( Color.red ); g.fillRect(x, y, 20, 30); g.setColor(Color.black); g.drawRect(x, y, 20, 30); g.setFont(hugeFont); g.setColor( Color.yellow ); g.drawString(""+weight, x+2, y+25); } /** * Drawing a node based on the input parameters. * @param g Graphical context * @param node The node to be drawn */ public void drawNode(Graphics g, Node node) { if (node.highlight) g.setColor(Color.black); else g.setColor( darkgreen ); g.fillRect(node.getX(), node.getY(), 20, 30); g.setColor(Color.black); g.drawRect(node.getX(), node.getY(), 20, 30); if (node.getLabel().length() > 0) { g.setColor( Color.yellow ); g.setFont( hugeFont ); g.drawString(node.getLabel(), node.getX() + 5, node.getY() + 12); } g.setColor( Color.white ); g.setFont( bigFont ); g.drawString(""+node.getWeight(), node.getX() + 2, node.getY()+27); // draw links to children if (node.getLeftNode() != null) { g.setColor( Color.black ); g.drawLine(node.getX() + 6, node.getY() + 30, node.getLeftNode().getX() + 10, node.getLeftNode().getY()); if (node.highlightLeft) { g.drawLine(node.getX() + 5, node.getY() + 30, node.getLeftNode().getX() + 9, node.getLeftNode().getY()); g.drawLine(node.getX() + 4, node.getY() + 30, node.getLeftNode().getX() + 8, node.getLeftNode().getY()); } } if (node.getRightNode() != null) { g.setColor( Color.black ); g.drawLine(node.getX() + 14, node.getY() + 30, node.getRightNode().getX() + 10, node.getRightNode().getY()); if (node.highlightRight) { g.drawLine(node.getX() + 15, node.getY() + 30, node.getRightNode().getX() + 11, node.getRightNode().getY()); g.drawLine(node.getX() + 16, node.getY() + 30, node.getRightNode().getX() + 12, node.getRightNode().getY()); } } } /** * Move the tree starting with node dx pixels to the right and dy * pixels down. * @param node The root node of the tree to be moved. * @param dx The change in x direction. * @param dy The change in y direction. */ public void moveNode(Node node, int dx, int dy) { if (node == null) return; node.moveNode(dx, dy); } public void highlightNode(Node node) { node.highlight = true; if (node.getLeftNode() != null) highlightNode(node.getLeftNode()); if (node.getRightNode() != null) highlightNode(node.getRightNode()); } public void restoreNode(Node node) { node.highlight = false; if (node.getLeftNode() != null) restoreNode(node.getLeftNode()); if (node.getRightNode() != null) restoreNode(node.getRightNode()); } public Node getNodeAt(int posn) { Node posnNode = (Node)posnList.elementAt(posn-1); for (int i = 0; i < nodeList.size(); i++) { Node node = (Node)nodeList.elementAt(i); if (node.getX() == posnNode.getX() && node.getY() == posnNode.getY()) { return node; } } return null; } public int sumNodeWeight(Node node) { if (node == null) return 0; return (sumNodeWeight(node.getRightNode()) + sumNodeWeight(node.getLeftNode()) + node.getWeight()); } Node root = null; public void setRoot(Node node) { this.root = node; } public int getX() { return getNodeAt(1).getX(); } public int getY() { return getNodeAt(1).getY(); } public void move(int x, int y) { Point orig = new Point(0, 0); if (root != null) { orig.x = root.getX(); orig.y = root.getY(); root.move(x, y); } else { root = (Node)nodeList.firstElement(); if (root != null) { orig.x = root.getX(); orig.y = root.getY(); root.move(x, y); } } if (root != null) { int dx = x - orig.x; int dy = y - orig.y; if (tmp1Str.length() > 0) { tmp1X += dx; tmp1Y += dy; } if (tmp2Str.length() > 0) { tmp2X += dx; tmp2Y += dy; } if (tmp3Str.length() > 0) { tmp3X += dx; tmp3Y += dy; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -