Tree structure construction without two iterations

Source: Internet
Author: User

 

The idea of this algorithm is:

Step 1: cyclically complete treenode Construction

Build the hierarchy in the second cycle

Data storage uses a general Hierarchical Storage Method to call the database at a time to complete all required data. The array index storage tree node and parentsqn avoid the disadvantages of search performance.

Table:

Inclumentid uniqueidentifier,
Departmentsqn int,
Departmentname nvarchar (32 ),
Parentid uniqueidentifier

Stored Procedure:

Create procedure DBO. department_selectcore
As
Set nocount on
Select inclumentid, departmentsqn, departmentname, parentid,
(Select departmentsqn from department parent where (parent. Partition mentid = Department. parentid)
Parentsqn
From Department
Order by departmentsqn
Return

Data Call code:

[Dataobject (true)]
Public class departmentdataobject
{
[Dataobjectmethod (dataobjectmethodtype. Select, true)]
Public static dataset readcore ()
{
Database DB = databasefactory. createdatabase ("workitemtrack ");
String sqlcommand = "department_selectcore"
Dbcommand = dB. getstoredproccommand (sqlcommand );
DB. addreturnvalueparameter (dbcommand );
Return dB. executedataset (dbcommand );
}
}

Data Structure Construction Code:

 

Protected void buildtree ()
{
_ Departmenttreeview. nodes. Clear ();
Dataset DS = departmentdataobject. readcore ();
Int COUNT = Ds. Tables [0]. Rows. count;
Treenode [] nodes = new treenode [count + 1];
Nullable <int> [] parentsqns = new nullable <int> [count + 1];
Foreach (datarow DR in DS. Tables [0]. Rows)
{
Nodes [(INT) Dr ["departmentsqn"])] = new treenode (string) Dr ["departmentname"], (guid) Dr ["inclumentid"]). tostring ());
Try
{
Parentsqns [(INT) Dr ["departmentsqn"] = (INT) Dr ["parentsqn"];
}
Catch
{
Parentsqns [(INT) Dr ["departmentsqn"] = NULL;
}
}
For (INT I = 1; I <count + 1; I ++)
{
If (parentsqns [I]. hasvalue)
{
Nodes [parentsqns [I]. value]. childnodes. Add (nodes [I]);
}
Else
{
_ Departmenttreeview. nodes. Add (nodes [I]);
}
}
}

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.