search tree
May 25, 2010 at 12:10pm May 25, 2010 at 12:10pm UTC
Hi,
I'm a student trying to program a searchtree with the class TreeSet and Node.
The comment is in dutch, so just ignore that. I don't really need help with functionality, just with c++. What do all these errors mean?
Like blablabla is undeclared in the declaration of nodes. I don't have any experience with pointers and stuff. I get the concept. Still I don't get why my compiler is complaining...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
In file included from treesettest.h:17,
treeset.h:56: error: `template <class T> class TreeSet' used without template parameters
treeset.h:57: error: non-member function `bool contains(const T&)' cannot have `const ' method qualifier
treeset.h: In function `bool contains(const T&)' :
treeset.h:58: error: `n' undeclared (first use this function)
treeset.h:58: error: (Each undeclared identifier is reported only once for each function it appears in.)
treeset.h: At global scope:
treeset.h:75: error: `template<class T> class TreeSet' used without template parameters
treeset.h: In function `void remove(const T&)':
treeset.h:84: error: `Node' undeclared (first use this function)
treeset.h:84: error: `currentNode' undeclared (first use this function)
treeset.h:85: error: `parentNode' undeclared (first use this function)
treeset.h:86: error: invalid use of `this ' in non-member function
treeset.h:175: error: `rightChild' undeclared (first use this function)
treeset.h:177: error: `leftChild' undeclared (first use this function)
treeset.h:216: error: missing template arguments before "leftTree"
treeset.h:216: error: expected `;' before "leftTree"
treeset.h:218: error: `leftTree' undeclared (first use this function)
...
more of the same
...
BUILD FAILED (exit value 2, total time: 2s)
This is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
#ifndef _TREESET_H
#define _TREESET_H
template <typename T>
class TreeSet
{
private :
class Node
{
private :
T _data;
public :
TreeSet * left;
TreeSet * right;
Node(T& data)
{
_data = data;
left = 0;
right = 0;
}
T getData(){return _data;};
void setData(const T& d){_data = d;};
};
Node * n;
public :
TreeSet(){n->setData(NULL);};
TreeSet(const T& data){n->setData(data);};
~TreeSet();
bool contains(const T &data) const ;
void add(const T &data);
void remove(const T& data);
void clear();
template <typename V>
void visit(V &visitor);
};
template <typename T>
bool TreeSet::contains(const T& data) const
{
if (n == 0)
return false ;
if (n->getData() == data)
return true ;
else if (n->getData() < data)
{
if (n->right != 0)
return n->right->contains(data);
return false ;
}
if (n->left != 0)
return n->left->contains(data);
return false ;
}
template <typename T>
void TreeSet::remove(const T& data)
{
if (contains(data))
{
//de Node bestaat zeker
bool found = false ;
bool isRightChild;
Node * currentNode;
Node * parentNode;
currentNode = this ->n;
if (currentNode->getData() == data)
{
found = true ;
}
//zoeken naar te verwijderen Node
while (!found)
{
if (currentNode->getData() == data)
{
found = true ;
}
else
{
parentNode = currentNode;
if (currentNode->getData() < data)
{
currentNode = currentNode->right->n;
isRightChild = true ;
}
else
{
currentNode = currentNode->left->n;
isRightChild = false ;
}
}
}
// 1. blad
// 2. heeft 1 kind
// 3. heeft 2 kinderen
if ( currentNode->left == 0 && currentNode->right == 0)
{
// 1. blad
if (isRightChild)
//Huidige Node is een rechter kind
parentNode->right = 0;
else
//Huidige Node is een linker kind
parentNode->left = 0;
delete currentNode;
return ;
}
if ((currentNode->left == 0 && currentNode->right != 0)|| (currentNode->left != 0 && currentNode->right == 0))
{
// 2. heeft 1 kind
if (currentNode->left == 0 && currentNode->right != 0)
{
//Huidige Node heeft een rechter kind
if (isRightChild)
{
//Huidige Node is een rechter kind
parentNode->right = currentNode->right;
}
else
{
//Huidige Node is een linker kind
parentNode->left = currentNode->right;
}
}
else
{
//Huidige Node heeft een linker kind
if (isRightChild)
{
//Huidige Node is een rechter kind
parentNode->right = currentNode->left;
}
else
{
//Huidige Node is een linker kind
parentNode->left = currentNode->left;
}
}
delete currentNode;
return ;
}
// deze controle is nutteloos, maar ik doe ze lekker toch,... nè!
if (currentNode->left != 0 && currentNode->right != 0)
{
// 3. heeft 2 kinderen
Node * rightChild;
rightChild = currentNode->right->n;
Node * leftChild;
leftChild = currentNode->left->n;
if ((rightChild->left == 0) && (rightChild->right == 0))
{
//isRightChild heeft geen kinderen; nice replacen die shit
if (isRightChild)
{
parentNode->right = currentNode->right;
}
else
{
parentNode->left = currentNode->right;
}
delete currentNode;
return ;
}
if ((leftChild->left == 0) && (leftChild->right == 0))
{
//linkerkind heeft geen kinderen; nice replacen die shit
if (isRightChild)
{
parentNode->right = currentNode->left;
}
else
{
parentNode->left = currentNode->left;
}
delete currentNode;
return ;
}
//zowel linkerkind als rechter kind heeft kinderen
//parentNode laten wijzen naar isRightChild vervangen
//bij isRightChild zoeken totdat het geen linkerkind heeft
//en die null pointer laten wijzen naar linkerkind
parentNode->right = currentNode->right;
TreeSet leftTree = currentNode->left;
while (leftTree != 0)
{
leftTree = leftTree->n->left;
}
leftTree->n->left = currentNode->left;
delete currentNode;
}
}
}
template <typename T>
void TreeSet::add(const T &data)
{
if (!contains(data))
//slecht dat hij deze controle recursief (telkens opnieuw) controleert,
//opl eventueel apparte methode die recursief wordt gebruikt
{
if (n->getData() < data)
if (n->right != 0)
n->right->add(data);
else
n->right = new TreeSet(data);
else
if (n->left != 0)
n->left->add(data);
else
n->left = new TreeSet(data);
}
}
template <typename T>
void TreeSet::clear()
{
if (n->right != 0)
{
n->right->clear();
delete n->right;
}
if (n->left != 0)
{
n->left->clear();
delete n->left;
}
delete n;
//huidige treeset moet niet delete worden, Node wel
//zal n bij huidige deze treeset dan wijzen naar NULL?
}
#endif
Thx for the help.
Regards
May 25, 2010 at 12:13pm May 25, 2010 at 12:13pm UTC
TreeSet is a template so you need to use parameters every time you declare one.
Your lines 17 & 18 above should probably be something like this:
1 2
TreeSet<T> * left;
TreeSet<T> * right;
May 25, 2010 at 12:13pm May 25, 2010 at 12:13pm UTC
It should be
1 2
template <typename T>
bool TreeSet<T>::contains(const T& data) const
Other part of source code you can change by your self
May 25, 2010 at 12:23pm May 25, 2010 at 12:23pm UTC
Thx, that helped a lot
Topic archived. No new replies allowed.