Home > Back-end >  001001 processing for Java tree structure
001001 processing for Java tree structure

Time:09-16

Existing a database tables, in accordance with the requirements, the need will be displayed in the table in a tree, only have these three fields, can't add the parent id, how to use the code to handle, please give a little advice

CodePudding user response:

 class TreeNode {
Private String dar.

Private String treeName;

Private String treeCode;

Private List The children;

Public TreeNode (String dar, String treeName, String treeCode) {
This. TId=dar;
Enclosing treeName=treeName;
Enclosing treeCode=treeCode;
}

Public String gettId () {
Return dar;
}

Public void settId (String dar) {
This. TId=dar;
}

Public String getTreeName () {
Return treeName;
}

Public void setTreeName (String treeName) {
Enclosing treeName=treeName;
}

Public String getTreeCode () {
Return treeCode;
}

Public void setTreeCode (String treeCode) {
Enclosing treeCode=treeCode;
}

Public List GetChildren () {
Return the children==null? New ArrayList<> () : the children;
}

Public void setChildren (List The list) {
This. The children=list;
}
}
 public List TreeBin (List The list) {
TreeNode root=new TreeNode (" root ", "root", "root");
//by treecode order, also can use SQL directly order
List. Sort ((a, b) - & gt; Atul gawande etTreeCode (). CompareTo (b.g etTreeCode ()));
Map The map=new HashMap<> (a);
For (TreeNode node: list) {
if (! Map. Either containsKey (node. GetTreeCode ())) {
The map. The put (node. GetTreeCode (), the node).
}
String parentCode=node. GetTreeCode (). The substring (0, node. GetTreeCode (). The length () - 3);
If (parentCode. Length ()==0) {
Root. GetChildren (). The add (node);
}
If (map. Either containsKey (parentCode)) {
The map. The get (parentCode). GetChildren (). The add (node);
}
}
Return a root. The getChildren ();
}

CodePudding user response:

If you are familiar with pid method is how to operate, so also is very simple,

Private String dar.
Private String parentTreeCode;
Private String treeCode;

Public String getParentTreeCode () {
If (StringUtils. IsNotBlank (enclosing treeCode)) {
Return parentTreeCode. Substring (0, dar. Length () - 3);
}
return null;
}

Attention to Mr Itself into the root node, the root node treeCode="", the tree is generated in the pid way

CodePudding user response:

reference 1/f, a handsome and forced response:
 class TreeNode {
Private String dar.

Private String treeName;

Private String treeCode;

Private List The children;

Public TreeNode (String dar, String treeName, String treeCode) {
This. TId=dar;
Enclosing treeName=treeName;
Enclosing treeCode=treeCode;
}

Public String gettId () {
Return dar;
}

Public void settId (String dar) {
This. TId=dar;
}

Public String getTreeName () {
Return treeName;
}

Public void setTreeName (String treeName) {
Enclosing treeName=treeName;
}

Public String getTreeCode () {
Return treeCode;
}

Public void setTreeCode (String treeCode) {
Enclosing treeCode=treeCode;
}

Public List GetChildren () {
Return the children==null? New ArrayList<> () : the children;
}

Public void setChildren (List The list) {
This. The children=list;
}
}
 public List TreeBin (List The list) {
TreeNode root=new TreeNode (" root ", "root", "root");
//by treecode order, also can use SQL directly order
List. Sort ((a, b) - & gt; Atul gawande etTreeCode (). CompareTo (b.g etTreeCode ()));
Map The map=new HashMap<> (a);
For (TreeNode node: list) {
if (! Map. Either containsKey (node. GetTreeCode ())) {
The map. The put (node. GetTreeCode (), the node).
}
String parentCode=node. GetTreeCode (). The substring (0, node. GetTreeCode (). The length () - 3);
If (parentCode. Length ()==0) {
Root. GetChildren (). The add (node);
}
If (map. Either containsKey (parentCode)) {
The map. The get (parentCode). GetChildren (). The add (node);
}
}
Return a root. The getChildren ();
}

This way generated is empty, what all have no

CodePudding user response:

refer to the second floor RockeyCui response:
if you are familiar with pid method is how to operate, so also is very simple,

Private String dar.
Private String parentTreeCode;
Private String treeCode;

Public String getParentTreeCode () {
If (StringUtils. IsNotBlank (enclosing treeCode)) {
Return parentTreeCode. Substring (0, dar. Length () - 3);
}
return null;
}

Note Mr Itself into the root node, the root node treeCode="", the tree is generated in the pid way.


The basic have no what relation and pid, although use recursion, method is totally different

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related