In java and js the tree structure method of of with infinite hierarchy is similar to recursion

  • 2020-05-16 06:57:20
  • OfStack

js:


var zNodes=[
{id:0,pId:-1,name:"Aaaa"},
  {id:1,pId:0,name:"A"},
  {id:11,pId:1,name:"A1"},
  {id:12,pId:1,name:"A2"},
  {id:13,pId:1,name:"A3"},
  {id:2,pId:0,name:"B"},
  {id:21,pId:2,name:"B1"},
  {id:22,pId:2,name:"B2"},
  {id:23,pId:2,name:"B3"},
  {id:3,pId:0,name:"C"},
  {id:31,pId:3,name:"C1"},
  {id:32,pId:3,name:"C2"},
  {id:33,pId:3,name:"C3"},
  {id:34,pId:31,name:"x"},
  {id:35,pId:31,name:"y"}, 
  {id:36,pId:31,name:"z"},
  {id:37,pId:36,name:"z1123"} ,
  {id:38,pId:37,name:"z123123123"}  
];
function treeMenu(a){
  this.tree=a||[];
  this.groups={};
};
treeMenu.prototype={
  init:function(pid){
    this.group();
    return this.getDom(this.groups[pid]);
  },
  group:function(){
    for(var i=0;i<this.tree.length;i++){
      if(this.groups[this.tree[i].pId]){
        this.groups[this.tree[i].pId].push(this.tree[i]);
      }else{
        this.groups[this.tree[i].pId]=[];
        this.groups[this.tree[i].pId].push(this.tree[i]);
      }
    }
  },
  getDom:function(a){
    if(!a){return ''}
    var html='\n<ul >\n';
    for(var i=0;i<a.length;i++){
      html+='<li><a href="#">'+a[i].name+'</a>';
      html+=this.getDom(this.groups[a[i].id]);
      html+='</li>\n';
    };
    html+='</ul>\n';
    return html;
  }
};
var html=new treeMenu(zNodes).init(0);
alert(html);

java:


package test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Collections;

/**
 *  More tree class 
*/
public class MultipleTree {
 public static void main(String[] args) {
 //  Reads the list of hierarchical data result sets  
 List dataList = VirtualDataGenerator.getVirtualResult(); 
 
 //  Node list (hash table for temporary storage of node objects) 
 HashMap nodeList = new HashMap();
 //  The root node 
 Node root = null;
 //  Construct a node list based on the result set (stored in a hash table) 
 for (Iterator it = dataList.iterator(); it.hasNext();) {
  Map dataRecord = (Map) it.next();
  Node node = new Node();
  node.id = (String) dataRecord.get("id");
  node.text = (String) dataRecord.get("text");
  node.parentId = (String) dataRecord.get("parentId");
  nodeList.put(node.id, node);
 }
 //  Construct a disordered polytree 
 Set entrySet = nodeList.entrySet();
 for (Iterator it = entrySet.iterator(); it.hasNext();) {
  Node node = (Node) ((Map.Entry) it.next()).getValue();
  if (node.parentId == null || node.parentId.equals("")) {
  root = node;
  } else {
  ((Node) nodeList.get(node.parentId)).addChild(node);
  }
 }
 //  Output the unordered tree menu JSON string 
 System.out.println(root.toString());  
 //  Horizontal sorting of multiple fork trees 
 root.sortChildren();
 //  Output ordered tree menu JSON string 
 System.out.println(root.toString()); 
 
 //  The output of the program is as follows (unordered tree menu) (formatted results) :  
 // {
 //  id : '100000', 
 //  text : ' Langfang bank head office ', 
 //  children : [
 //   {
 //   id : '110000', 
 //   text : ' Langfang branch ', 
 //   children : [
 //    {
 //    id : '113000', 
 //    text : ' Langfang bank development zone branch ', 
 //    leaf : true
 //    },
 //    {
 //    id : '111000', 
 //    text : ' Langfang bank jinguang dao branch ', 
 //    leaf : true
 //    },
 //    {
 //    id : '112000', 
 //    text : ' Langfang bank jiefang road branch ', 
 //    children : [
 //     {
 //     id : '112200', 
 //     text : ' Langfang bank 3 Street branch ', 
 //     leaf : true
 //     },
 //     {
 //     id : '112100', 
 //     text : ' Langfang bank guangyangdao branch ', 
 //     leaf : true
 //     }
 //    ]
 //    }
 //   ]
 //   }
 //  ]
 // }

 //  The output of the program is as follows (ordered tree menu) (formatted results) : 
 // {
 //  id : '100000', 
 //  text : ' Langfang bank head office ', 
 //  children : [
 //   {
 //   id : '110000', 
 //   text : ' Langfang branch ', 
 //   children : [
 //    {
 //    id : '111000', 
 //    text : ' Langfang bank jinguang dao branch ', 
 //    leaf : true
 //    },
 //    {
 //    id : '112000', 
 //    text : ' Langfang bank jiefang road branch ', 
 //    children : [
 //     {
 //     id : '112100', 
 //     text : ' Langfang bank guangyangdao branch ', 
 //     leaf : true
 //     },
 //     {
 //     id : '112200', 
 //     text : ' Langfang bank 3 Street branch ', 
 //     leaf : true
 //     }
 //    ]
 //    },
 //    {
 //    id : '113000', 
 //    text : ' Langfang bank development zone branch ', 
 //    leaf : true
 //    }
 //   ]
 //   }
 //  ]
 // } 
 
 }
  
}


