Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8913177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:23:37+00:00 2026-06-15T04:23:37+00:00

So Basically I want to only print the first 5 node of a AVL

  • 0

So Basically I want to only print the first 5 node of a AVL tree using recursion

    void printInOrder(void* theTree, void(*printnode)(void *data)) {

        struct AVLTreeNode *node= (struct AVLTreeNode*)theTree;

        if (node == NULL) return;

         printInOrder(node->left, printnode);

        (printnode)(node->data);

        printInOrder(node->right, printnode);

     }

the code above is a recursive inorder print i would just like to modify it to only print out the first 5 nodes of the tree. Also here is the printnode function

    void printnode(void *node){

        struct todo *printnode = node;

        printf("%s, %d\n", printnode->activity, printnode->priority);

    } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:23:38+00:00Added an answer on June 15, 2026 at 4:23 am

    You have to tell the function when to stop: you can supply a counter of how many nodes are still available to print (initially 5), and each time one node is actually printed, you decrement the counter.

    (Or you can increment the counter, but in that case the check has two terms – current counter value and maximum counter value – and you need to pass both as parameters, or make them global variables).

    If you need to do something in addition to print, or need a more flexible

    int printInOrder(void* theTree, int remaining, void(*printnode)(void *data))
    {
        if (0 == remaining)
            return 0;
        struct AVLTreeNode *node= (struct AVLTreeNode*)theTree;
        if (node == NULL) return remaining;
        remaining = printInOrder(node->left, remaining, printnode);
    
        // When actually printing nodes, decrement counter
        (printnode)(node->data); remaining--;
    
        return printInOrder(node->right, remaining, printnode);
    }
    
    printInOrder(tree, 5, ...);
    

    If you need to do something in addition to print, or need a more flexible function, you can do the same but passing the parameters to printnode:

    void printInOrder(void* theTree, int remaining, int(*printnode)(int r, void *data))
    {
        struct AVLTreeNode *node= (struct AVLTreeNode*)theTree;
        if (node == NULL) return remaining;
        printInOrder(node->left, remaining, printnode);
        remaining = (printnode)(remaining, node->data);
        printInOrder(node->right, remaining, printnode);
    }
    

    In this second case, it is printnode that manipulates the counter, while printInOrder just passes it along. Now, printnode gets called once for each node, but does the actual printing only when counter is nonzero, and can do any other housekeeping in all cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please see #7755661 first. I am using ECL and basically want to execute some
Basically I want to make an if statement that only shows the date of
I basically want to open a browser window from Word using VBA that does
Basically, I want to have an app with only one view that has an
I basically want something like: TextView Button Button . . . TextView TextView SeekBar
I basically want to setup a relationship similar to Netflix's DVD rental model. There
I basically want to do: git checkout branchA git checkout -b branchB <commit_id> which
I basically want the same functionality as facebook, twitter and all those other infinite
I basically want to do this in code: PersonList myPersonList; //populate myPersonList here, not
I basically want to be able to deploy multiple versions of the same EAR

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.