Showing posts with label Recursive method. Show all posts
Showing posts with label Recursive method. Show all posts

Thursday, 28 August 2014

Another example of recursive method.


Print menus recursively (you can amend the code according to your needs of course)


This is the method you would call to get the menus printed indented according to their level in the hierarchy in a console application

PrintMenus(GenerateAllMenus());


private static string menuLevel = string.Empty; ///
/// Prints the menus. /// /// The menus. private static void PrintMenus(IEnumerable menus) { menuLevel += " "; foreach (var menu in menus) { Console.WriteLine(string.Concat(menuLevel, menu.MenuTitle)); var subMenu = GetSubMenu(menu); if (subMenu != null) PrintMenus(subMenu); } menuLevel = menuLevel.Substring(0, menuLevel.Length - 5); }



///
/// Generates all menus. /// /// private static IEnumerable GenerateAllMenus() { var menus = new List(); var fileMenu = new HeirarchicalMenu { MenuTitle = "File", SubMenus = new List { new HeirarchicalMenu { MenuTitle = "New", SubMenus = new List { new HeirarchicalMenu { MenuTitle = "Class / Hypermodule" } } }, new HeirarchicalMenu { MenuTitle = "Open", SubMenus = new List { new HeirarchicalMenu { MenuTitle = "Class / Hypermodule", SubMenus = new List { new HeirarchicalMenu { MenuTitle = "5th Level", SubMenus = new List { new HeirarchicalMenu { MenuTitle = "6th Level", SubMenus = new List { new HeirarchicalMenu { MenuTitle = "7th Level" } } } } } } } } }, new HeirarchicalMenu { MenuTitle = "Exit" } } }; menus.Add(fileMenu); var editMenu = new HeirarchicalMenu { MenuTitle = "Edit Menu", SubMenus = new List { new HeirarchicalMenu { MenuTitle = "Cut" }, new HeirarchicalMenu { MenuTitle = "Copy", SubMenus = new List { new HeirarchicalMenu { MenuTitle = "Deep Copy" }, new HeirarchicalMenu { MenuTitle = "Shallow Copy" } } }, new HeirarchicalMenu { MenuTitle = "Paste" } } }; menus.Add(editMenu); var viewMenu = new HeirarchicalMenu { MenuTitle = "View Menu", SubMenus = new List { new HeirarchicalMenu { MenuTitle = "Cascade" }, new HeirarchicalMenu { MenuTitle = "Horizontal" }, new HeirarchicalMenu { MenuTitle = "Vertical" } } }; menus.Add(viewMenu); return menus; } /// /// Gets the sub menu. /// /// The menu. /// private static IEnumerable GetSubMenu(HeirarchicalMenu menu) { if (menu.SubMenus != null && menu.SubMenus.Count > 0) return menu.SubMenus; return null; } public class HeirarchicalMenu { /// /// Gets or sets the menu title. /// /// /// The menu title. /// public string MenuTitle { get; set; } /// /// Gets or sets the sub menus. /// /// /// The sub menus. /// public List SubMenus { get; set; } /// /// Initializes a new instance of the class. /// public HeirarchicalMenu() { if (SubMenus == null) SubMenus = new List(); } }



Hope its helpful.


Example of using recursive methods.




The task is to get the Fibonacci series printed in a recursive method.

Its output is every number is a total of previous two numbers i.e. 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610



static int Fibonacci(int value)
{
if (value == 0 || value == 1)
return value;
return (Fibonacci(value - 1) + Fibonacci(value - 2));
}


in the above code value represents number of permutations we would like to get this series generated for. The output printed before the code is for 15 permutations.