Build tree object encapsulation Information

Source: Internet
Author: User
Tags return tag nodesource

Package com. wangjin. domaintree;
Public class NodeSource {
Private int id;
Private int parentid;
Private String title;
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public int getParentid (){
Return parentid;
}
Public void setParentid (int parentid ){
This. parentid = parentid;
}
Public String getTitle (){
Return title;
}
Public void setTitle (String title ){
This. title = title;
}
Public NodeSource (int id, int parentid, String title ){
Super ();
This. id = id;
This. parentid = parentid;
This. title = title;
}
Public NodeSource (){
Super ();
}
 
 
}
 
Package com. wangjin. domaintree;
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. List;
Import java. util. Map;
Public class TreeNode {
// Value carried by the node
Private Map <String, Object> tag;
// Son Node
Private List <TreeNode> nodes;
/** The node information key-value pair. This attribute will not be a null pointer, but there may be no key-value pairs [that is, map. size () = 0] */
Public Map <String, Object> getTag (){
If (tag = null)
Tag = new HashMap <String, Object> ();
Return tag;
}
Public void setTag (Map <String, Object> tag ){
This. tag = tag;
}
/** Obtain all child nodes. This attribute is not a null pointer, but may not contain any set element */
Public List <TreeNode> getNodes (){
If (nodes = null)
Nodes = new ArrayList <TreeNode> ();
Return nodes;
}
Public void setNodes (List <TreeNode> nodes ){
This. nodes = nodes;
}
Public TreeNode (Map <String, Object> tag, List <TreeNode> nodes ){
Super ();
This. tag = tag;
This. nodes = nodes;
}
Public TreeNode (){
Super ();
}
Public TreeNode (Map <String, Object> tag ){
Super ();
This. tag = tag;
}
}
Package com. wangjin. domaintree;
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. List;
Import java. util. Map;
Public class CapsulationTree {
Public TreeNode getTree (){
// Use a set to simulate the data table content, that is, the set content can be fully queried from the database
List <NodeSource> treeList = new ArrayList <NodeSource> ();
TreeList. add (new NodeSource (0,-1, "root "));
TreeList. add (new NodeSource (1, 0, "node1 "));
TreeList. add (new NodeSource (2, 1, "node2 "));
TreeList. add (new NodeSource (3,1, "node3 "));
TreeList. add (new NodeSource (4,0, "node4 "));
TreeList. add (new NodeSource (5, 4, "node5 "));
TreeList. add (new NodeSource (6, 4, "node6 "));
TreeList. add (new NodeSource (7,5, "node7 "));
TreeList. add (new NodeSource (8, 5, "node8 "));
TreeList. add (new NodeSource (9, 7, "node9 "));
TreeList. add (new NodeSource (10, 7, "node10 "));

// Encapsulate this from a set into TreeNode
// Construct a root node
TreeNode rootNode = new TreeNode ();
RootNode. getTag (). put ("id", "0 ");
RootNode. getTag (). put ("title", "root ");
 

// Encapsulate this set into Map
Map <String, TreeNode> treeNodeMap = new HashMap <String, TreeNode> ();
// The following loop completes two functions:
// 1. encapsulate all objects into key-value pairs in the Map, and the values are of the TreeNode type.
// 2. Add TreeNode without a parent node to the root node rootNode
For (NodeSource nodeSource: treeList ){
TreeNode node = new TreeNode ();
Node. getTag (). put ("id", (Integer) nodeSource. getId (). toString ());
Node. getTag (). put ("title", nodeSource. getTitle ());
TreeNodeMap. put (Integer) nodeSource. getId (). toString (), node );
System. out. println ("(" + nodeSource. getId () + "," + nodeSource. getParentid () + ")");
If (nodeSource. getParentid () =-1) {// Add to the root node if no parent node exists
RootNode. getNodes (). add (node );
System. out. println (nodeSource. getParentid () + "WANGJINROOT ");
}
}


// The following Code establishes the subordination between nodes.
For (int I = 0; I <treeList. size (); I ++ ){
NodeSource nodeSource = treeList. get (I );
If (treeNodeMap. containsKey (Integer) nodeSource. getParentid (). toString () {// if the parent function number of the current function exists in treeNodeMap
TreeNodeMap
. Get (Integer) nodeSource. getParentid (). toString () // find the parent node
. GetNodes () // child node set of the parent node
. Add (// add a subnode to the subnode set
TreeNodeMap. get (Integer) nodeSource. getId (). toString () // locate the subnode

);
System. out. println (Integer) nodeSource. getParentid ()). toString () + "," + (Integer) nodeSource. getId ()). toString ());
}
}
Return treeNodeMap. get ("0 ");
}
Public static void main (String [] args ){
CapsulationTree capsulationTree = new CapsulationTree ();
TreeNode rootNode = capsulationTree. getTree ();
System. out. print (rootNode. getTag (). get ("title") + "-");
For (int I = 0; I <rootNode. getNodes (). size (); I ++ ){
TreeNode node1 = rootNode. getNodes (). get (I );
System. out. print (node1.getTag (). get ("title") + "--");
If (node1.getNodes (). size ()! = 0 ){
For (int k = 0; k <node1.getNodes (). size (); k ++ ){
TreeNode node2 = node1.getNodes (). get (k );
System. out. print (node2.getTag (). get ("title") + "--");
If (node2.getNodes (). size ()! = 0 ){
For (int k1 = 0; k1 <node2.getNodes (). size (); k1 ++ ){
TreeNode node3 = node2.getNodes (). get (k1 );
System. out. print (node3.getTag (). get ("title") + "--");
If (node3.getNodes (). size ()! = 0 ){
For (int k2 = 0; k2 <node3.getNodes (). size (); k2 ++ ){
TreeNode node4 = node3.getNodes (). get (k2 );
System. out. print (node4.getTag (). get ("title") + "----");
}
}
}
}
}
}
}
 
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.