Skip to main content
2 of 6
added 72 characters in body
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65
  1. Putting the tag-type two lines down instead of on the same line as the closing brace is curious.

  2. I wonder why you don't use createNode() in insertRight() and insertLeft().

  3. Assuming that resource-aquisition always succeeds is quite brave.

  4. If you don't need to modify something, don't require the right. Use const.

  5. isTreeUniv() is just wrong. It only checks the direct descendents, while it should recurse into them.

  6. Consequently, countUnivSubT() is also wrong. Still, fixing isTree() would result in a quadratic algorithm, when it should be linear. The idea is to get all the info you need at once.

  7. You shouldn't use a global for the subtree-counts. It makes the code non-reentrant.

static bool countUnivSubTimpl(const stTree* node, const stTree* parent, size_t* count) {
    if (!node) return true;
    bool r = node->value == parent->value;
    r &= countUnivSubTimpl(node->left, node, count);
    r &= countUnivSubTimpl(node->right, node, count);
    count += r;
    return r;
}
size_t countUnivSubT(const stTree* node) {
    size_t r = 0;
    countUnivSubTimpl(node, node, &r);
    return r;
}
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65