模板 b 树 C++ 实现
template b-tree c++ implementation
我正在尝试在 C++ 中为 b-tree 编写模板 class,以便它可以使用任何类型的 char 或整数,当我尝试插入树时遇到了这个错误,在第 49 、 76 和 12 行,所以如果有人可以帮助代码,请查看下面的代码,该代码仅用于插入和打印。
template<typename t,int order>
class Node {
public :
t *keys;
int degree; // minimum degree
Node **children;
int numberOfKeys;
bool leaf;
Node(int index, bool leaf);
void insertNonFull(t data);
void splitChild(int i, Node *node);
void print();
friend class BTree;
};
template<typename t,int order>
class BTree {
Node<t,order> *root;
int degree; // minimum degree
public:BTree() {
root = NULL;
this->degree = order;
}
public:void print() {
if (root != NULL)
root->print();
}
public:void insert(t data);
};
template<typename t,int order>
Node<t,order>::Node(int degree, bool leaf) {
this->degree = degree;
this->leaf = leaf;
keys = new int[2 * degree - 1];
children = new Node *[2 * degree];
numberOfKeys = 0;
}
template<typename t,int order>
void BTree<t,order>::insert(t data) {
if (root == NULL) {
root = new Node<t,order>(degree, true);
root->keys[0] = data;
root->numberOfKeys = 1;
} else {
if (root->numberOfKeys == 2 * degree - 1) {
Node<t,order> *newnode = new Node<t,order>(degree, false);
newnode->children[0] = root;
newnode->splitChild(0, root);
int i = 0;
if (newnode->keys[0] < data)
i++;
newnode->children[i]->insertNonFull(data);
root = newnode;
} else
root->insertNonFull(data);
}
}
int main(){
BTree<int,3> t;
t.insert(1);
t.insert(5);
t.insert(0);
t.insert(4);
t.insert(3);
t.insert(2);
t.print();
return 0;
}
the error i got is in the picture
在 Node
class 之前添加 BTree
的 forward declaration,并将 friend class BTree;
替换为 friend class BTree<t, order>;
。还要将 NULL
与 nullptr
交换。它会解决问题。
我正在尝试在 C++ 中为 b-tree 编写模板 class,以便它可以使用任何类型的 char 或整数,当我尝试插入树时遇到了这个错误,在第 49 、 76 和 12 行,所以如果有人可以帮助代码,请查看下面的代码,该代码仅用于插入和打印。
template<typename t,int order>
class Node {
public :
t *keys;
int degree; // minimum degree
Node **children;
int numberOfKeys;
bool leaf;
Node(int index, bool leaf);
void insertNonFull(t data);
void splitChild(int i, Node *node);
void print();
friend class BTree;
};
template<typename t,int order>
class BTree {
Node<t,order> *root;
int degree; // minimum degree
public:BTree() {
root = NULL;
this->degree = order;
}
public:void print() {
if (root != NULL)
root->print();
}
public:void insert(t data);
};
template<typename t,int order>
Node<t,order>::Node(int degree, bool leaf) {
this->degree = degree;
this->leaf = leaf;
keys = new int[2 * degree - 1];
children = new Node *[2 * degree];
numberOfKeys = 0;
}
template<typename t,int order>
void BTree<t,order>::insert(t data) {
if (root == NULL) {
root = new Node<t,order>(degree, true);
root->keys[0] = data;
root->numberOfKeys = 1;
} else {
if (root->numberOfKeys == 2 * degree - 1) {
Node<t,order> *newnode = new Node<t,order>(degree, false);
newnode->children[0] = root;
newnode->splitChild(0, root);
int i = 0;
if (newnode->keys[0] < data)
i++;
newnode->children[i]->insertNonFull(data);
root = newnode;
} else
root->insertNonFull(data);
}
}
int main(){
BTree<int,3> t;
t.insert(1);
t.insert(5);
t.insert(0);
t.insert(4);
t.insert(3);
t.insert(2);
t.print();
return 0;
}
the error i got is in the picture
在 Node
class 之前添加 BTree
的 forward declaration,并将 friend class BTree;
替换为 friend class BTree<t, order>;
。还要将 NULL
与 nullptr
交换。它会解决问题。