English 中文(简体)
1. 树木自下而上
原标题:Filling a tree bottom up
  • 时间:2011-03-16 12:39:26
  •  标签:
  • java
  • tree

基本上,我需要做的是,从一个无孔不入的物体清单中形成树木结构。 这一树需根据清单中物体的母异构体填充。 因此,如果名单上的物体有<代码>即为>uid=0,即为根基。 如果它有<条码>,即<<>条/条码>,则该物体的子女有<条码>id =1>。 砍伐树木是问题。 现在,这只是静态填充的,但我需要一个充满活力的方法来填充树。 希望有人能向我提出一些建议。 我对我的问题进行了总结,并制定了以下法典:

public class Node {
   private int id,parentid;
    private String text;

    public int getParentid() {
        return parentid;
    }

    public void setParentid(int parentid) {
        this.parentid = parentid;
    }

    Node(int id , String s,int pid){
        setId(id);
        setParentid(pid);
        setText(s);
    }

    public int getNummer() {
        return id;
    }

    public void setId(int nummer) {
        this.id = nummer;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collection;

public class NodeTreeSample {

    public static void main(String args[]) {

        JFrame frame = new JFrame("Tree");

        //The unsorted list with the objects
        Collection<Node> treeList = new ArrayList<Node>();
        treeList.add(new Node(1,"Rootnode",0));
        treeList.add(new Node(2,"Child of node with id 1",1));
        treeList.add(new Node(3, "Child of node with id 1", 1));
        treeList.add(new Node(4, "Child of node with id 2", 2));
        treeList.add(new Node(5, "Child of node with id 2", 2));

        DefaultMutableTreeNode root=new DefaultMutableTreeNode("Root");

        //Filling the tree
        for(Node n:treeList){
            if(n.getParentid()==0){
               root = new DefaultMutableTreeNode(n.getText());
            }
            if(n.getParentid()==1){
                root.add(new DefaultMutableTreeNode(n.getText()));
            }
            if(n.getParentid()==2){

            }
        }
        JTree tree = new JTree(root);
        JScrollPane scrollPane = new JScrollPane(tree);
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.setSize(300, 150);
        frame.setVisible(true);
    }
}
最佳回答

这是一 tree双树或一根非常复杂的树,因此,你不需要“动力解决方案”,我猜想你不意味着动态的方案拟订? 你们想要的任何东西都是对树木进行深度第一的搜索,以添加新的 no子,因为那一树确实是复杂的树,而你必须把树子分开。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签