crecursionbinary-search-treeinorder

Inorder Traversal - Add Node Data to Int Array in C Langauge


I am implementing inorder traversal algorithm in C language and below is my code. My task is to add each node's data in nodeDataList. However, nodeDataList is storing weird data,

void inorder(Node* N, int* nodeDataList, int index){
   if(N != NULL){
       inorder(N->left, nodeDataList, index);
       printf("%d: %d ", index, N->data);
       nodeDataList[index++] = N->data;
       inorder(N->right, nodeDataList, index);
   }
}

Below is the set up in main function,

struct Node root = {27, NULL, NULL};
struct Node root_left = {26, NULL, NULL};
struct Node root_right = {39, NULL, NULL};
struct Node root_right_right = {48, NULL, NULL};
struct Node root_right_left = {40, NULL, NULL};
struct Node *N;
struct Node *N_left;
struct Node *N_right;
struct Node *N_right_right;
struct Node *N_right_left;
N = &root;
N_left = &root_left;
N_right = &root_right;
N_right_right = &root_right_right;
N_right_left = &root_right_left;

N_right->right = N_right_right;
N_right->left = N_right_left;

N->left = N_left;
N->right = N_right;
int result = 5;

int nodeDataList[result];
int index = 0;

inorder(N, nodeDataList, index);
printf("\n");

printf("%d\n", nodeDataList[0]);
printf("%d\n", nodeDataList[1]);
printf("%d\n", nodeDataList[2]);
printf("%d\n", nodeDataList[3]);
printf("%d\n", nodeDataList[4]);

When running the code, I am getting the following data,

0: 26 0: 27 1: 40 1: 39 2: 48 
27
39
48
-1427448988
-1086203488

I am not sure what -1427448988 and -1086203488 are. I have created the inorder correctly but the int array is generating incorrect data at indices 3 and 4.


Solution

  • You have a problem with passing the last argument index. Passing it by value doesn't preserve its value between subsequent calls.

    Try this out:

    #include <stdio.h>
    
    struct Node
    {
        int data;
        struct Node *left;
        struct Node *right;
    };
    typedef struct Node Node;
    
    void inorder(Node* N, int* nodeDataList, int *index){
       if(N != NULL){
           inorder(N->left, nodeDataList, index);
           printf("%d: %d ", *index, N->data);
           nodeDataList[(*index)++] = N->data;
           inorder(N->right, nodeDataList, index);
       }
    }
    
    
    
    int main(void){
        struct Node root = {27, NULL, NULL};
        struct Node root_left = {26, NULL, NULL};
        struct Node root_right = {39, NULL, NULL};
        struct Node root_right_right = {48, NULL, NULL};
        struct Node root_right_left = {40, NULL, NULL};
        struct Node *N;
        struct Node *N_left;
        struct Node *N_right;
        struct Node *N_right_right;
        struct Node *N_right_left;
        N = &root;
        N_left = &root_left;
        N_right = &root_right;
        N_right_right = &root_right_right;
        N_right_left = &root_right_left;
    
        N_right->right = N_right_right;
        N_right->left = N_right_left;
    
        N->left = N_left;
        N->right = N_right;
        int result = 5;
    
        int nodeDataList[result];
        int index = 0;
    
        inorder(N, nodeDataList, &index);
        printf("\n");
    
        printf("%d\n", nodeDataList[0]);
        printf("%d\n", nodeDataList[1]);
        printf("%d\n", nodeDataList[2]);
        printf("%d\n", nodeDataList[3]);
        printf("%d\n", nodeDataList[4]);
    }
    ``