Note that there are some explanatory texts on larger screens.

plurals
  1. POBinary Search Tree(search function returning NULL)
    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;::LoadTree(const char *file) { ifstream fin; fin.open(file); string buffer; T buff; while (!fin.eof()) { getline(fin,buffer,'~'); fin&gt;&gt;buff; TreeNode&lt;T&gt; *temp,*temp1; temp=Root; temp1=temp; while (temp!=NULL) { temp1=temp; if (temp-&gt;key&gt;buff) { temp=temp-&gt;LeftChild; } else if (temp-&gt;key&lt;buff) { temp=temp-&gt;RightChild; } } temp=new TreeNode&lt;T&gt;(buff,buffer); if (temp!=Root) temp-&gt;Parent=temp1; cout&lt;&lt;temp-&gt;value&lt;&lt;temp-&gt;key&lt;&lt;endl; if (temp-&gt;LeftChild!=0) cout&lt;&lt;(temp-&gt;LeftChild)-&gt;value&lt;&lt;endl; } fin.close(); } template &lt;class T&gt; string BinaryTree&lt;T&gt;::searchNode(T Key) { TreeNode&lt;T&gt; *temp=Root; while (temp!=NULL) { if (temp-&gt;key==Key) { return temp-&gt;value; } if (temp-&gt;key&gt;Key) { temp=temp-&gt;LeftChild; } else if (temp-&gt;key&lt;Key) { temp=temp-&gt;RightChild; } } return "\0"; } </code></pre> <p>Above are the header file and constructor and functions for building tree from a file and searching a node in it.I don't understand what am I missing in my function because whenever i run the search function,it always returns NULL which is the default condition when the node key exists in the file/tree. Thanks a lot everyone,it's working now.I am really grateful.I used my insert function to build up my tree.</p> <pre><code>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; insertNode(buff,buffer); } fin.close(); } </code></pre>
    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.
    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