Use C # To generate multi-tree and convert it to the json data format of TreeNode in extjs (ajax UI framework based on js scripts) (The format is

Source: Internet
Author: User

When thinking about this algorithm, I first thought about the extjs format conversion method before writing it. After writing it, I didn't consider whether extjs can directly use {'id ':'', what is the format of 'pid ': '', 'text? Well, if so, it will be depressing ~~, No. To use Extjs to display Tree nodes on the client, you need to obtain the child node set of nodes. So it took me three hours to write a traversal algorithm (coding and code writing capabilities need to be improved. Sometimes the relationship between them gets dizzy, you have to draw a picture to write code ~~), List <TreeNode> with the children field empty analyzes and returns a root node tree containing children to generate json data.

The algorithm IDEA is as follows:
1) when a node exists in nodelist, extract a node from nodeList and remove it from nodeList. 2)
2) The deep traversal algorithm is used to push each node into the stack. Because nodelist does not contain the root node, a root node is created. 3)
3) check whether the current node is empty in a loop (that is, the children set that has been added as the root node). If it is not empty, perform 4); otherwise, exit ).
4) retrieve the top node of the stack. Determine whether the node has a child node not added to the children set (that is, whether the node can be found in nodeList ).
If the pid is the node id, perform 5). Otherwise, perform 6)
5) Remove the node from nodeList, go to the stack, continue searching, and return 3)
6) if the current node does not have a child node, to 7)
7) at this time, judge whether the stack is empty. If it is empty (indicating that all the subnodes of the current node have been found), to 8), if the stack is not empty, to 9)
8) the current node is a leaf node, and the top node of the stack is the parent node of the current node. To add the child node to the children set of the parent node. The parent node re-joins the stack. To 3)
9) Determine whether the current node has a parent node (that is, to judge whether there is a node with the id as the pid of the current node in nodeList); if so, to 10); otherwise, to 11)
10) Remove the node as pNode, delete it from nodeList, and add the current node cNode to the children set of pNode,
That is, the child node of the parent node. The parent node may have other child nodes in the stack ). To 3)
11) indicates that the current node is a top-level node and is directly added to the children of the root user. To 3)
Every time a subnode is found, it will be removed from nodelist.
Exit) The program ends. The returned root node is the complete multi-tree root node. You can access the node through its Child set and
Use the json method to convert the tree data format.

 

Public class TreeNodeHelper {/// <summary> /// generate a root node tree /// </summary> /// <param name = "nodeList"> node list, contains unconnected Tree nodes. id, pid, and text fields are provided in the node </param> // <returns> </returns> public TreeNode GenerateTreeRoot (List <TreeNode> nodeList) {TreeNode root = new TreeNode (); TreeNode cNode; TreeNode chNode; TreeNode pNode; Stack <TreeNode> stack = new Stack <TreeNode> (); while (nodeList. count> 0) {cNode = nodeList [0]; n OdeList. Remove (cNode); stack. Push (cNode); while (cNode! = Null) {cNode = stack. Pop (); if (chNode = getChildren (cNode, nodeList ))! = Null) {stack. push (cNode); nodeList. remove (chNode); stack. push (chNode);} else {if (stack. count> 0) {pNode = stack. pop (); pNode. children. add (cNode); stack. push (pNode);} else {if (pNode = getParent (cNode, nodeList ))! = Null) {nodeList. remove (pNode); stack. push (pNode); pNode. children. add (cNode);} else {root. children. add (cNode); cNode = null ;}}} return root;} public TreeNode getChildren (TreeNode node, List <TreeNode> list) {return list. find (delegate (TreeNode n) {return n. pid = node. id;});} public TreeNode getParent (TreeNode node, List <TreeNode> list) {return list. find (delegate (TreeNode n) {return n. id = node. pid ;});}}
 

The following is the definition of a node class:

public class TreeNode{    public TreeNode()    {        m_Id = String.Empty;        m_Pid = String.Empty;        m_Text = String.Empty;        m_Children = new List<TreeNode>();    }    public TreeNode(string id, string pid, string text)    {        m_Id = id;        m_Pid = pid;        m_Text = text;        m_Children = new List<TreeNode>();    }    private string m_Id;    public string Id    {        get { return m_Id; }        set { m_Id = value; }    }    private string m_Pid;    public string Pid    {        get { return m_Pid; }        set { m_Pid = value; }    }    private string m_Text;    public string Text    {        get { return

Related Article

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.