How to stop a recursive search

RogerSchlueter-7899 1,236 Reputation points
2020-12-17T21:41:09.273+00:00

I have an recursive Organization Class as follows:

Public Class Organization
    Public Property OrganizationID As Integer
    Public Name as string
    Public Property Subsidiaries As ObservableCollection(Of Organization)
....
End Class

I want a function that, given an OrganizationID, will walk the hierarchy until it finds the Organization then return its name. I have this so far:

Private Function SearchHierarchy(Orgs As ObservableCollection(Of Organization), ID As Integer) As String
    For Each Org As Organization In Orgs
        If Org.OrganizationID = ID Then
            Return Org.Name
        Else
            If Org.Subsidiaries IsNot Nothing Then
                SearchHierarchy(Org.Subsidiaries, ID)
            End If
        End If
    Next
End Function

The problem I'm having is that I cannot figure out how to halt the search when it finds the sought organization. I thought the Return would do that but it only exits the last call to the function, not the whole search.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,644 questions
0 comments No comments
{count} votes

Accepted answer
  1. YASER SHADMEHR 781 Reputation points
    2020-12-18T00:51:01.403+00:00

    You need to check the result of SearchHierarchy and returns it if is not null:

    Private Function SearchHierarchy(Orgs As ObservableCollection(Of Organization), ID As Integer) As String
            For Each Org As Organization In Orgs
                Console.WriteLine("SearchHierarchy Enter :" + Org.Name)
    
                If Org.OrganizationID = ID Then
                    Return Org.Name
                Else
                    If Org.Subsidiaries IsNot Nothing Then
                        Dim result As String = SearchHierarchy(Org.Subsidiaries, ID)
    
                        If Not String.IsNullOrWhiteSpace(result) Then
                            Return result
                        End If
    
                    End If
                End If
                Console.WriteLine("SearchHierarchy Leave:" + Org.Name)
            Next
    
            Return String.Empty
        End Function
    

    Suggestion

    I prefer to write a recursive function on the simple class type, not a collection. For example, in your scenario, I would write SearchHierarchy in this way:

    Private Function SearchHierarchy(Org As Organization, ID As Integer) As String
            If Org.OrganizationID = ID Then
                Return Org.Name
            ElseIf Org.Subsidiaries IsNot Nothing Then
                For Each subOrg As Organization In Org.Subsidiaries
                    Dim result As String = SearchHierarchy(subOrg, ID)
    
                    If Not String.IsNullOrWhiteSpace(result) Then
                        Return result
                    End If
                Next
            End If
    
            Return String.Empty
        End Function
    

    Please note this is a suggestion and I share it just in case you like it too.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful