c# - Search algorithm in n-ary tree -


i trying implement search algorithm n-ary tree. following code have written:

public class employee {      public int employeeid { get; set; }     public string level { get; set; }     public int billcode { get; set; }      public virtual list<employee> reportsto { get; set; } } 

i need bfs on tree find element in child nodes , stop recursion when element found in tree. code wrote far is:

static bool closestmanager(employee a, employee b) //a contains tree elements , b 1 need find     {       if (a.employeeid == b.employeeid)             return true;         if (a.reportsto != null)         {             foreach (var curremployee in a.reportsto)             {                 closestmanager(curremployee, b);             }         }         return false;     } 

this 1 returns false if element exists in subtree. because of return false in end. if remove compiler error saying code path must return value.

how stop recursion once element found in tree ?

just return true if closestmanager found in recursive call:

static bool closestmanager(employee a, employee b) //a contains tree elements , b 1 need find {   if (a.employeeid == b.employeeid)     return true;   if (a.reportsto != null)   {     foreach (var curremployee in a.reportsto)     {       if (closestmanager(curremployee, b))          return true;     }   }   return false; 

}


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -