public int RecurentFind(int source) { if (Level == 1) { Description.AppendLine($@" ================================================== | We found factorial for {source, 2} and show | | how calls of functions stack in memory. | | Each function in call stack will be descripted | ================================================== "); } if (source != 1) { Description.AppendLine($"{ConsoleExtensions.MakeLeftPadding(Level)} Recurent level: {Level,2} source: {source}"); Level++; int nextResult = RecurentFind(source - 1); int currentResult = source * nextResult; Level--; Description.AppendLine($"{ConsoleExtensions.MakeLeftPadding(Level)} Back to level: {Level,2} result: {currentResult} = {source} * {nextResult}"); return(currentResult); } else { Description.AppendLine($"{ConsoleExtensions.MakeLeftPadding(Level)} BASE CASE: recursion End, now function run back on call stack"); return(source); } }
public List <int> RecurentWalk(Node root, string nodeType = "root") { List <int> result = new List <int>(); if (Level == 1) { Description.AppendLine($@" ================================================= | We show recurent walking through binary tree | | At first we look at left side and if we have | | left node we move there. Then we look at rght | | and if we have right node - move there. Then | | we look at current node to take it value and | | move to the root | ================================================= "); } if (root.Left != null) { Level++; Description.Append($"|({nodeType}) -> Left |"); var left = RecurentWalk(root.Left, "left"); result.AddRange(left); } if (root.Right != null) { Level++; Description.Append($"|({nodeType}) -> Right |"); var right = RecurentWalk(root.Right, "right"); result.AddRange(right); } result.Add(root.Data); Description.AppendLine($"|({nodeType}) = Got: {root.Data}|"); Description.AppendLine($@"{ConsoleExtensions.MakeLeftPadding(Level)} | Recurent level: {Level} | All values: {result.Represent()} -------------------------------------------------------------------------------------------------------------------"); Level--; return(result); }