Winforms Windows窗体中二叉树的图形视图

Winforms Windows窗体中二叉树的图形视图,winforms,graphics,binary-tree,Winforms,Graphics,Binary Tree,我必须做一个二叉树的图形表示,可以选择节点…有什么想法吗 应该是这样的 稍后我可能会将其翻译成一个快速、实用的C#示例,但这是我在2000年完成的Java家庭作业! 您应该能够根据Java类收集绘制树所需的算法和代码: // FileName: DrawBinaryTree.java /************************************ * Student: Michael Tomlinson * * Course: CS 145-Sect

我必须做一个二叉树的图形表示,可以选择节点…有什么想法吗

应该是这样的

稍后我可能会将其翻译成一个快速、实用的C#示例,但这是我在2000年完成的Java家庭作业!

您应该能够根据Java类收集绘制树所需的算法和代码:

//    FileName: DrawBinaryTree.java
/************************************
 *     Student: Michael Tomlinson   *
 *      Course: CS 145-Section 1    *
 *  Instructor: Dareleen Schaffer   *
 *                                  *
 *   Assignment #4, Problem #1      *
 *    Due Date: July 24, 2000       *
 ************************************
 */
package BinaryTree;

import java.awt.*;
import javax.swing.*;

public class DrawBinaryTree {

// Data Fields
     private BinaryTree tree=null;
     private double xDiv=50, yDiv=50;
     private boolean sizeToFit=true;
     private Panel outputPanel=null;

// Constructors
     public DrawBinaryTree() {} // default constructor
     public DrawBinaryTree(BinaryTree tree) {
          setBinaryTree(tree);
     } // end constructor with (BinaryTree) parameters
     public DrawBinaryTree(Panel panel) {
          setOutputPanel(panel);
     } // end constructor with (JPanel) parameters
     public DrawBinaryTree(BinaryTree tree, Panel panel) {
          setBinaryTree(tree);
          setOutputPanel(panel);
     } // end constructor with (BinaryTree, JPanel) parameters

// Modifiers
     public void setBinaryTree(BinaryTree tree) {
          this.tree = tree;
     } // end method setBinaryTree

     public void setOutputPanel(Panel panel) {
          this.outputPanel = panel;
     } // end method setOutputPanel

     public void setSizeToFit(boolean sizeToFit) {
          this.sizeToFit = sizeToFit;
     } // end method setSizeToFit

     public void increaseXDiv() {
          xDiv*=1.1;
     } // end method increaseXDiv

     public void increaseYDiv() {
          yDiv*=1.1;
     } // end method increaseYDiv

     public void decreaseXDiv() {
          xDiv*=.9;
     } // end method decreaseXDiv

     public void decreaseYDiv() {
          yDiv*=.9;
     } // end method decreaseyDiv

// Public Methods
     public void drawTree(Point translate) {
          if(outputPanel!=null  && tree!=null) {
               Graphics g = outputPanel.getGraphics();
               g.translate(translate.x, translate.y);
               Dimension panelSize = outputPanel.getSize();
               if(!tree.isEmpty()) {
                    int treeDepth = tree.maxLevel();
                    if(sizeToFit) {
                         xDiv = (double)panelSize.width/(Math.pow(2,treeDepth+1)+1);
                         yDiv = (double)panelSize.height/((double)treeDepth+1);
                    }

                    Point rootCoord = new Point(panelSize.width/2, (int)(yDiv/2));
                    drawTreeNode(rootCoord, tree.getRoot(), 0, g);
               }
               else {
                    Font f = g.getFont();
                    String message = "The Tree is Empty.";
                    FontMetrics fm = g.getFontMetrics(f);
                    int messageLength = fm.stringWidth(message);
                    int messageHeight = fm.getHeight();
                    g.drawString(message, panelSize.width/2 - messageLength/2,
                         panelSize.height/2 - messageHeight/2);

               }
               g.dispose();
          }
     } // end method drawTree

// Private Methods
     private void drawTreeNode(Point coord, SearchTreeNode node, int depth,
               Graphics g) {
          double xOffset = (Math.pow(2, tree.maxLevel() - depth)/2)*xDiv;
          int newY =(int)(((double)depth+1)*yDiv+yDiv/2.0);
          if(node.leftChild!=null) {
               int lcx = (int)(coord.x - xOffset);
               g.setColor(Color.blue);
               g.drawLine(coord.x, coord.y, lcx, newY);
               Point leftChildCoord = new Point(lcx, newY);
               drawTreeNode(leftChildCoord, node.leftChild, (depth + 1), g);
          }
          if(node.rightChild!=null) {
               int rcx = (int)(coord.x + xOffset);
               g.setColor(Color.red);
               g.drawLine(coord.x, coord.y, rcx, newY);
               Point rightChildCoord = new Point(rcx, newY);
               drawTreeNode(rightChildCoord, node.rightChild, (depth + 1), g);
          }
          g.setColor(Color.black);
          g.drawOval(coord.x-5, coord.y-5, 10, 10);
          g.fillOval(coord.x-5, coord.y-5, 10, 10);
          g.drawString(node.item.toString(), coord.x+10, coord.y+5);
     } // end method drawTreeNode

} // end class DrawBinary Tree

