Q:

Tree recursion bugs

After debugging literally since Sunday the same code, I've officially given up and think its time someone else takes a look. I have spent hours and hours and cant image whats wrong with my code. Its a bit long, but its easy to follow, and I would really appreciate any help-you're the only choice I got now. 
What I'm trying to do is implement a few functions that collectively do this: Given a quad tree where each leaf represents 1 pixel of an image, prune the tree to remove or combine similar colors. Each tree has a root node with 4 children, and so on. Each node has x,y,width,height, and element where width and height represent the block of pixels that represents that area and x,y represent the lower left corner pixel of the node and element is a type pixel. Each non-leaf node stores the average color of all of its children. If the difference in color of a certain non-leaf node myNode with one of its children leaf nodes is greater than the parameter tol(or tolerance), then you cant prune. If there are no such cases, then you prune, meaning you'd remove all of the children of myNode, and myNode would then just store the average value of all the nodes it just deleted (which should be just automatic when the average is calculated in the code initially).
So, here's my codeflow:

First I calculate and store the
average of each node into its element
pixel field.
Now an outside function calls pruneSize on some tree. pruneSize does not actually prune, but just returns the number of leaves that would be remaining in the tree if you were to prune.
From there, the code is commented. Basically in the deeper and signal function, I tried to make it so that if orig cannot be pruned for some reason, then now you have to check origs children to see if they can be pruned, and so on. 

The problem is that I am not getting the correct output when I run it. It's off bigtime. I'm hoping the error will be easy to spot for the experienced among you. 
(Also, the quadtree class is implemented perfectly. Everything else has been tested and works fine, so its something with my average calculator or pruneSize.)

A:

I could be wrong here (just skimmed over it and can't verify right now), but as far as I'm aware, to square in C++, you need to explicitly multiply a number with itself. Doing A^2 will calculate A XOR 2.

