Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Look at it this way:</p> <pre><code>0 00 000 | (0,0) (0,1) (0,2) 001 | (1,2) 1 10 100 | (2,0) (2,1) (2,2) 101 | (3,2) 11 | (4,1) </code></pre> <p>I used the same syntax as you to name the nodes (left side) and the corresponding position "(row,column)" into the grid (right side). There are two top level departments here, 0 and 1. You can label your nodes with "(x,y)" coordinates with a depth first visit for every tree/top level department. Whenever you descend from father to child, you have to increase the column number by 1. Every time you visit a new sibling your row index must refer to a new, empty one (in the example, the sibling of 10 is 11 but you can't put it in (3,1) because row 3 is already occupied by dept 101).</p> <p>I would write the algorithm following these guidelines. Keep track of the current (x,y) with two variables that are passed down as parameters of a recursive depth-first visit, together with the current node/department to be visited. Make sure that the function edits as required the excel representation but returns the maximum row index used (so that you can use it to know which is the following row when visiting a new sibling -- as in the previous example). Every time you make a recursive call you have to call it with coords (x, y+1) because it's a new column. In a similar manner, when you visit a new sibling you call it with (coords prev_call_retn+1, y) because it's a new row (where coords_prev_call_retn is the value of the last recursive call on the previous sibling). An helper function will call the recursive one on the root node and using as base coords (0,0), or whatever you like to start from.</p> <pre><code>#include &lt;iostream&gt; #include &lt;list&gt; #include &lt;string&gt; using namespace std; class Dept { public: string id; list&lt;Dept*&gt; *subDepts; Dept(string id) { this-&gt;id = id; this-&gt;subDepts = new list&lt;Dept*&gt;(); } Dept *addChild(Dept *d) { this-&gt;subDepts-&gt;push_back(d); return d; } Dept *addChild(string id) { Dept *d = new Dept(id); return this-&gt;addChild(d); } }; void visit(Dept *d, int row, int col) { cout &lt;&lt; "Dept " &lt;&lt; d-&gt;id &lt;&lt; ": (" &lt;&lt; row &lt;&lt; ", " &lt;&lt; col &lt;&lt; ")\n"; } int label(Dept *d, int row, int col) { if (d == 0) return row; visit(d, row, col); list&lt;Dept*&gt; *lst = d-&gt;subDepts; for (list&lt;Dept*&gt;::iterator it = lst-&gt;begin() ; it != lst-&gt;end(); it++) { row = label(*it, row, col+1) + 1; } if (lst-&gt;size() &gt; 0) row--; return row; } int label(Dept *d) { label(d, 0, 0); } </code></pre> <p>In this C++ code, label() is the function you're interested in.</p> <p>The parameters row and col are supposed to be the correct coordinates of the department *d, so that we can visit the node immediately.</p> <p>The loop recursively calls label() on each children (if any). Note that I use the variable row in order to keep track of the last used row + 1.</p> <p>Lastly, we have to return the last used row by the function, but being careful to subtract one if the d->subDepts is not empty. This is because the for loop that adds 1 extra row in the last iteration.</p> <p>Here is an example main:</p> <pre><code>int main(int argc, char **argv) { Dept *d0 = new Dept("0"); Dept *d00 = d0-&gt;addChild("00"); Dept *d01 = d0-&gt;addChild("01"); Dept *d000 = d00-&gt;addChild("000"); Dept *d001 = d00-&gt;addChild("001"); Dept *d002 = d00-&gt;addChild("002"); Dept *d010 = d01-&gt;addChild("010"); Dept *d02 = d0-&gt;addChild("02"); label(d0); return 0; } </code></pre> <p>And here the result:</p> <pre><code>bash-4.2$ g++ dept.cpp &amp;&amp; ./a.out Dept 0: (0, 0) Dept 00: (0, 1) Dept 000: (0, 2) Dept 001: (1, 2) Dept 002: (2, 2) Dept 01: (3, 1) Dept 010: (3, 2) Dept 02: (4, 1) </code></pre> <p>I hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload