i've been working on a function that basically is supposed to print the binary tree i have in the program, and i'm having a problem. the tree is dynamice, and the values are numbers, bigger new number will be added in the right side, and smaller in the left side.
basically the way the tree is supposed to be printed is this: let's say there is only the root with the value 3, then the output will be:
(3)
after adding node with the value 2:
((2)<(3))
the output I'm getting:
(((2))<(3))
after adding 8:
((2)<(3)>(8))
the output I'm getting:
(((2))<(3)>((8))))
after adding 7:
((2)<(3)>((7)<(8)))
the output I'm getting:
(((2))<(3)>(((7))<(8)))
and so on... notice that the first node, the root has a different struct, therefore the saparation to 2 cases.
the main problem is I'm getting too many brackets.
this is the code i wrote so far:
void PrintTree(Root* root, Node* currentNode,Boolean isFirst) {
if (root == NULL)
return;
else if (root->right == NULL && root->left == NULL) {
root->PrintObject(root->value);
printf("\n");
} else {
printf("(");
if (isFirst == TRUE) {
if (root->left != NULL) {
PrintTree(root,root->left, FALSE);
printf("<");
}
root->PrintObject(root->value);
if (root->right != NULL) {
printf(">");
PrintTree(root,root->right, FALSE);
}
} else {
if (currentNode->left != NULL) {
PrintTree(root,currentNode->left, FALSE);
printf("<");
}
root->PrintObject(currentNode->value);
if (currentNode->right != NULL) {
printf(">");
PrintTree(root,currentNode->right, FALSE);
}
}
printf(")");
}
return;
}
void PrintObject(void* value) {
printf("(%d)", *(int*)value);
return;
}
would appreciate any kind of help. thanks!