简体   繁体   中英

Printing left and right values of an avl tree

I am trying to write a piece of code that displays the integers as they are being loaded into a binary tree. I have written this so far:

node*t;    
t = NULL;

for( j = 0; j < 33; j++)
{
    printf ("Table %d \n", j+1);
    printf ("LineNum  Left  Data   Right\n");
    printf ("%5d %5d %5d %5d", t->num, t->left->data, t->data, t-> right -> data);      
    t = insert(j, a[j], t );


}

The program still keeps crashing though. How can I solve that? Am i going about this the wrong way?

EDIT: I fixed the problem pointed out in the previous comments

printf ("%5d %5d %5d %5d", t->num, t->left->data, t->data, t-> left ->right);

------------------------------------------------------------------------------------------------^

Do you mean t->right->data ?

Seeing as how for left you are printing t->left->data , I'm guessing you want t->right->data not t-> left ->right .

If you are trying to print the pointer, cast t-> left ->right to an int ( unsigned int would be better)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM