title: Avoiding children nodes appear at same or upper level of parent level
body: <p>I am constructing a map. for that I need to assign a level to each node of the map and I need this levels to be automatically generated. </p>
  
  <p>Currently I have relation between the nodes. (relation as in parent-children)
  In the given example there altogether 23 nodes.</p>
  
  <p>I don't want any children to be at the same level as parent or above the parent level. </p>
  
  <p>I currently have the two dataTables. First contains the node ids and second one contains the levels corresponding to the node ids.</p>
  
  <p>How can I make any adjustment in the below code  so that any child does not come on the same level or above the level of its parent?</p>
  
  <p>Output of the below code is x and y coordinates of the nodes.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace coordinatesGeneration
  {
      class Program
      {
          internal class Node
          {
              public Node Parent { get; set; }
              private Node m_child;
              public Node Child
              {
                  get { return m_child; }
                  set
                  {
                      m_child = value;
                      value.Parent = this;
                  }
              }
              public int Id { get; set; }
              public string Title { get; set; }
          }
  
  
          internal class Program1
          {
              static void Main(string[] args)
              {
                  Dictionary&lt;int, Node&gt; nodes = new Dictionary&lt;int, Node&gt;()
          {
          {0, new Node() {Id = 0, Title = "Node1"}},
          {1, new Node() {Id = 1, Title = "Node2"}},
          {2, new Node() {Id = 2, Title = "Node7"}},
          {3, new Node() {Id = 3, Title = "Node3"}},
          {4, new Node() {Id = 4, Title = "Node4"}},
          {5, new Node() {Id = 5, Title = "Node5"}},
          {6, new Node() {Id = 6, Title = "Node6"}},
          {7, new Node() {Id = 7, Title = "Node8"}},
          {8, new Node() {Id = 8, Title = "Node9"}},
          {9, new Node() {Id = 9, Title = "Node10"}},
          {10, new Node() {Id = 10, Title = "Node11"}},
          {11, new Node() {Id = 11, Title = "Node12"}},
          {12, new Node() {Id = 12, Title = "Node13"}},
          {13, new Node() {Id = 13, Title = "Node14"}},
          {14, new Node() {Id = 14, Title = "Node15"}},
          {15, new Node() {Id = 15, Title = "Node16"}},
          {16, new Node() {Id = 16, Title = "Node17"}},
          {17, new Node() {Id = 17, Title = "Node18"}},
          {18, new Node() {Id = 18, Title = "Node19"}},
          {19, new Node() {Id = 19, Title = "Node20"}},
          {20, new Node() {Id = 20, Title = "Node21"}},
          {21, new Node() {Id = 21, Title = "Node22"}},
          {22, new Node() {Id = 22, Title = "Node23"}}
  
  
      };
              nodes[0].Child = nodes[3];
              nodes[1].Child = nodes[3];
              nodes[2].Child = nodes[4];
              nodes[2].Child = nodes[5];
              nodes[3].Child = nodes[6];
              nodes[3].Child = nodes[7];
              nodes[3].Child = nodes[8];
              nodes[3].Child = nodes[9];
              nodes[6].Child = nodes[10];
              nodes[2].Child = nodes[11];
              nodes[2].Child = nodes[12];
              nodes[7].Child = nodes[13];
              nodes[8].Child = nodes[14];
              nodes[4].Child = nodes[15];
              nodes[5].Child = nodes[15];
              nodes[7].Child = nodes[15];
              nodes[12].Child = nodes[16];
              nodes[13].Child = nodes[16];
              nodes[13].Child = nodes[17];
              nodes[14].Child = nodes[18];
              nodes[8].Child = nodes[19];
              nodes[13].Child = nodes[20];
              nodes[14].Child = nodes[20];
              nodes[8].Child = nodes[21];
              nodes[15].Child = nodes[21];
              nodes[18].Child = nodes[22];
              nodes[19].Child = nodes[22];
  
              int parentlessNodeCounter = 0;
              Dictionary&lt;int, List&lt;Node&gt;&gt; nbParentNodesDictionary = new Dictionary&lt;int, List&lt;Node&gt;&gt;();
              foreach (KeyValuePair&lt;int, Node&gt; valuePair in nodes)
              {   
                  Node parent = valuePair.Value.Parent;
                  int nbOfParent = 0;
  
                  if (valuePair.Value.Parent == null)
                      parentlessNodeCounter++;
                  while (parent != null)
                  {
                      nbOfParent++;
                      parent = parent.Parent;
                  }
                  if (valuePair.Value.Parent == null &amp;&amp; parentlessNodeCounter &gt; 1)
                      nbOfParent ++;
                  if (!nbParentNodesDictionary.ContainsKey(nbOfParent))
                  {
                      nbParentNodesDictionary[nbOfParent] = new List&lt;Node&gt;();
                  }
                  nbParentNodesDictionary[nbOfParent].Add(valuePair.Value);
              }
  
              const int yOffSet = 150;// initial value used for yOffset = 100; 
  
              foreach (KeyValuePair&lt;int, List&lt;Node&gt;&gt; keyValuePair in nbParentNodesDictionary)
              {
                  const int xMax = 1000;// initial value used for xMax = 500; 
                  int xOffset = xMax / (keyValuePair.Value.Count + 1);
                  int x = 0;
                  foreach (Node node in keyValuePair.Value)
                  {
                      x += xOffset;
                      Console.Write("id:" + node.Id + " title:" + node.Title + " x:" + x + " y:" + yOffSet * keyValuePair.Key);
                  }
              }
  
          }
      }
      }
  }
  </code></pre>