Author: Xingda Computer technology company (www.study01job.com) Guo Baoli
Second, add to what position
After an article, we continue to discuss how to determine where to add a node. This is actually a tree-structured traversal problem. The traversal of a tree structure is usually implemented using a recursive program, so this section gives a search by content, traverses the tree structure, and returns the handle of the node if it is equal to the content to be found. The insert completes after the return, using the knowledge described in the previous article to be implemented. The functions are as follows:
/*-----------------------------------------------------------------------------
* Function Name: Long Wf_reader (Long al_handle,string as_target)
* Function: traverse the tree structure to find the node of the specified content.
* Return value: The handle of the node.
* Algorithm Description: Traversing the tree structure, using recursion to complete.
* Date of preparation: 2004.6.26
* Author: da Tong Xing Computer technology Guo Baoli
*----------------------------------------------------------------------------*/
Long Ll_handle
Long Ll_handleold
TreeViewItem Ltvi_item
String Ls_label
String Ls_data
If Tv_1.getitem (al_handle,ltvi_item) =-1 Then return-1
Ls_label=trim (String (Ltvi_item.label))
Ls_data=trim (String (Ltvi_item.data))
If Ls_label = As_target Then
Return Al_handle
Else
Ll_handle = Tv_1.finditem (childtreeitem!,al_handle)
End If
If Ll_handle > 0 Then
If Tv_1.getitem (ll_handle,ltvi_item) =-1 then return 0
Ls_label=trim (String (Ltvi_item.label))
Ls_data=trim (String (Ltvi_item.data))
If Ls_label = As_target Then
Return ll_handle//found the node to add
Else
Return Wf_reader (Ll_handle,as_target)
End If
Else
Ll_handleold = Al_handle
Ll_handle = Tv_1.finditem (nexttreeitem!,ll_handleold)
Do While Ll_handle < 0
Ll_handle = Tv_1.finditem (parenttreeitem!,ll_handleold)
If Ll_handle > 0 Then
Ll_handleold = Ll_handle
Ll_handle = Tv_1.finditem (nexttreeitem!,ll_handleold)
Else
Ll_handle = Tv_1.finditem (nexttreeitem!,ll_handle)
If Ll_handle < 0 Then
Return-1
End If
End If
Loop
If Tv_1.getitem (ll_handle,ltvi_item) =-1 Then return-1
Ls_label=string (Ltvi_item.label)
Ls_data=string (Ltvi_item.data)
If Ls_label = As_target Then
Return ll_handle//found the node to add
Else
Return Wf_reader (Ll_handle,as_target)
End If
End If
Return Ll_handle
invocation example. For example, we put a single-line editor sle_1 on the window, the user enters the content, and clicks the command button ' find ' to call the above function:
String Ls_find
Long Ll_handle
Ls_find = Trim (Sle_1.text)
Ll_handle = Tv_1.finditem (roottreeitem!, 0)
If Ll_handle > 0 Then
Ll_handle = Wf_reader (ll_handle,ls_find)
If Ll_handle > 0 Then
MessageBox (' Find ', String (Ll_handle))
Else
MessageBox (', ' not found ')
End If
End If