Skip to main content
fixed code
Source Link
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65
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;r & node->value == parent->value;
}
size_t countUnivSubT(const stTree* node) {
    size_t r = 0;
    countUnivSubTimpl(node, node, &r);
    return r;
}

int main() {
    stTree* root =
        createNode(0,
            createNode(1, NULL, NULL),
            createNode(0,
                createNode(1,
                    createNode(1, NULL, NULL),
                    createNode(1, NULL, NULL),
                ),
                createNode(0, NULL, NULL)));

    size_t uTreeCount = countUnivSubT(root);
    printf("total universal subree: %u\n", uTreeCount);
}
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;
}

int main() {
    stTree* root =
        createNode(0,
            createNode(1, NULL, NULL),
            createNode(0,
                createNode(1,
                    createNode(1, NULL, NULL),
                    createNode(1, NULL, NULL),
                ),
                createNode(0, NULL, NULL)));

    size_t uTreeCount = countUnivSubT(root);
    printf("total universal subree: %u\n", uTreeCount);
}
static bool countUnivSubTimpl(const stTree* node, const stTree* parent, size_t* count) {
    if (!node) return true;
    bool r = countUnivSubTimpl(node->left, node, count)
        & countUnivSubTimpl(node->right, node, count);
    *count += r;
    return r & node->value == parent->value;
}
size_t countUnivSubT(const stTree* node) {
    size_t r = 0;
    countUnivSubTimpl(node, node, &r);
    return r;
}

int main() {
    stTree* root =
        createNode(0,
            createNode(1, NULL, NULL),
            createNode(0,
                createNode(1,
                    createNode(1, NULL, NULL),
                    createNode(1, NULL, NULL),
                ),
                createNode(0, NULL, NULL)));

    size_t uTreeCount = countUnivSubT(root);
    printf("total universal subree: %u\n", uTreeCount);
}
added 1 character in body
Source Link
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65
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*count += r;
    return r;
}
size_t countUnivSubT(const stTree* node) {
    size_t r = 0;
    countUnivSubTimpl(node, node, &r);
    return r;
}

int main() {
    stTree* root =
        createNode(0,
            createNode(1, NULL, NULL),
            createNode(0,
                createNode(1,
                    createNode(1, NULL, NULL),
                    createNode(1, NULL, NULL),
                ),
                createNode(0, NULL, NULL)));

    size_t uTreeCount = countUnivSubT(root);
    printf("total universal subree: %u\n", uTreeCount);
}
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;
}

int main() {
    stTree* root =
        createNode(0,
            createNode(1, NULL, NULL),
            createNode(0,
                createNode(1,
                    createNode(1, NULL, NULL),
                    createNode(1, NULL, NULL),
                ),
                createNode(0, NULL, NULL)));

    size_t uTreeCount = countUnivSubT(root);
    printf("total universal subree: %u\n", uTreeCount);
}
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;
}

int main() {
    stTree* root =
        createNode(0,
            createNode(1, NULL, NULL),
            createNode(0,
                createNode(1,
                    createNode(1, NULL, NULL),
                    createNode(1, NULL, NULL),
                ),
                createNode(0, NULL, NULL)));

    size_t uTreeCount = countUnivSubT(root);
    printf("total universal subree: %u\n", uTreeCount);
}
copy-edited
Source Link
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65
  1. If you don't need to modify something, don't require the right. Use const.

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

  3. Consequently, countUnivSubT() is also wrong. Still, fixing isTreeUniv() would result in a quadratic\$O(n^2)\$ algorithm, when it should be linear\$O(n)\$. The idea is to get all the info you need at once.

  4. You shouldn't use a global for the subtree-countsAvoid globals. ItUsing uTreeCount makes the code non-reentrant, and breaks locality of reasoning.

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;
}

int main() {
    stTree* root =
        createNode(0,
            createNode(1, NULL, NULL),
            createNode(0,
                createNode(1,
                    createNode(1, NULL, NULL),
                    createNode(1, NULL, NULL),
                ),
                createNode(0, NULL, NULL)));

    size_t uTreeCount = countUnivSubT(root);
    printf("total universal subree: %u\n", uTreeCount);
}
  1. If you don't need to modify something, don't require the right. Use const.

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

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

  4. 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;
}

int main() {
    stTree* root = createNode(0,
        createNode(1, NULL, NULL),
        createNode(0,
            createNode(1,
                createNode(1, NULL, NULL),
                createNode(1, NULL, NULL),
            ),
            createNode(0, NULL, NULL)));

    size_t uTreeCount = countUnivSubT(root);
    printf("total universal subree: %u\n", uTreeCount);
}
  1. If you don't need to modify something, don't require the right. Use const.

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

  3. Consequently, countUnivSubT() is also wrong. Still, fixing isTreeUniv() would result in a \$O(n^2)\$ algorithm, when it should be \$O(n)\$. The idea is to get all the info you need at once.

  4. Avoid globals. Using uTreeCount makes the code non-reentrant, and breaks locality of reasoning.

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;
}

int main() {
    stTree* root =
        createNode(0,
            createNode(1, NULL, NULL),
            createNode(0,
                createNode(1,
                    createNode(1, NULL, NULL),
                    createNode(1, NULL, NULL),
                ),
                createNode(0, NULL, NULL)));

    size_t uTreeCount = countUnivSubT(root);
    printf("total universal subree: %u\n", uTreeCount);
}
copy-edited
Source Link
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65
Loading
added 72 characters in body
Source Link
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65
Loading
Source Link
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65
Loading