java - Filling a tree bottom up -


What should I do in order to build tree-structure from a sorted list with objects The tree of the object in this tree list Need to be filled in the ID attribute basis. Therefore, if an object in the list has parentid = 0 , then it is the root if it contains parentid = 1 , then this is the child of the object id = 1 < There is a problem under 'tree filling' with / code> now it is static in full, but I need a dynamic way to fill the tree. Hopefully someone can give me some advice, I have leveled my problem and made the following code:

  Public class node {private IIT, parent; Private string text; Public int getParentid () {return parentid; } Public Zero Set Parentid (int parentid) {this.parentid = parentid; } Node (int id, string s, int pid) {setId (id); SetParentid (PID); SetText (s); } Public int getNummer () {return ID; } Public Zero Set ID (Ant Number) {this.id = nummer; } Public string getText () {return text; } Public Zero Set Text (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 nodtreesammil {public static zero chief (string agre []) {jeffrey frame = new jefram ("tree"); Sort items with // archive collection & lt; Node & gt; Tree list = new array list & lieutenant; Node & gt; (); Tree list.ed (node ​​node (1, "routode", 0)); TreeList.add (new node (2, "node child with id 1", 1)); Pedalist.ed (node ​​node (3, "baby node with id 1", 1)); Tree list.ed (node ​​node (4, "child of node with id2", 2)); Tree list.ed (node ​​node (5, "node child with id 2", 2)); DefaultMutableTreeNode root = new DefaultMutableTreeNode ("root"); // Filling the tree (node ​​n: tree list) {if (n.getParentid () == 0) {root = new DefaultMutableTreeNode (n.getText ()); } If (n.getParentid () == 1) {root.add (new default prompt node (n.getText ())); } If (n.getParentid () == 2) {}} Jetry Tree = New Jetri (Route); JScrollPane scrollPane = new JScrollPane (tree); Frame.getContentPane () Add (ScrollPan, BorderLayout.CENTER); Frame.setSize (300, 150); Frame.setVisible (true); }}    

This is not a binary tree or a very complex tree, "Dynamic solution" is not required and I think you do not understand dynamic programming? Anyway whatever you want to insert a new node is the first discovery of a tree depth because it is not really a complex tree where you have to divide the node.

Comments