TreeView build method without recursive (optimization scheme)

xiaoxiao2021-04-07  351

Efficient TreeView construction method

I have always had a friend on the Internet that TreeView on the .Net is not fast enough, and it is not convenient. So what is the real situation? I need a quick Tree in a project, its data source is MS SQL, and the structure recorded in the corresponding table is as follows:

ID FatherID Title 0101 01 Basic Construction Expenditure 010109 0101 Other Basic Construction Expenditures

It is characterized by a string that the ID of the parent node is exactly the length of the current record ID.

So I wrote the following code:

First, ordinary approach

Private function buildentree (Byval DS AS Dataset)

DIM RS AS DATAROWCOLLECTION

DIM R AS DATAROW

DIM ID As String

Dim Fatherid As String

DIM TITLE As String

DIM FN As Treenode

Dim Node as Treenode

RS = ds.tables (0) .ROWS

Bootnode = New Treenode

Bootnode.text = "[0] All Units"

BootNode.tag = "0" 'root directory

Treeen.Nodes.Add (bootnode)

For Each R in Rs

ID = Directcast (R.Item (0), String) .trim 'CODE

Fatherid = Directcast (R.Item (1), String) .trim 'Fathercode

Title = "[" & id & "]" & Directcast (r.Item (2), string) .trim 'Title

'Find the same node as the parent point ID

Fn = findNode (Father)

IF fn is nothing then

'Nodes that did not find the corresponding ID

Else

Node = new TREENODE

With node

.Tag = ID

.Text = Title

End with

Fn.nodes.add (node)

END IF

NEXT

Bootnode.expand ()

END FUNCTION

'Look for the ID match node under the specified node

Private function FindNode (byval n as treeode, byval id as string) as Treenode

DIM NS as TreenodeCollection

Dim Node as Treenode

Dim flag as boolean

DIM STRText As String

Dim returnnode as treenode

Flag = false

IF n.tag = id life

Return N

Else

'If the path is not the same, return

IF (id.Length "0") Andalso (id.substring (0, n.tag.length) <> n.tag)) THEN

Return Nothing

END IF

ns = n.nodes

For Each Node in NS

ReturnNode = FindNode (Node, ID)

If returnnode is nothing then

'do nothing

Else

Flag = TRUE

EXIT Forend IF

NEXT

IF flag = true kiln ing retrnode

END IF

Return Nothing

END FUNCTION

The above code uses the ID value of the current word node to the Node's TAG, and then starts traversal from the root directory. It is found that its efficiency is extremely low after operation. But there are mostly the case online.

Is there any other way to increase its running speed.

God said: "Algorithm either in space change time, either in time."

I was ambiguing at some point. I found that I can provide Hashtable provided by. Net, this is a good stuff.

So how do you use it?

Second, fast way

First we need to add a definition

Private fasthashtable as hashtable

Then you need to make a small adjustment of BuildEntree.

Private function buildentree (Byval DS AS Dataset)

DIM RS AS DATAROWCOLLECTION

DIM R AS DATAROW

DIM ID As String

Dim Fatherid As String

DIM TITLE As String

DIM FN As Treenode

Dim Node as Treenode

RS = ds.tables (0) .ROWS

Bootnode = New Treenode

Bootnode.text = "[0] All Units"

BootNode.tag = "0" 'root directory

Treeen.Nodes.Add (bootnode)

Fasthashtable.Add ("0", bootnode)

For Each R in Rs

ID = Directcast (R.Item (0), String) .trim 'CODE

Fatherid = Directcast (R.Item (1), String) .trim 'Fathercode

Title = "[" & id & "]" & Directcast (r.Item (2), string) .trim 'Title

'Find the same node as the parent point ID

Fn = findNode (Father)

IF fn is nothing then

'Nodes that did not find the corresponding ID

Else

Node = new TREENODE

With node

.Tag = ID

.Text = Title

End with

Fn.nodes.add (node)

FasthashTable.Add (ID, Node)

END IF

NEXT

Bootnode.expand ()

END FUNCTION

As seen from the above code, I just increased the code of the two black body. Its role is to store the new node not only to TreeView, but also save to FashHashTable, whose keyword is ID value, as long as there is no repetition ID Then you can use it.

Next we also need to change FindNode.

'New version fast look

Private function findnode (byval id as string) as Treenode

Return fasthashtable.Item (ID)

END FUNCTION

Ok, now there is a job to do, just to make the foundhashtable to initiate it, try to make the initiator to the same number of data nodes you will build, I am 3000 nodes in this system, so I added the following One sentence:

Fasthashtable = New hashtable (3000) 'The number is now very fast.

Third, post

Even in the .NET era, the data structure is also very useful to us. I am here today!

转载请注明原文地址:https://www.9cbs.com/read-132591.html

New Post(0)