I am writing a program using binary tree. I have an average to display, but each time I call it, the error produces "conversion from int to float, possible loss of data"
I know i'm likely using static cast incorrectly, just hoping someone could properly fix my mistake. Here's what I have
1 2 3 4 5 6
int treeSum, numNodes;
float avg;
cout << "E. Sum and Average of the tree: " << endl;
treeSum = root.sum();
avg = static_cast<float>(treeSum / numNodes);
cout << "\tSum: " << treeSum << " Average: " << fixed << setprecision(1) <<avg << endl << endl;
You lost accuracy through integer division before using the ugly static_cast<>(). Try avg = static_cast<float>(treeSum) / numNodes;
or (asking for trouble here!) avg = (treeSum + 0.0) / numNodes;
I recommend you use double rather than float as well.
That's called function overloading, when two functions have the same name, but different numbers of parameters or types of parameters.
The purpose, in this case, is to separate the interface of the code from its implementation.
A user of the class is meant to call the 1-arg insert, because the user should not have to care about the fact that a tree is implemented with a root node, or that it has "nodes" or pointers at all. The user just cares that they have a tree, and can insert values (ints, in this case) into the tree.
The author of the code felt it was convenient to have the parent node passed as a parameter to the 2-arg insert function. Perhaps there were other parts of the code where insert(node*, int) was called without the node pointer being the root.
Ideally, the first insert function you show should be private to the class, and the second insert function is the public-facing one that the user can directly call.
void printInorder(node* root, int& cnt) //function to print the tree in order
{
if (root == NULL)
return;
//// Can I add something like this?
if(deleteMethod)
cnt < 25;
//// or should i just create a second printInOrder method?
if (cnt < 20)
{
cnt++;
printInorder(root -> LC, cnt);
cout << root -> num << " ";
printInorder(root -> RC, cnt);
}
}
void printInorder(int& cnt)
{
printInorder(root, cnt);
}
To add: the program has me print the first 20 values of my tree, then when the delete method is called, i'm to reprint the first 25 values. and print the in order
On my assignment there is a requirement to delete nodes that add up to be less than 10.
My question is, within my printInOrder method, can I add an 'IF' statement or any other statement that will allow the method to check IF the delete method has been used/called and then change its output quantity to 25 from 20, so i don't have to make another method just for the 25 numbers?
I'm a little slow, thats why my communcation is so awful. Apologies! I'm not quite sure how else to explain and or ask what i'm trying to convey.
Here is the current delete method. The PrintInOrder mehtod is above.
I'm wanting my code to verify that the delete has been called and jump to the in-order method and change its quantity from 20 to 25.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
node* leafDelete(struct node* root) //deletes nodes in the tree <10
{
if (root == NULL)
return NULL;
if (root->LC == NULL && root->RC == NULL)
{
if (sumDigits(root->num) < 10)
{
delete(root);
return NULL;
}
}
root->LC = leafDelete(root->LC);
root->RC = leafDelete(root->RC);
return root;
}
class tree{
//...
bool delete_method_was_called = false;
node* leafDelete(struct node* root){
this->delete_method_was_called = true; //¿when to reset?
//...
}
};
but that is awful, consider instead
1 2 3 4 5
void printInorder(node* root, int& cnt, int limit){
if (cnt < limit)
//recurse
}
printInorder(this->root, cnt, 42);
> Apologies! I'm not quite sure how else to explain and or ask what i'm trying to convey.
rephrase, show examples, draw a diagram
we are rubber ducks https://rubberduckdebugging.com/
I'm having a small issue with the digitsum within the leafdelete method.
Uncertain where or what is causing the issue. I believe the digit sum method works just fine. But when I call that method within the delete method, its not deleting the correct nodes, as 100 = 1 + 0 + 0 = 1 which is less than 10. and it's still being printed to the output.
The sumdigit is having issues with 3 digit numbers
Here is the LeafDelete method and the output I am getting from it
If no rubber duck is available try "Lucy 'splainin'" the code to a rock. A rock is a not-so distant relative to a CPU. Same steps apply as with a rubber duckie.