1

I'm a newbie and trying to learn C, but faced some problem in implementing strings and pointer in a linked list. I was required to build a preorder tree using a linked list and one problem came out.

BTW here are my codes:

#include <stdio.h>
#include <stdlib.h>
struct BinTreeNode {
    char *data ;
    struct BinTreeNode *left;
    struct BinTreeNode *right;
};

struct BinTreeNode *initBinTreeNode(char *data);

void visit(char *data);

void preorder(struct BinTreeNode *node);
void postorder(struct BinTreeNode *node);
void inorder(struct BinTreeNode *node);

int main(void)
{
    struct BinTreeNode *root, *parent1, *parent2;
  /* growing the tree */
    root = initBinTreeNode("5");
    root->left  = initBinTreeNode("Mee (F) & Keong (M)");
    root->right = initBinTreeNode("Saw (F)");
    parent1 = root->left;
    parent1->left  = initBinTreeNode("Min (F) & Heng (M)");
    parent1->right = initBinTreeNode("Ning (F)");
    parent2 = parent1->left;
    parent2->left  = initBinTreeNode("Rui (F)");
  /* traverse and print tree */
    printf("\nPreorder traversal:\t");  preorder(root);
    
    return(0);
}

void preorder(struct BinTreeNode *node)
{
    if (node){  /* if Node exists */
        visit(node->data);
        preorder(node->left);
        preorder(node->right);
    }
}

void visit(char *data)
{
    printf("%c -", *data);
}

struct BinTreeNode *initBinTreeNode(char *data)
{
    struct BinTreeNode *temp;
    temp = malloc(sizeof(struct BinTreeNode));
    if (temp == NULL)
    {
        printf ("Memory allocation failed.");
        return NULL;
    }

    (*temp).data = data;
    (*temp).left = NULL;  
    (*temp).right = NULL;   
    return (temp);          
}

When I have {"5"},{"Mee (F) & Keong (M)"},{"Saw (F)"},{"Min (F) & Heng (M)"},{"Ning (F)"},{"Rui (F)"} in my list, it comes out only the first word is printed out like this:

Preorder traversal:     5 -M -M -R -N -S -

The preorder output that I want, should be:

Preorder traversal:     5 -Mee (F) & Keong (M) -Min (F) & Heng (M) -Rui (F) -Ning (F) -Saw (F) -

May I know, what I am missing? I would be really grateful for any help and advice!

Illustration of Preorder tree

1
  • 2
    Welcome to Stack Overflow. When you write code, it is wise to develop new functionality in isolation as much as possible, and get it to work perfectly, before introducing it into the whole. If you had tested visit when you first wrote it, you would have noticed the error, and known that the bug was in that function. Commented Jan 17, 2021 at 17:11

1 Answer 1

2

Change line (%c - prints single character, in this case first character of data):

printf("%c -", *data);

To (%s - prints entire string):

printf("%s -", data);
Sign up to request clarification or add additional context in comments.

2 Comments

!!!! Wow this really helped solving this problem that struggles me for hours! Thank you very much! Just another small request, can you explain a bit regarding the changes?
@youyiheng72 When this answer solves your problem, consider accepting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.