Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Binary Search Tree error with deleting nodes
    text
    copied!<p>I'm working with deleting nodes from a binary search tree and I keep getting a segfault error after the while loop in this function. Please help me catch any errors if you can.</p> <p>Here is the function:</p> <pre><code>void deleteNode() { int key; nodePtr location = NULL, parent = NULL; cout &lt;&lt; "Enter account number to delete: "; cin &gt;&gt; key; cout &lt;&lt; endl &lt;&lt; endl; bool found = searchTree(key, &amp;location, &amp;parent); if (!found) { cout &lt;&lt; "Error! Account number: " &lt;&lt; key &lt;&lt; " was not found." &lt;&lt; endl &lt;&lt; endl; } else if (location-&gt;left != NULL &amp;&amp; location-&gt;right != NULL) { //delete node with left and right subtrees nodePtr leftmost = location-&gt;right, leftmostParent = NULL; while (leftmost-&gt;left != NULL) { leftmostParent = leftmost; leftmost = leftmost-&gt;left; } leftmost-&gt;left = location-&gt;left; if (location-&gt;right != leftmost) leftmost-&gt;right = location-&gt;right; cout &lt;&lt; "1" &lt;&lt; endl; if (parent != NULL) { if (parent-&gt;acctNum &gt; location-&gt;acctNum) parent-&gt;left = leftmost; else parent-&gt;right = leftmost; } leftmostParent-&gt;left = NULL; delete location; location = NULL; } else if (location-&gt;left != NULL &amp;&amp; location-&gt;right == NULL) { //delete node with only a left subtree if (parent-&gt;acctNum &gt; location-&gt;acctNum) parent-&gt;left = location-&gt;left; else parent-&gt;right = location-&gt;left; delete location; location = NULL; } else if (location-&gt;left == NULL &amp;&amp; location-&gt;right != NULL) { //delete node with only a right subtree nodePtr leftmost = location-&gt;right, leftmostParent = NULL; while (leftmost-&gt;left != NULL) { leftmostParent = leftmost; leftmost = leftmost-&gt;left; } leftmost-&gt;right = location-&gt;right; leftmostParent-&gt;left = NULL; if (parent-&gt;acctNum &gt; location-&gt;acctNum) parent-&gt;left = leftmost; else parent-&gt;right = leftmost; delete location; location = NULL; } else { //delete a leaf node if (location-&gt;acctNum &gt; parent-&gt;acctNum) parent-&gt;right = NULL; else parent-&gt;left = NULL; delete location; location = NULL; } } </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