I have to make my own BinTree
template class for my C++ course. The problem is that in the example from my prof it´s all made with const
, but my code only compiles if I take the const
s out. In the code below I always receive an error in the input()
method by creating the new node. Netbeans throws the following error:
BinTree.hpp:52:73: error: invalid conversion from 'const BinTree::node*' to 'BinTree::node*' [-fpermissive] : key{ch}, value {val}, top{t}, left{nullptr}, right{nullptr}, counter{1} {}
I hope you can help me fix it.
template <class T> class BinTree {
struct node; //forward deklaration
public:
BinTree();
~BinTree();
int size() const {
return sz;
}
node* find(const char& ) const;
node* insert(const char&);
node* getRoot();
void in_Order();
void level_Order(node*);
void treeHigh();
int count(node*);
private:
struct node {
char key;
T value;
node* top;
node* left;
node* right;
volatile int counter;
explicit node(const char&, const T& = T {}, const node* t = nullptr);
};
node* root;
int sz;
};
template<class T>
BinTree<T>::BinTree() : root{'\0'}, sz{0} {}
template<class T>
BinTree<T>::node::node(const char& ch, const T& val, const node* t)
: key{ch}, value {val}, top{t}, left{nullptr}, right{nullptr}, counter{1} {}
template<class T> typename BinTree<T>::node*
BinTree<T>::insert(const char& val) {
node * n{root};
node * nn{ new node{val}} ;
if (root == nullptr) {
n = nn;
}
while (n != nullptr) {
if (nn->value == n->key) {
n->counter++;
delete nn;
return n;
}
if (nn->value < n->key) {
if (n->left == nullptr) {
n->left = nn;
nn->top = n;
break;
};
} else n = n->left;
continue;
if (nn->value > n->key) {
if (n->right == nullptr) {
n->right = nn;
nn->top = n;
break;
};
} else n = n->right;
continue;
}
++sz;
return nn;;
}