Search code examples
c#data-structuresrecursion

C#: recursive search in two different data structures


I need to perform a search in two different data structures in C#, and here's the deal: I have one name (which is a string) and I want to perform a search. I have a function called Exists which will return a bool indicating whether it exists or not.

In case it exists, I increase the name (simply adding a 1 at the end of the string), and then I need to perform the search again (via method exists) to see if an object with the new name exists.

This would go on until there's an unused name, which I could use, BUT, in case it doesn't exist, now I should perform a search another data structure which contains the objects that were deleted, and if the string is found there, then I'd have to increase the name again, and start searching since the beginning. This would all end in case there's no object with such name neither using Exists method nor in the data structure where all the deleted objects are.

How could I approach this problem?

I hope I expressed myself clearly :-)

Thanks a lot in advance!


Solution

  • a non recursive and simple solution could be something like this ( I don't see any need of recursion in this case)

       //pseudocode
    
        String name;
        bool condition = true;
    
        while(condition)
        {
            if(ExistInFirstDataStructure(name))
            {
               //increment name
            }
            else
            {
               if(ExistInDeletedDataStructure(String name))
               {
                   //increment name
               }
               else
               {
                   condition = false;
               }
    
            }
        }
    
    
        bool ExistInFirstDataStructure(String name)
        {
    
        }
    
        bool ExistInDeletedDataStructure(String name)
        {
    
        }