稍后,我可能会将其翻译成一个快速、实用的C#示例,但这是我在2000年完成的Java家庭作业!

您应该能够根据Java类收集绘制树所需的算法和代码:

//    FileName: DrawBinaryTree.java
/************************************
 *     Student: Michael Tomlinson   *
 *      Course: CS 145-Section 1    *
 *  Instructor: Dareleen Schaffer   *
 *                                  *
 *   Assignment #4, Problem #1      *
 *    Due Date: July 24, 2000       *
 ************************************
 */
package BinaryTree;

import java.awt.*;
import javax.swing.*;

public class DrawBinaryTree {

// Data Fields
     private BinaryTree tree=null;
     private double xDiv=50, yDiv=50;
     private boolean sizeToFit=true;
     private Panel outputPanel=null;

// Constructors
     public DrawBinaryTree() {} // default constructor
     public DrawBinaryTree(BinaryTree tree) {
          setBinaryTree(tree);
     } // end constructor with (BinaryTree) parameters
     public DrawBinaryTree(Panel panel) {
          setOutputPanel(panel);
     } // end constructor with (JPanel) parameters
     public DrawBinaryTree(BinaryTree tree, Panel panel) {
          setBinaryTree(tree);
          setOutputPanel(panel);
     } // end constructor with (BinaryTree, JPanel) parameters

// Modifiers
     public void setBinaryTree(BinaryTree tree) {
          this.tree = tree;
     } // end method setBinaryTree

     public void setOutputPanel(Panel panel) {
          this.outputPanel = panel;
     } // end method setOutputPanel

     public void setSizeToFit(boolean sizeToFit) {
          this.sizeToFit = sizeToFit;
     } // end method setSizeToFit

     public void increaseXDiv() {
          xDiv*=1.1;
     } // end method increaseXDiv

     public void increaseYDiv() {
          yDiv*=1.1;
     } // end method increaseYDiv

     public void decreaseXDiv() {
          xDiv*=.9;
     } // end method decreaseXDiv

     public void decreaseYDiv() {
          yDiv*=.9;
     } // end method decreaseyDiv

// Public Methods
     public void drawTree(Point translate) {
          if(outputPanel!=null  && tree!=null) {
               Graphics g = outputPanel.getGraphics();
               g.translate(translate.x, translate.y);
               Dimension panelSize = outputPanel.getSize();
               if(!tree.isEmpty()) {
                    int treeDepth = tree.maxLevel();
                    if(sizeToFit) {
                         xDiv = (double)panelSize.width/(Math.pow(2,treeDepth+1)+1);
                         yDiv = (double)panelSize.height/((double)treeDepth+1);
                    }

                    Point rootCoord = new Point(panelSize.width/2, (int)(yDiv/2));
                    drawTreeNode(rootCoord, tree.getRoot(), 0, g);
               }
               else {
                    Font f = g.getFont();
                    String message = "The Tree is Empty.";
                    FontMetrics fm = g.getFontMetrics(f);
                    int messageLength = fm.stringWidth(message);
                    int messageHeight = fm.getHeight();
                    g.drawString(message, panelSize.width/2 - messageLength/2,
                         panelSize.height/2 - messageHeight/2);

               }
               g.dispose();
          }
     } // end method drawTree

// Private Methods
     private void drawTreeNode(Point coord, SearchTreeNode node, int depth,
               Graphics g) {
          double xOffset = (Math.pow(2, tree.maxLevel() - depth)/2)*xDiv;
          int newY =(int)(((double)depth+1)*yDiv+yDiv/2.0);
          if(node.leftChild!=null) {
               int lcx = (int)(coord.x - xOffset);
               g.setColor(Color.blue);
               g.drawLine(coord.x, coord.y, lcx, newY);
               Point leftChildCoord = new Point(lcx, newY);
               drawTreeNode(leftChildCoord, node.leftChild, (depth + 1), g);
          }
          if(node.rightChild!=null) {
               int rcx = (int)(coord.x + xOffset);
               g.setColor(Color.red);
               g.drawLine(coord.x, coord.y, rcx, newY);
               Point rightChildCoord = new Point(rcx, newY);
               drawTreeNode(rightChildCoord, node.rightChild, (depth + 1), g);
          }
          g.setColor(Color.black);
          g.drawOval(coord.x-5, coord.y-5, 10, 10);
          g.fillOval(coord.x-5, coord.y-5, 10, 10);
          g.drawString(node.item.toString(), coord.x+10, coord.y+5);
     } // end method drawTreeNode

} // end class DrawBinary Tree

根据你之前的问题,我想你更喜欢C#?是的,很抱歉我没有具体说明。但我认为这并不重要,因为它是一种图形表示。我不太熟悉WPF的功能,是否可以在WPF中构造类似的东西?基于您之前的问题,我假设您更喜欢C#?是的,很抱歉我没有指定。但我认为这并不重要,因为它是一种图形表示。我不太熟悉WPF的功能,是否可以在WPF中构建类似的东西?