Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Binary Search Tree Insert via Recursion
    text
    copied!<p>So my code is below. I'm not getting any errors and it places everything in the node just fine. But based on my debug statements Everytime anything is inserted it's finding the root. I'm not sure if that is right. But according to output file for the assignment, my answers are different when it comes to the height of the tree, the traversals, and I just flat am still having troubles with my leaf count function. Another story though.</p> <p>Based on the debug statements it looks like everything is going right where they should. But I figure I might need fresh eyes. I don't see how my traversals could change at all since it is really only a matter of where I'm proccessing the node that should effect the Inorder, preorder, and postorder. </p> <pre><code>template &lt;class T&gt; void BT&lt;T&gt;::insert(const T&amp; item) { Node&lt;T&gt;* newNode; newNode = new Node&lt;T&gt;(item); insert(root, newNode); } template &lt;class T&gt; void BT&lt;T&gt;::insert(struct Node&lt;T&gt; *&amp;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; } else { if (newNode-&gt;data &lt; root-&gt;data) { insert(root-&gt;left, newNode); cout &lt;&lt; "Inserting Left" &lt;&lt; newNode-&gt; data &lt;&lt; endl; } else { insert(root-&gt;right, newNode); cout &lt;&lt; "Inserting Right" &lt;&lt; newNode-&gt;data &lt;&lt; endl; } } } </code></pre> <p>My height function is as follows just in case my insert is actually fine.</p> <pre><code>template &lt;class T&gt; int BT&lt;T&gt;::height() const { return height(root); } template &lt;class T&gt; int BT&lt;T&gt;::height(Node&lt;T&gt;* root) const { if (root == NULL) return 0; else { if (height(root-&gt;right) &gt; height(root-&gt;left)) return 1 + height(root-&gt; right); return 1 + height(root-&gt;left); } } </code></pre>
 

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