java 实现二叉树的可视化
来源:互联网 发布:考无忧通用算法注册机 编辑:程序博客网 时间:2024/05/16 22:57
public class BrinaryTree extends javax.swing.JFrame { /** Creates new form BrinaryTree */ CreatNodes root; public BrinaryTree() { initComponents(); DefaultMutableTreeNode dmt=new DefaultMutableTreeNode(50); ((DefaultTreeModel)jTree1.getModel()).setRoot(dmt); root=new CreatNodes(50,dmt); for(int i=1;i<Math.pow(2, 7);i++) { root.InsertTree(i,root); root.InsertTree(i-5,root); } //TreeNode rootnode=dmt; //JTree tree = new JTree(rootnode); // setViewportView(tree); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { JFrame = new javax.swing.JScrollPane(); jTree1 = new javax.swing.JTree(); OKbutton = new javax.swing.JButton(); tempTextField = new javax.swing.JTextField(); OKlabel = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTree1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); JFrame.setViewportView(jTree1); OKbutton.setText("确定"); OKbutton.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { OKbuttonMouseClicked(evt); } }); OKbutton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { OKbuttonActionPerformed(evt); } }); tempTextField.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tempTextFieldActionPerformed(evt); } }); OKlabel.setText("点击显示结点"); jLabel1.setText("jLabel1"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(JFrame, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addGap(36, 36, 36) .addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(50, 50, 50) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(OKlabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(OKbutton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(27, 27, 27) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(JFrame, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(17, 17, 17) .addComponent(OKlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(OKbutton, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addContainerGap(20, Short.MAX_VALUE)) ); pack(); }// </editor-fold> class CreatNodes{ int data; CreatNodes lefttree; CreatNodes righttree; DefaultMutableTreeNode parent ; CreatNodes(int data, DefaultMutableTreeNode dmt) { this.data=data; this.parent=dmt; this.lefttree=null; this.righttree=null; } public void InsertTree(int data,CreatNodes root) { //DefaultMutableTreeNode dmt1=new DefaultMutableTreeNode(1); //parent.add(dmt1); //DefaultMutableTreeNode dmt2=new DefaultMutableTreeNode(2); //dmt1.add(dmt2); if(data>=root.data) { if(root.righttree==null) { DefaultMutableTreeNode son=new DefaultMutableTreeNode(data); root.parent.add(son); root.righttree=new CreatNodes(data,son); } else { InsertTree(data,root.righttree); } } else { if(root.lefttree==null) { DefaultMutableTreeNode son=new DefaultMutableTreeNode(data); root.parent.add(son); root.lefttree=new CreatNodes(data,son); } else { InsertTree(data,root.lefttree); } }} CreatNodes searchnode(int key) { return searchnode( root,key); } CreatNodes searchnode(CreatNodes node ,int key) { if(node==null) { return null; } else if(node.data==key) { return node; } else if(key>node.data) { return searchnode(node.righttree,key); } else return searchnode(node.lefttree,key); }} private void OKbuttonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int findnode = (int)(Double.parseDouble(tempTextField.getText())); OKlabel.setText("要查找的结点"+findnode); CreatNodes searchfindnode=root.searchnode(findnode); if(searchfindnode==null) { OKlabel.setText("没找到"+findnode); } else{ TreePath Path = new TreePath(((DefaultTreeModel)jTree1.getModel()).getPathToRoot(searchfindnode.parent)); jTree1.makeVisible(Path); jTree1.addSelectionPath(Path); } } private void tempTextFieldActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: //DefaultMutableTreeNode selectedNode//= (DefaultMutableTreeNode )tree.getLastSelectedPathComponent(); } private void OKbuttonMouseClicked(java.awt.event.MouseEvent evt) { // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new BrinaryTree().setVisible(true); } }); //TextField msg=new TextField(); } // Variables declaration - do not modify private javax.swing.JScrollPane JFrame; private javax.swing.JButton OKbutton; private javax.swing.JLabel OKlabel; private javax.swing.JLabel jLabel1; private javax.swing.JTree jTree1; private javax.swing.JTextField tempTextField; // End of variables declaration }
这是运行之后的效果图,可以输入节点名称进行查找,树形结构也比较明确。
- java 实现二叉树的可视化
- 二叉树的可视化
- 二叉树的可视化
- 二叉树的可视化
- 二叉树的可视化
- Flex布局+JS:实现可视化的二叉树遍历
- 二叉树的java实现
- Java二叉树的实现
- java二叉树的实现
- 二叉树的java实现
- java二叉树的实现
- 二叉树的Java实现
- 二叉树的java实现
- 二叉树的实现Java
- 二叉树的Java实现
- ## java实现的二叉树 ##
- 二叉树可视化--Graphviz
- 可视化遍历二叉树
- Android服务重启
- SQL调用系统存储过程改变列名
- 程序为什么开头总是PUSH EBP
- windows下使用Eclipse运行MapReduce程序出错: Failed to set...
- <c:if>判断两个<c:forEach>里的数据是否相等
- java 实现二叉树的可视化
- 在Java中实现UDP协议编程(DatagramSocket/DatagramPacket)
- 形参与实参
- boost,pthread,clapack,jpeg,gsl在windows下配置方法
- 点击短信内容,获取短信内的号码和链接(2)
- ios 学习资料
- 0-1背包问题
- SQL 查询count显示为0的结果
- Js三级联动例子