Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The cost of traversing the tree is <strong><code>n-1</code></strong> where <code>n</code> is the number of nodes.</p> <p>This is true because every tree has exactly <code>n-1</code> edges - and you need to use all of them in order to visit all the nodes.</p> <hr> <p>To be exact it is known that the next 3 statements are equivalent for a <em>graph</em> <code>T</code> with <code>n</code> nodes:</p> <ol> <li>T is a tree (connected without cycles)</li> <li>T is connected and has n-1 edges</li> <li>T has no cycle and has n-1 edges</li> </ol> <p>From the above we can conclude, that in a tree, in order to get to all nodes we must use all edges (because there are no cycles, so no redundant edges) - and there are EXACTLY <code>n-1</code> of those.</p> <hr> <p><strong>EDIT</strong>:</p> <p>From your example it seems you are also counting the times you go back from each edge (i.e. some edges are counted twice).</p> <p>Well, in this case the optimal solution will be:</p> <pre><code>cost = (n-1)*2 - height </code></pre> <p><strong>Explanation/proof guidelines:</strong> <br>There are <code>n-1</code> edges exactly in the tree. Each of them is traversed exactly twice except those that leads from the root to the deepest node. <br>You must use each edge (except the mentioned) exactly twice, because with exception of the last branch - you go back from each node. <br>Since there are exactly <code>height</code> edges in the last branch, you get total of <code>cost = (n-1)*2 - height</code></p> <p>Note that it is basically the same as you got:</p> <pre><code>height + 2*(n-1-height) = height + 2(n-1) -2height = 2(n-1) - height </code></pre>
    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