Parent and Child node tree data output

Source: Internet
Author: User

When I was a senior, I was about to submit my resume and look for a job. Today I just met a C # interview question. I generally felt good about myself. I wanted to solve it three times, five times, and two times. I didn't expect it to take nearly an hour to solve it.

 

As described above, we can draw the following requirements from the question:

1. The ID is auto-increment, And the FID of the first-level node is 0.

2. Show nodes with 1 are displayed. Otherwise, hidden nodes are displayed.

3. subnodes under the same parent node. If the showder is the same, the IDs are not displayed

4. The question seems to have only three levels of nodes output, but I think level 4, Level 5, or even level N should be able to output, so we should implement it using recursive methods.

Considering the requirements of the above questions, my solution is as follows. The code below is implemented in Visual Studio 2008. For other versions of Vs, see the database creation process.

1. Since C # Is Object-Oriented, we should give full play to its object-oriented advantages. So I created a class node to represent a data node.

 

Code

 Using system. Collections. Generic;

Namespace interview offertest. Model
{
Public class Node
{
Public int ID {Get; set ;}
Public int FID {Get; set ;}
Public String nodename {Get; set ;}
Public int showder {Get; set ;}
Public int show {Get; set ;}
Public list <node> childnodes {Get; set;} // list of subnodes under this node
Public bool flag {Get; set;} // indicates whether the node has been output
Public node ()
{
Childnodes = new list <node> (); // allocate space for the list

}
}
}

2. Read the data initialization node list

 

Code

 Static void main (string [] ARGs)
{
VaR nodelist = new list <node> (); // The entire node list
VaR connstr = @ "Data Source =. \ sqlexpress; attachdbfilename = F: \ C # \ interview offertest \ mytest. MDF; Integrated Security = true; user instance = true "; // write the connection string to death for convenience. You 'd better put it in the configuration file ......
If (string. isnullorempty (connstr ))
Return;

Using (VAR conn = new sqlconnection (connstr ))
{
If (conn. State = connectionstate. Closed)
Conn. open ();
VaR sqlstr = "select * From treetest order by ID ";
VaR comm = new sqlcommand (sqlstr, Conn );
VaR reads = comm. executereader ();

While (reads. Read ())
{
VaR node = new node
{
Id = reads. getint32 (0 ),
FID = reads. getint32 (1 ),
Nodename = reads. getstring (2 ),
Showder = reads. getint32 (3 ),
Show = reads. getint32 (4 ),
Flag = false

};


Nodelist. Add (node );

If (node. FID! = 0) // if it is not a level-1 node, a parent node should exist. Add the node to the parent node's subnode list.
{
VaR parentnode = nodelist. firstordefault (P => P. ID = node. FID); // find the parent node
If (parentnode = NULL) return;
If (parentnode. childnodes. firstordefault (P => P. showder = node. showder) = NULL)
Parentnode. childnodes. Add (node );
Else
{// Do not display the same showder
Node. Show = 0;
}
}


}

}

Foreach (VAR row in nodelist. orderby (P => P. ID ))
{
Printtree (row, 0); // recursively outputs node Information

}
Console. Read ();


}

 

 

 

3. Write recursive functions

 

Code

 
Private Static void printtree (node, int level)
{
// Node indicates the node to be output, and level indicates its depth, that is, its level-1
If (node = NULL) return;
If (node. Show = 1 & node. Flag = false)
{
VaR COUNT = level;
While (count> 0)
{
Console. Write ("| ");
Count --;


}
Console. writeline ("| ----" + node. nodename );
Node. Flag = true; // set whether the node has been output.

}

If (node. childnodes! = NULL)
{
Foreach (VAR item in node. childnodes. orderby (P => P. showder ))
{
Printtree (item, level + 1); // recursion next node
}

}

}

4. Running result

1005-01-01-01 is the level-4 node that I add to the test recursion. This shows that the recursive result is correct.

 

To sum up, it seems that the interview questions are not easy. However, you still need to make up your knowledge. Haha, joke, smile ......

 

 

 

 

 

 

 

 

 

 

 

 

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.