java 实现二叉树的可视化

时间:2023-01-17 17:28:04
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 实现二叉树的可视化

这是运行之后的效果图,可以输入节点名称进行查找,树形结构也比较明确。