Ken H

Ken H

  • NA
  • 646
  • 361.8k

Recursive and non-recursive algorithm transformation in C#

Feb 10 2015 9:10 AM
 Hello friend,
       How to will the recursive convert a non-recursive algorithm(I.e., a normal loop)?
 
 My codes:
 
[Serializable]
public class MyModel{
public MyModel() { }

public MyModel(int id, string displayName, int precursorId){
this.Id = id;
this.DisplayName = displayName;
this.PrecursorId = precursorId;
}
public int Id { get; set; }
public string DisplayName { get; set; }
public int PrecursorId { get; set; }
}
public class Program{
static void InitializeCollection(System.Collections.ObjectModel.Collection<MyModel> collection) {
collection.Add(new MyModel(1, "Programming Languages", 0));
collection.Add(new MyModel(2, "Database", 0));
collection.Add(new MyModel(3, "Script Languages", 0));
collection.Add(new MyModel(4, "C", 1));
collection.Add(new MyModel(5, "C++", 1));
collection.Add(new MyModel(6, "C#", 1));
collection.Add(new MyModel(7, "Java", 1));
collection.Add(new MyModel(8, "VB", 1));
collection.Add(new MyModel(9, "Delphi", 1));
collection.Add(new MyModel(10, "Delegate And Event", 6));
collection.Add(new MyModel(11, "Interface", 6));
collection.Add(new MyModel(12, "Oracle", 2));
collection.Add(new MyModel(13, "DB2", 2));
collection.Add(new MyModel(14, "MS SQL Server", 2));
collection.Add(new MyModel(15, "JavaScript", 3));
collection.Add(new MyModel(16, "jQuery", 3));
collection.Add(new MyModel(17, "PHP", 1));
collection.Add(new MyModel(18, "Pointer", 5));
collection.Add(new MyModel(19, "LINQ", 6));
collection.Add(new MyModel(20, "Lambda", 6));
collection.Add(new MyModel(21, "LINQ to SQL", 19));
collection.Add(new MyModel(22, "LINQ to Object", 19));
collection.Add(new MyModel(23, "LINQ to XML", 19));
collection.Add(new MyModel(24, "MySql", 2));
collection.Add(new MyModel(25, "Algorithm", 0));
collection.Add(new MyModel(26, "Linked List", 25));
collection.Add(new MyModel(27, "Stack", 25));
collection.Add(new MyModel(28, "Queue", 25));
collection.Add(new MyModel(29, "String(KMP Algorithm)", 25));
collection.Add(new MyModel(30, "Binary Tree", 25));
collection.Add(new MyModel(31, "Graphs", 25));
collection.Add(new MyModel(32, "Class Template", 5));
collection.Add(new MyModel(33, "Friend Function", 5));
collection.Add(new MyModel(34, "JDBC", 7));
collection.Add(new MyModel(35, "Pure virtual function", 5));

}

static void Main(string[] args){
System.Collections.ObjectModel.Collection<MyModel> collection = new System.Collections.ObjectModel.Collection<MyModel>();

InitializeCollection(collection);
GetParentNode(collection);

Console.ReadKey(true);
}

static void GetParentNode(System.Collections.ObjectModel.Collection<MyModel> collection){
foreach (var p in collection){
if (p.PrecursorId==0){
Console.WriteLine(p.DisplayName);
GetChildrenNode(p.Id,collection, "..");
}
}
}

static void GetChildrenNode(int precursorId, System.Collections.ObjectModel.Collection<MyModel> collection, string spacer){

if (precursorId==-1) return;

foreach (var c in collection){
if (c.PrecursorId == precursorId) {
Console.WriteLine(spacer + c.DisplayName);
GetChildrenNode(c.Id, collection, spacer + "..");
}
}

GetChildrenNode(-1, collection, "..");
}

 
 
Thank in advance. :)

Answers (3)