0

Is it possible to print an array of nodes? I need to display the AVL tree as it is being built, but whenever I run this code, the program crashes. Any alternative ways around this?

     int k = 0;
    t = NULL;
    node* nodearray[32];
    for( j = 0; j < 33; j++)
    {
        printf ("Table %d \n", j+1);
        printf ("LineNum Left Data Right\n");
        t = Insert(j, a[j], t );

    for (k= 0 ; k < j ; k ++)
    {
         printf ("%5d %5d %5d %5d", nodearray[k]->num, nodearray[k]->left->data, nodearray[k]->data, nodearray[k]-> right ->data);
    }
}
user1816546
  • 313
  • 4
  • 15

1 Answers1

2

Problems:

  1. nodearray is uninitialized - the pointers in it have indeterminate values. Dereferencing them invokes undefined behavior.

  2. for( j = 0; j < 33; j++) - but you declared nodearray to be 32 elements long. It's hard to tell without seeing the implementation of Insert(), but probably you also have an off-by-one error (you're reading/writing past the end of the array).