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);
}
}
Problems:
nodearray
is uninitialized - the pointers in it have indeterminate values. Dereferencing them invokes undefined behavior.
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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.