title: organize nodes in a list of data with tree structure
body: <p>I'm using typescript (for an angular project) and I have a class like this:</p>
  
  <pre><code>class Encyc {
    constructor(id: number, parentId: number) {
      this.id = id;
      this.parentId = parentId;
    }
  
    // these fields come from server side
    private _id: number;
    private _parentId: number;
  
    // extra variable
    private _hasChildren: boolean;
    private _children: Encyc[];
  
    // getters and setters
  }
  </code></pre>
  
  <p>imagine that I fetch a list like this from server:</p>
  
  <pre><code>[
    Encyc(1, null),
    Encyc(2, null),
    Encyc(3, 1),
    Encyc(4, 1),
    Encyc(5, 3),
    Encyc(6, 2),
    Encyc(7, 4),
    Encyc(8, null),
    Encyc(9, 2),
    Encyc(10, 5)
  ]
  </code></pre>
  
  <p>now I need a method to get this data, set children of each parent and return a list like this:</p>
  
  <pre><code>[
    Encyc(1, null, true, [
      Encyc(3, 1, true, [
        Encyc(5, 3, true, [
          Encyc(10, 5, false, [])
        ])
      ]),
  
      Encyc(4, 1, true, children[
        Encyc(7, 4, false, [])
      ])
    ]),
  
    Encyc(2, null, true, [
      Encyc(6, 2, false, []),
      Encyc(9, 2, false, [])
    ]),
  
    Encyc(8, null, false, [])
  ]
  </code></pre>
  
  <p>at the first, I got a list of 10 items in it, then (after the function that I need) the list will contain just 3 items. the first one has 2 direct children and all of its children counts is 5 </p>
  
  <p>so is there any library or algorithms for something like this?</p>