Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here we are. This seems to work. If anyone has any suggestions as to how I can optimize this or a problem that I need to fix please speak up! Thanks!</p> <pre><code>void BinaryTree::remove(int data){ // Is the data valid? if(data &lt; 0){ cerr &lt;&lt; data &lt;&lt; " is not valid. Must be a positive integer value." &lt;&lt; endl; } else{ // Find the node BinNode* loc = root; BinNode* parent = nullptr; bool found = false; while(!found &amp;&amp; loc != nullptr){ if(data &gt; loc-&gt;data){ parent = loc; loc = loc-&gt;right; } else if(data &lt; loc-&gt;data){ parent = loc; loc = loc-&gt;left; } else found = true; // If there are children, save pointers to them BinNode* leftChild = nullptr; BinNode* rightChild = nullptr; if(loc-&gt;left != nullptr) leftChild = loc-&gt;left; if(loc-&gt;right != nullptr) rightChild = loc-&gt;right; // So now pointers to the children have been saved (if they exist) and // parent pointers have been taken care of (if they exist) the node can be deleted // If no children exist simply just delete the node and return // Check if two children exist if(leftChild != nullptr &amp;&amp; rightChild != nullptr){ // Find a minimum in the right subtree BinNode * min = loc-&gt;right; BinNode * minParent = loc; while(min-&gt;left != nullptr){ minParent = min; min = min-&gt;left; } // Replace value of the node to be removed with the found minimum loc-&gt;data = min-&gt;data; // Delete the duplicate if(minParent != loc) minParent-&gt;left = nullptr; else minParent-&gt;right = nullptr; delete min; } // If one child exists // Need to handle if it is the root here // change root pointer to remaining child else if(leftChild != nullptr || rightChild != nullptr){ // If there is a parent, take care of the pointer if(parent != nullptr){ if(loc-&gt;data &lt; parent-&gt;data) parent-&gt;left = nullptr; else if(loc-&gt;data &gt; parent-&gt;data) parent-&gt;right = nullptr; // Now loc can be deleted delete loc; if(leftChild != nullptr){ if(parent != nullptr){ if(leftChild-&gt;data &lt; parent-&gt;data) parent-&gt;left = leftChild; else if(leftChild-&gt;data &gt; parent-&gt;data) parent-&gt;right = leftChild; } // If it is the root else{ root = leftChild; } } else if(rightChild != nullptr){ if(parent != nullptr){ if(rightChild-&gt;data &lt; parent-&gt;data) parent-&gt;left = rightChild; else if(rightChild-&gt;data &gt; parent-&gt;data) parent-&gt;right = rightChild; } // If it is the root else{ root = rightChild; } } } // If there are no children else if (leftChild == nullptr &amp;&amp; rightChild == nullptr){ // If there is a parent, take care of the pointer if(parent != nullptr){ if(loc-&gt;data &lt; parent-&gt;data) parent-&gt;left = nullptr; else if(loc-&gt;data &gt; parent-&gt;data) parent-&gt;right = nullptr; } // If it is the root else root = nullptr; delete loc; } } </code></pre>
    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.
    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