Putting the tag-type two lines down instead of on the same line as the closing brace is curious.
I wonder why you don't use
createNode()
ininsertRight()
andinsertLeft()
.Assuming that resource-aquisition always succeeds is quite brave.
If you don't need to modify something, don't require the right. Use
const
.isTreeUniv()
is just wrong. It only checks the direct descendents, while it should recurse into them.Consequently,
countUnivSubT()
is also wrong. Still, fixingisTree()
would result in a quadratic algorithm, when it should be linear. The idea is to get all the info you need at once.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;
}