Implement generic tree traversal using recursion and Delegation

Source: Internet
Author: User
Tree is a common data structure, and traversal is the most common access mode.
. Net. If generics are used, the following method will be more generic. This document does not use generics.

/// <Summary>
/// Delegate-process subnodes
/// </Summary>
1) Public Delegate void dealwith (string Str );

/// <Summary>
/// Delegate-obtain the subnode
/// </Summary>
2) Public Delegate string [] getchilds (string Str );

/// <Summary>
/// Recursive access To the tree data structure
/// </Summary>
/// <Param name = "root"> node to be accessed </param>
/// <Param name = "GCS"> </param>
/// <Param name = "DW"> </param>
3) Public static void getallchilds (string root, getchilds GCS, dealwith DW)
{
DW (Root );
String [] child = GCS (Root );
For (INT I = 0; I <child. length; I ++)
{
Getallchilds (child [I], GCS, DW );
}
}

1) Processing of accessed nodes, such as display or filling in the list
2) It mainly indicates a delegate for obtaining its lower-level nodes. Here it is a delegate that returns a String Array
3) The main function used to traverse nodes.

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.