Skip to content

Commit b43321b

Browse files
committed
Fixed: reinit().
1 parent 04c6a00 commit b43321b

File tree

2 files changed

+6
-17
lines changed

2 files changed

+6
-17
lines changed

ddt.h

+5-16
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,13 @@ class ddt {
9999
freeTree(root->left);
100100
freeTree(root->right);
101101

102-
// TODO: make it more CPP-like.
103-
free(root->value);
104-
free(root->key);
105-
free(root);
102+
delete root;
106103
}
107104
}
108105

109106
TREE* createAndInitTreeNode(const string& key, const T& value)
110107
{
111-
// TODO: make it more CPP-like.
112-
TREE* newNode = (TREE*)malloc(sizeof(TREE));
108+
TREE* newNode = new TREE;
113109

114110
newNode->key = key;
115111
newNode->value = value;
@@ -236,11 +232,11 @@ class ddt {
236232
else {
237233
/* Check for leaf node: both left and right child nodes are empty. */
238234
if (!(root->left || root->right)) {
239-
free(root);
235+
delete root;
240236
return nullptr;
241237
} else if (root->left == nullptr ^ root->right == nullptr) { // If either are empty
242238
TREE* temp = either(root->right, root->left); // No code duplication
243-
free(root);
239+
delete root;
244240
root = temp;
245241
} else {
246242
TREE* temp = root->left;
@@ -289,8 +285,7 @@ class ddt {
289285

290286
if (key_hash == root_value_hash) {
291287
return root->value;
292-
}
293-
else if (key_hash < root_value_hash)
288+
} else if (key_hash < root_value_hash)
294289
return getValueByKey(root->left, key);
295290
else /* key_hash > root_value_hash */
296291
return getValueByKey(root->right, key);
@@ -311,12 +306,6 @@ class ddt {
311306
tree = removeNode(tree, key);
312307
}
313308

314-
// FIXME:
315-
void clear() {
316-
freeTree(tree);
317-
}
318-
319-
// FIXME:
320309
void reinit(const string& key, const T& value) {
321310
clearTree(tree, key, value);
322311
}

main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int main() {
2121
d.remove("abcd");
2222
d.print();
2323

24-
//d.clear();
24+
d.reinit("a4r22", "try");
2525
d.insert("abbbd", "cool");
2626
d.insert("accbdd", "not cool");
2727
d.print();

0 commit comments

Comments
 (0)