-1

Let's say i need to build this tree from the digits 61207895 and then perform a pre-order traversal of the tree.

         6
        / \
       /   \
      /     \
     1       7
    / \     / \
   0   2   5   8
                \
                 9

The expected result would be: 6 1 0 2 7 5 8 9

But my code outputs: 6 1 0 2 5 7 8 9

For some reason the position of digits 5 and 7 is switched. Could anyone help me to solve this problem?

This is my main function:

int main()
{
    char* digits = malloc(9 * sizeof(char));
    printf("Digits:");
    scanf("%s", digits);

    Node* node = createNode(digits[0] - '0');

    for (int i = 1; i < strlen(digits); i++)
    {
        insertNode(node, digits[i] - '0');
    }
    free(digits);

    printf("Pre-order Traversal: ");
    preorderTraversal(node);
    return 0;
}

This is my function to insert nodes in the tree:

Node* insertNode(Node* node, int data)
{
    if (node != NULL)
    {
        if (data < node->data)
        {
            node->left = insertNode(node->left, data);
        }
        else
        {
            node->right = insertNode(node->right, data);
        }
        return node;
    }
    else
    {
        return createNode(data);
    }
}

And this is my function to perform the Pre-order Traversal:

void preorderTraversal(Node* node)
{
    if (node != NULL)
    {
        printf("%d ", node->data);
        preorderTraversal(node->left);
        preorderTraversal(node->right);
    }
}
Varnion
  • 3
  • 2
  • 3
    `char* digits = malloc(sizeof(char));` ===> `digits` is only long enough for just the empty string!! You cannot hold `"61027589"` in there!! – pmg Jun 27 '22 at 08:23
  • @pmg When I ran the code, `char* RA = malloc(sizeof(char))` was able to store the entire string, but anyway, I edited it to `char* RA = malloc(8 * sizeof(char))`. – Varnion Jun 27 '22 at 08:29
  • 1
    `8` bytes is not enough for `"61027589"`. You need to account for the terminating zero byte!! – pmg Jun 27 '22 at 08:31
  • @pmg edited again. – Varnion Jun 27 '22 at 08:39
  • @SupportUkraine This is a homework I need to do for my course. The professor wrote in the question that we should build this tree from these digits, following the rule `"Whenever new data is to be added to the tree, it will be compared to the root node. If it is smaller than the root, it must be added to the left sub-tree, otherwise the right sub-tree"`. In the example given by him, with the digits `61207895` the tree and the result should be the ones I wrote in the post. But the tree I end up with is the one pointed out by @pmg. – Varnion Jun 27 '22 at 09:00
  • @SupportUkraine Yeah, I thought the question had a problem. But in my mind, the chance that I got something wrong was greater than the chance that the professor wrote a bug. Apparently I was wrong. Thank you for your help. – Varnion Jun 27 '22 at 09:13
  • Voting to close this as a typo – klutt Jun 27 '22 at 09:25

1 Answers1

2

Because you end up with

         6
        / \
       /   \
      /     \
     1       7
    / \       \
   0   2       8
        \       \
         5       9
pmg
  • 106,608
  • 13
  • 126
  • 198