Note that there are some explanatory texts on larger screens.

plurals
  1. POBinary Search Tree--delete function not working
    primarykey
    data
    text
    <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;fstream&gt; using namespace std; template &lt;class T&gt; struct TreeNode{ string value; T key; TreeNode&lt;T&gt; *Parent; TreeNode&lt;T&gt; *LeftChild; TreeNode&lt;T&gt; *RightChild; TreeNode (T k,string Val) { this-&gt;value=Val; this-&gt;key=k; this-&gt;Parent=NULL; this-&gt;LeftChild=NULL; this-&gt;RightChild=NULL; } }; template &lt;class T&gt; class BinaryTree{ private: TreeNode&lt;T&gt; *Root; public: BinaryTree(); void LoadTree(const char file[]); ~BinaryTree(); void insertNode(T Key,string Val); void deleteNode(T Key); string searchNode(T Key); void UpdateKey(T newkey,T oldkey); int Height(TreeNode&lt;T&gt; *node); int height(); }; template &lt;class T&gt; BinaryTree&lt;T&gt;::BinaryTree() { Root=NULL; } template &lt;class T&gt; void BinaryTree&lt;T&gt;::insertNode(T Key,string Val) { TreeNode&lt;T&gt; **temp=&amp;Root; TreeNode&lt;T&gt; *temp1=NULL; if (*temp==NULL) { Root=new TreeNode&lt;T&gt;(Key,Val); return; } else { while (*temp!=NULL) { temp1=*temp; if (temp1-&gt;key&gt;Key) { temp=&amp;(*temp)-&gt;LeftChild; } else if (temp1-&gt;key&lt;Key) { temp=&amp;(*temp)-&gt;RightChild; } } } *temp=new TreeNode&lt;T&gt;(Key,Val); (*temp)-&gt;Parent=temp1; } template &lt;class T&gt; void BinaryTree&lt;T&gt;::LoadTree(const char *file) { ifstream fin; fin.open(file); string buffer; T buff; while (!fin.eof()) { getline(fin,buffer,'~'); fin&gt;&gt;buff; if (!buff) continue; insertNode(buff,buffer); } fin.close(); } void BinaryTree&lt;T&gt;::deleteNode(T Key) { TreeNode&lt;T&gt; *temp=Root; TreeNode&lt;T&gt; *temp1=temp; while (temp!=NULL) { if (temp-&gt;key==Key) { temp1=temp; if (temp==Root) { TreeNode&lt;T&gt; *temp2=Root; temp2=temp2-&gt;RightChild; while (temp2-&gt;LeftChild!=NULL) { temp2=temp2-&gt;LeftChild; } temp-&gt;key=temp2-&gt;key; temp-&gt;value=temp2-&gt;value; delete temp2; } else if (temp-&gt;RightChild==NULL) { TreeNode&lt;T&gt; *temp2=temp; temp2=temp-&gt;Parent; if (temp2-&gt;RightChild==temp) { temp2-&gt;RightChild=temp-&gt;LeftChild; (temp-&gt;LeftChild)-&gt;Parent=temp2; } else if (temp2-&gt;LeftChild==temp) { temp2-&gt;LeftChild=temp-&gt;LeftChild; (temp-&gt;LeftChild)-&gt;Parent=temp2; } delete temp1; delete temp; return; } else if (temp-&gt;LeftChild==NULL) { TreeNode&lt;T&gt; *temp2=temp; temp2=temp-&gt;Parent; if (temp2-&gt;RightChild==temp) { temp2-&gt;RightChild=temp-&gt;RightChild; (temp-&gt;RightChild)-&gt;Parent=temp2; } else if (temp2-&gt;LeftChild==temp) { temp2-&gt;LeftChild=temp-&gt;RightChild; (temp-&gt;RightChild)-&gt;Parent=temp2; } delete temp1; delete temp; return; } else if (temp-&gt;RightChild!=NULL) { TreeNode&lt;T&gt; *tmp=temp; temp=temp-&gt;RightChild; if (temp-&gt;LeftChild!=NULL) { while (temp-&gt;LeftChild!=NULL) { tmp=temp; temp=temp-&gt;LeftChild; } } if (tmp!=temp1) tmp-&gt;LeftChild==NULL; else temp1-&gt;RightChild==NULL; temp1-&gt;key=temp-&gt;key; temp1-&gt;value=temp-&gt;value; delete temp; return; } } if (temp-&gt;key&gt;Key) { temp=temp-&gt;LeftChild; } else if (temp-&gt;key&lt;Key) { temp=temp-&gt;RightChild; } } return; } </code></pre> <p>This is part of the code for a Binary Search tree that I am making.I am facing a problem when I run the delete function i.e for some nodes, i am getting the error "testing.exe has stopped working".testing is name of my main file.I have checked again and again but I don't seem to find the problem.Can anyone see the problem?Thanks</p>
    singulars
    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.
 

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