/**
*  Node class 
*/
class Node {
 /**
 *  Node number 
 */
 public String id;
 /**
 *  Node content 
 */
 public String text;
 /**
 *  Parent node number 
 */
 public String parentId;
 /**
 *  Child node list 
 */
 private Children children = new Children();
 
 //  First order traversal, splice JSON string 
 public String toString() { 
 String result = "{"
  + "id : '" + id + "'"
  + ", text : '" + text + "'";
 
 if (children != null && children.getSize() != 0) {
  result += ", children : " + children.toString();
 } else {
  result += ", leaf : true";
 }
  
 return result + "}";
 }
 
 //  Horizontal sorting of sibling nodes 
 public void sortChildren() {
 if (children != null && children.getSize() != 0) {
  children.sortChildren();
 }
 }
 
 //  Add child node 
 public void addChild(Node node) {
 this.children.addChild(node);
 }
}

/**
*  Child list class 
*/
class Children {
 private List list = new ArrayList();
 
 public int getSize() {
 return list.size();
 }
 
 public void addChild(Node node) {
 list.add(node);
 }
 
 //  Splice child nodes JSON string 
 public String toString() {
 String result = "["; 
 for (Iterator it = list.iterator(); it.hasNext();) {
  result += ((Node) it.next()).toString();
  result += ",";
 }
 result = result.substring(0, result.length() - 1);
 result += "]";
 return result;
 }
 
 //  Child node sorting 
 public void sortChildren() {
 //  Sort the nodes in this layer 
 //  You can pass in different comparators based on different sort properties, and here you pass in ID The comparator 
 Collections.sort(list, new NodeIDComparator());
 //  For each node down 1 Layer nodes are sorted 
 for (Iterator it = list.iterator(); it.hasNext();) {
  ((Node) it.next()).sortChildren();
 }
 }
}

/**
 *  Node comparator 
 */
class NodeIDComparator implements Comparator {
 //  Compare by node number 
 public int compare(Object o1, Object o2) {
 int j1 = Integer.parseInt(((Node)o1).id);
   int j2 = Integer.parseInt(((Node)o2).id);
   return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));
 } 
}

/**
 *  Construct virtual hierarchical data 
 */
class VirtualDataGenerator {
 //  Construct an unordered list of result sets. In practice, the data should be retrieved from the database. 
 public static List getVirtualResult() {  
 List dataList = new ArrayList();
 
 HashMap dataRecord1 = new HashMap();
 dataRecord1.put("id", "112000");
 dataRecord1.put("text", " Langfang bank jiefang road branch ");
 dataRecord1.put("parentId", "110000");
 
 HashMap dataRecord2 = new HashMap();
 dataRecord2.put("id", "112200");
 dataRecord2.put("text", " Langfang bank 3 Street branch ");
 dataRecord2.put("parentId", "112000");
 
 HashMap dataRecord3 = new HashMap();
 dataRecord3.put("id", "112100");
 dataRecord3.put("text", " Langfang bank guangyangdao branch ");
 dataRecord3.put("parentId", "112000");
   
 HashMap dataRecord4 = new HashMap();
 dataRecord4.put("id", "113000");
 dataRecord4.put("text", " Langfang bank development zone branch ");
 dataRecord4.put("parentId", "110000");
   
 HashMap dataRecord5 = new HashMap();
 dataRecord5.put("id", "100000");
 dataRecord5.put("text", " Langfang bank head office ");
 dataRecord5.put("parentId", "");
 
 HashMap dataRecord6 = new HashMap();
 dataRecord6.put("id", "110000");
 dataRecord6.put("text", " Langfang branch ");
 dataRecord6.put("parentId", "100000");
 
 HashMap dataRecord7 = new HashMap();
 dataRecord7.put("id", "111000");
 dataRecord7.put("text", " Langfang bank jinguang dao branch ");
 dataRecord7.put("parentId", "110000"); 
  
 dataList.add(dataRecord1);
 dataList.add(dataRecord2);
 dataList.add(dataRecord3);
 dataList.add(dataRecord4);
 dataList.add(dataRecord5);
 dataList.add(dataRecord6);
 dataList.add(dataRecord7);
 
 return dataList;
 } 
}

Related articles: