Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@Vlion:<br> It should be a pointer to the left/right/root pointers (i.e. a double pointer), so the posted code is correct, although somewhat unclear.</p> <p>@Doug:<br> Consider changing your insert function thus:</p> <pre><code>template &lt;class T&gt; void BT&lt;T&gt;::insert(struct Node&lt;T&gt;** root, struct Node&lt;T&gt;* newNode) { if (*root == NULL) { cout &lt;&lt; "Root Found" &lt;&lt; newNode-&gt;data &lt;&lt; endl; *root = newNode; } </code></pre> <p>It makes clear your intention that you'll be changing the pointer passed as the first parameter (or rather, the pointer whose address will be passed as the first parameter.) It will help avoid confusion such as the one that just happened.</p> <p>The calls to this insert(), such as:</p> <pre><code>insert(&amp;root, newNode); </code></pre> <p>will also reflect your intention of changing the pointer's value. This is a matter of style, though, so I can't argue if you don't want to change.</p> <hr> <p>As for checking whether the tree is "correct," why not draw it out and see for yourself? Something along the lines of:</p> <pre><code>template class&lt;T&gt; void printTree(struct Node&lt;T&gt;* node, int level=0) { if (!node) { for (int i=0; i&lt;level; ++i) cout &lt;&lt; " "; cout &lt;&lt; "NULL" &lt;&lt; endl; return; } printTree(node-&gt;left, level+1); for (int i=0; i&lt;level; ++i) cout &lt;&lt; " "; cout &lt;&lt; node-&gt;data &lt;&lt; endl; printTree(node-&gt;right, level+1); } </code></pre> <p>(Untested code)</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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