Deducing template arguments
<p>I wrote code to construct binary search tree and also a bfs a binary search tree to test whether it works, but i got a lot of bugs, I debug them  for a long time and cannot find the bug.</p>

<pre><code>#include&lt;vector&gt;

template&lt;typename Key, typename Value&gt;
struct BSTNode{
    Key key;
    Value value;
    BSTNode * left;
    BSTNode * right;
    BSTNode(Key k, Value v, BSTNode *l=NULL, BSTNode *r=NULL) :
    key(k), value(v), left(l), right(r) {}
};

template&lt;typename Key, typename Value&gt;
BSTNode&lt;Key, Value&gt;* min_height(std::vector&lt;int&gt; &amp;v, int left, int right ) {
    if(left&lt;=right){
        int mid=left+ (right-left)/2;
        BSTNode&lt;Key, Value&gt;* node=new BSTNode&lt;Key, Value&gt;(v[mid], v[mid]);
        node-&gt;left=min_height(v, left, mid-1 );
        node-&gt;right=min_height(v, mid+1, right );
        return node;
    }
}

int main(){
    std::vector&lt;int&gt; v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    BSTNode&lt;int, int&gt;* root=min_height(v, 0, 5);
}
</code></pre>

<p>Compiler error:</p>

<blockquote>
<pre><code>prog.cpp: In function ‘BSTNode&lt;Key, Value&gt;* min_height(std::vector&lt;int&gt;&amp;, int, int)’:
prog.cpp:48:42: error: no matching function for call to ‘min_height(std::vector&lt;int&gt;&amp;, int&amp;, int&amp;)’
prog.cpp:48:42: note: candidate is:
prog.cpp:44:22: note: template&lt;class Key, class Value&gt; BSTNode&lt;Key, Value&gt;* min_height(std::vector&lt;int&gt;&amp;, int, int)
prog.cpp:44:22: note:   template argument deduction/substitution failed:
prog.cpp:48:42: note:   couldn't deduce template parameter ‘Key’
prog.cpp:49:41: error: no matching function for call to ‘min_height(std::vector&lt;int&gt;&amp;, int&amp;, int&amp;)’
prog.cpp:49:41: note: candidate is:
prog.cpp:44:22: note: template&lt;class Key, class Value&gt; BSTNode&lt;Key, Value&gt;* min_height(std::vector&lt;int&gt;&amp;, int, int)
prog.cpp:44:22: note:   template argument deduction/substitution failed:
prog.cpp:49:41: note:   couldn't deduce template parameter ‘Key’
prog.cpp: In function ‘int main()’:
prog.cpp:63:45: error: no matching function for call to ‘min_height(std::vector&lt;int&gt;&amp;, int, int)’
prog.cpp:63:45: note: candidate is:
prog.cpp:44:22: note: template&lt;class Key, class Value&gt; BSTNode&lt;Key, Value&gt;* min_height(std::vector&lt;int&gt;&amp;, int, int)
prog.cpp:44:22: note:   template argument deduction/substitution failed:
prog.cpp:63:45: note:   couldn't deduce template parameter ‘Key’
prog.cpp:63:22: warning: unused variable ‘root’ [-Wunused-variable]
</code></pre>
</blockquote>
