バイナリ検索ツリーを構築するコードと、それが機能するかどうかをテストするためのbfsバイナリ検索ツリーを作成しましたが、多くのバグが発生し、長い間デバッグしていて、バグを見つけることができません。
#include<vector>
template<typename Key, typename Value>
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<typename Key, typename Value>
BSTNode<Key, Value>* min_height(std::vector<int> &v, int left, int right ) {
if(left<=right){
int mid=left+ (right-left)/2;
BSTNode<Key, Value>* node=new BSTNode<Key, Value>(v[mid], v[mid]);
node->left=min_height(v, left, mid-1 );
node->right=min_height(v, mid+1, right );
return node;
}
}
int main(){
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
BSTNode<int, int>* root=min_height(v, 0, 5);
}
コンパイラエラー:
prog.cpp: In function ‘BSTNode<Key, Value>* min_height(std::vector<int>&, int, int)’: prog.cpp:48:42: error: no matching function for call to ‘min_height(std::vector<int>&, int&, int&)’ prog.cpp:48:42: note: candidate is: prog.cpp:44:22: note: template<class Key, class Value> BSTNode<Key, Value>* min_height(std::vector<int>&, 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<int>&, int&, int&)’ prog.cpp:49:41: note: candidate is: prog.cpp:44:22: note: template<class Key, class Value> BSTNode<Key, Value>* min_height(std::vector<int>&, 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<int>&, int, int)’ prog.cpp:63:45: note: candidate is: prog.cpp:44:22: note: template<class Key, class Value> BSTNode<Key, Value>* min_height(std::vector<int>&, 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]