简体   繁体   中英

Printing elements from AVL Tree

I'm trying to write a code that prints 20 elements every time I go trough the tree. What I have done so far doesn't do it properly. I have tried to implement a global variable (called flag ) that each time I print the content of a node , I increment that variable but it doesn't work just right. I've also used a inorder tree search to print each element from the tree. This is what I have :

int inorder(Myprod a)
{  
int f=0;
char resposta[1];
int i;
int p;


if(a != NULL )
{ 

    inorder(a->left);
    printf("%s\n",a->prod);
    f++;
    flag += f;
    inorder(a->right);


    if(flag == 20)

    printf("Want to see more elements from tree ? (Y/N)\n");

    i = scanf ("%s" , resposta);

    if (resposta[0] == 'N' && i == 1)
    {
      return 1;
    }

    flag = f =  0;
     p = inorder(a);
     if (p == 1)
        return 1;

    }



return 0;
}

Desired output : 20 elements from tree; Ask the user if it wants more elements from tree; Next 20 elements ; ...

How can I implement it right ?

Just ran any DFS on the tree and after returning first 20 elements store the pointer to the node where you have finished. If there will be any future request of printing another 20 nodes just resume your DFS from the node where you finished the last time.

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