我先把代碼貼出來,有50行,但大家不必全看,我會指出來可能出錯的地方。謝謝。
#include <iostream>
using namespace std;
template <typename key,typename value>
class BST {
typedef class node {
public:
node(key i,value va):k(i),val(va),n(1),left(NULL),right(NULL) { }
key k;
value val;
node *left, *right;
int n;
} *pnode;
int size() const { return size(root); }
int size(pnode x) { if (x = NULL) return 0; else return x.n; };
void put(key k, value val) { root = put(root, k, val); }
pnode put(pnode x, key k, value val);
pair<bool, value> get(key k) { pnode temp = get(root, k); if (temp == NULL) return{ false,0 }; else return{ true,temp.val }; }
pnode get(pnode x, key k);
private:
pnode root = NULL;
int compare(key k1, key k2) { if (k1 < k2) return -1; else if (k1>k2) return 1; else return 0; }
};
template <typename key,typename value>
BST<key, value>::put(pnode x,key k, value val)->pnode //這一行報錯了!!
{
if (x = NULL) return new node(k, val);
int cmp = compare(k, x.k);
if (cmp < 0)
x.left = put(x.left, k, val);
else if (cmp>0)
x.right = put(x.right, k, val);
else
x.val = val;
x.n = size(x.left) + size(x.right) + 1;
return x;
}
template <typename key, typename value>
BST<key, value>::get(pnode x, key k)->pnode //這一行報錯了!!!
{
if (x = NULL) return NULL;
int cmp = compare(k, x.k);
if (cmp < 0)
return get(x.left, k);
else if (cmp>0)
return get(x.right, k);
else
return x;
}
下面是報錯的截圖(visual studio 2015):
“->pnode”這是什麼用法?沒見過,難道是高級寫法?
“BST::get(pnode x, key k)”前面不要加返回值類型嗎?