Click here to Skip to main content
15,898,373 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: how to convert an array of records into a hierarchical structure Pin
makumazan8429-Jul-10 3:03
makumazan8429-Jul-10 3:03 
GeneralRe: how to convert an array of records into a hierarchical structure Pin
harold aptroot29-Jul-10 3:04
harold aptroot29-Jul-10 3:04 
GeneralRe: how to convert an array of records into a hierarchical structure Pin
makumazan8429-Jul-10 6:01
makumazan8429-Jul-10 6:01 
GeneralRe: how to convert an array of records into a hierarchical structure Pin
harold aptroot29-Jul-10 6:07
harold aptroot29-Jul-10 6:07 
GeneralRe: how to convert an array of records into a hierarchical structure Pin
harold aptroot2-Aug-10 4:51
harold aptroot2-Aug-10 4:51 
AnswerRe: how to convert an array of records into a hierarchical structure Pin
T M Gray2-Aug-10 8:41
T M Gray2-Aug-10 8:41 
GeneralRe: how to convert an array of records into a hierarchical structure Pin
harold aptroot2-Aug-10 8:56
harold aptroot2-Aug-10 8:56 
GeneralRe: how to convert an array of records into a hierarchical structure Pin
getroshan6-Sep-10 21:47
getroshan6-Sep-10 21:47 
Hope this code helps. Its a generic implementation to render hierarchical objects using recursion.

<br />
  public interface IHierarchy<T><br />
        {<br />
            string Name { get; set; }<br />
            List<T> Relations { get; set; }<br />
        }<br />
<br />
        public class Employee : IHierarchy<Employee><br />
        {<br />
            private string name;<br />
<br />
            public string Name<br />
            {<br />
                get { return name; }<br />
                set { name = value; }<br />
            }<br />
            private List<Employee> relations;<br />
<br />
            public List<Employee> Relations<br />
            {<br />
                get { return relations; }<br />
                set { relations = value; }<br />
            }<br />
<br />
        }<br />
<br />
    public class Hierarchy<T> where T : IHierarchy<T><br />
        {<br />
            private List<T> hierarchylist;<br />
            public Hierarchy(List<T> list)<br />
            {<br />
                hierarchylist = list;<br />
            }<br />
<br />
          <br />
            private string Replicate(string s, int count)<br />
            {<br />
                string output = string.Empty;<br />
                for (int i = 0; i < count; i++)<br />
                {<br />
                    output += s;<br />
                }<br />
                return output;<br />
            }<br />
            public void Render()<br />
            {<br />
                Render(hierarchylist, 0);<br />
            }<br />
            private void Render(List<T> list, int Level)<br />
            {<br />
<br />
                foreach (T emp in list)<br />
                {<br />
<br />
                    Console.WriteLine(Replicate("-", Level) + emp.Name);<br />
                    if (emp.Relations != null)<br />
                    {<br />
                        int NextLevel = Level + 1;<br />
                        Render(emp.Relations, NextLevel);<br />
                    }<br />
<br />
                }<br />
<br />
            }<br />
<br />
         <br />
        }<br />
private void button1_Click(object sender, EventArgs e)<br />
        {<br />
<br />
            #region Heirarchy<br />
            List<Employee> c4 = new List<Employee>();<br />
            c4.Add(new Employee { Name = "C41", Relations = null });<br />
            c4.Add(new Employee { Name = "C42", Relations = null });<br />
<br />
            List<Employee> c1 = new List<Employee>();<br />
            c1.Add(new Employee { Name = "C11", Relations = null });<br />
            c1.Add(new Employee { Name = "C12", Relations = null });<br />
<br />
<br />
            List<Employee> p1 = new List<Employee>();<br />
            p1.Add(new Employee { Name = "C1", Relations = c1 });<br />
            p1.Add(new Employee { Name = "C2", Relations = null });<br />
            List<Employee> p3 = new List<Employee>();<br />
            p3.Add(new Employee { Name = "C3", Relations = null });<br />
            p3.Add(new Employee { Name = "C4", Relations = c4 });<br />
<br />
            List<Employee> employees1 = new List<Employee>();<br />
            employees1.Add(new Employee { Name = "P1", Relations = p1 });<br />
            employees1.Add(new Employee { Name = "P2", Relations = null });<br />
            employees1.Add(new Employee { Name = "P3", Relations = p3 });<br />
            Hierarchy<Employee> empH = new Hierarchy<Employee>(employees1);<br />
            empH.Render();<br />
            #endregion<br />
<br />
         <br />
<br />
           <br />
        }<br />

AnswerRe: how to convert an array of records into a hierarchical structure Pin
Andrew Rissing11-Oct-10 4:12
Andrew Rissing11-Oct-10 4:12 
GeneralAutomatic Assignment Pin
alkowarizmi27-Jul-10 14:38
alkowarizmi27-Jul-10 14:38 
AnswerRe: Automatic Assignment Pin
Luc Pattyn27-Jul-10 14:58
sitebuilderLuc Pattyn27-Jul-10 14:58 
GeneralRe: Automatic Assignment Pin
alkowarizmi28-Jul-10 3:47
alkowarizmi28-Jul-10 3:47 
GeneralRe: Automatic Assignment Pin
Luc Pattyn28-Jul-10 4:04
sitebuilderLuc Pattyn28-Jul-10 4:04 
GeneralRe: Automatic Assignment Pin
Yusuf28-Jul-10 4:07
Yusuf28-Jul-10 4:07 
GeneralRe: Automatic Assignment Pin
getroshan7-Sep-10 0:44
getroshan7-Sep-10 0:44 
QuestionWANTED: Programmer looking for an algorithmic challenge PinPopular
Xpnctoc23-Jul-10 5:58
Xpnctoc23-Jul-10 5:58 
AnswerRe: WANTED: Programmer looking for an algorithmic challenge Pin
PIEBALDconsult23-Jul-10 17:01
mvePIEBALDconsult23-Jul-10 17:01 
AnswerRe: WANTED: Programmer looking for an algorithmic challenge Pin
ProtoBytes24-Jul-10 11:57
ProtoBytes24-Jul-10 11:57 
GeneralRe: WANTED: Programmer looking for an algorithmic challenge Pin
Xpnctoc24-Jul-10 12:09
Xpnctoc24-Jul-10 12:09 
AnswerRe: WANTED: Programmer looking for an algorithmic challenge Pin
ProtoBytes24-Jul-10 12:56
ProtoBytes24-Jul-10 12:56 
GeneralRe: WANTED: Programmer looking for an algorithmic challenge Pin
Xpnctoc25-Jul-10 8:29
Xpnctoc25-Jul-10 8:29 
GeneralRe: WANTED: Programmer looking for an algorithmic challenge Pin
ProtoBytes25-Jul-10 8:59
ProtoBytes25-Jul-10 8:59 
GeneralRe: WANTED: Programmer looking for an algorithmic challenge Pin
Xpnctoc25-Jul-10 10:45
Xpnctoc25-Jul-10 10:45 
AnswerRe: WANTED: Programmer looking for an algorithmic challenge [modified] Pin
Luc Pattyn25-Jul-10 12:12
sitebuilderLuc Pattyn25-Jul-10 12:12 
AnswerRe: WANTED: Programmer looking for an algorithmic challenge Pin
Member 419459325-Jul-10 12:52
Member 419459325-Jul-10 12:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.