Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ linked binary search tree (DeleteTree)
    primarykey
    data
    text
    <p>I have to implement a binary search tree using C++ for one of assignments. I've created the class, and attempted to implement the <strong>InsertItem, PrintTree, DeleteTree</strong> methods for the class, I think I did everything right but for some reason my program keeps crashing :(</p> <p>Here's my code:</p> <p><strong>PrintTree Method</strong></p> <pre><code>template &lt;class TItem&gt; void BinarySearchTree&lt;TItem&gt;::PrintTree() { PrintTree(RootNode); } template &lt;class TItem&gt; void BinarySearchTree&lt;TItem&gt;::PrintTree(BinarySearchTreeNode* Node) { if(Node == NULL) return; cout &lt;&lt; Node-&gt;Data &lt;&lt; endl; PrintTree(Node-&gt;LeftChild); PrintTree(Node-&gt;RightChild); } </code></pre> <p><strong>DeleteTree Method</strong></p> <pre><code>template &lt;class TItem&gt; void BinarySearchTree&lt;TItem&gt;::DeleteTree() { DeleteTree(RootNode); } template &lt;class TItem&gt; void BinarySearchTree&lt;TItem&gt;::DeleteTree(BinarySearchTreeNode* Node) { if(Node == NULL) return; DeleteTree(Node-&gt;LeftChild); DeleteTree(Node-&gt;RightChild); delete Node; } </code></pre> <p><strong>My sequence of method calls up until the program crashes:</strong></p> <p>I insert items <code>F,B,G,A,D,I,C,E,H</code>: <strong>works fine</strong></p> <p>I call <code>PrintTree()</code>: <strong>works fine</strong></p> <p>I call <code>DeleteTree()</code>: <strong>works fine</strong></p> <p>I call <code>PrintTree()</code> again: <strong>program crashes</strong></p> <p>For some reason the expression <code>if(RootNode == NULL)</code> is not returning true after the <code>DeleteTree()</code> method is called, so the program tries to print something that doesn't exist and crashes. I'm not sure why this is happening, what am I doing wrong here?</p> <p>Any and all help is appreciated. </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.
 

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