-1

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!

Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
Ryan
  • 301
  • 2
  • 14
  • `root->PrintObject(...)` doesn't look right. And you could probably simplify everything by using type Node for the root node as well. – nullptr Jan 25 '15 at 14:47
  • hey, i don't understand what's the problem in root->PrintObject()? – Ryan Jan 25 '15 at 15:07
  • Ah, sorry, I missed the sentence about getting too many brackets. (Well, `root->PrintObject` should not compile in `C` unless `Root` contains a member `PrintObject` which is a pointer to a function, and I really don't know if it is. Never mind.) Could you provide an example of which output you are getting vs. what you need? – nullptr Jan 25 '15 at 15:13
  • it's already provided above the code, i gave a few examples. – Ryan Jan 25 '15 at 15:20
  • I must learn to read properly first. Sorry. So, if I get the idea right, your code prints extra brackets for leaf nodes (i.e. nodes without children). I'd suggest not printing these brackets. You already have a condition that handles this case for root node (`root->left==NULL && root->right==NULL`), but it doesn't help with other nodes. Just add a similar one for currentNode. – nullptr Jan 25 '15 at 15:31

1 Answers1

0

problem solved, a condition was missing:

void PrintTree(Root* root, Node* currentNode,Boolean isFirst)
{
  if (root==NULL)
    return;
  else if (root->right==NULL&&root->left==NULL)
    {
      root->PrintObject(root->value); 
      return;
    }
  else if (currentNode!=NULL)
    {
      if(currentNode->left==NULL&&currentNode->right==NULL&&isFirst==FALSE)
        root->PrintObject(currentNode->value);
        return;
    }
  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;
}
Ryan
  • 301
  • 2
  • 14