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 8683773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:07:50+00:00 2026-06-12T22:07:50+00:00

While printing Binary Search Tree(BST) using recursive function (pre-order). I need to print all

  • 0

While printing Binary Search Tree(BST) using recursive function (pre-order). I need to print all the parents(path form root) of current node.
An auxiliary data structure can(e.g. path in my code) be use but I don’t want to keep node->path to store path.

      4                           
     / \  
    /   \  
   2     6
  / \   / \
 1   3  5  7  

Suppose I am printing nodes in rows using pre-order traverse:

NODE    PATH  
4       4  
2       4,2  
1       4,2,1
3       4,2,3       
6       4,6
5       4,6,5
7       4,6,7  

I did as follows: Working fine!
Path end with 0 (Zero) value in this code. And there is no node value is 0 in BST.

void printpath(int* mypath){
   while(*mypath)  
      printf("%d ", *mypath++);  
}

void preorder(struct tree *p, int* path){
    int *mypath = calloc(sizeof(path)/sizeof(int) + 1 , sizeof(int*));
    int* myp=mypath;

    if(p!=NULL){  
       while( *myp++ = *path++ );  
       --myp;
       *myp=p->data;
       *(myp+1)=0;

        printf("%d PATH ",p->data);
        printpath(mypath);
        printf("\n");
        preorder(p->left, mypath);
        preorder(p->right, mypath);
    }
    free(mypath);
}

But I don’t want to keep path array as there is lots of nodes in BST. Can some one suggest me other data-structure/ or method ? A suggestion would be enough but should be efficient.

  • 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-12T22:07:52+00:00Added an answer on June 12, 2026 at 10:07 pm

    Here’s an old trick, which still works: keep the back pointers in the call stack.

        struct stacked_list{
          struct stacked_list* prev;
          struct tree* tree; 
        };
    
       void printpath_helper(int data, struct stacked_list* path) {
          if (!path->prev)
            printf("%d PATH ", data);
          else
            printpath_helper(data, path->prev);
          printf("%d ", path->tree->data);
        }
    
        void printpath(struct stacked_list* path) {
          printpath_helper(path->tree->data, path);
          putchar('\n');
        }
    
        void preorder_helper(struct stacked_list* path) {
          if (path->tree) {
            printpath(path);
            struct stacked_list child = {path, path->tree->left};
            preorder_helper(&child);
            child.tree = path->tree->right;
            preorder_helper(&child);
          }
        }
    
        void preorder(struct tree* tree) {
          struct stacked_list root = {NULL, tree};
          preorder_helper(&root);
        }
    

    Each recursion of preorder_helper creates an argument struct and passes its address to the next recursion, effectively creating a linked list of arguments which printpath_helper can walk up to actually print the path. Since you want to print the path from top to bottom, printpath_helper needs to also reverse the linked list, so you end up doubling the recursion depth of the function; if you could get away with printing bottom to top, printpath_helper could be a simple loop (or tail recursion).

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

Sidebar

Related Questions

Print a web page in landscape mode using javascript. While printing, I want to:
I am facing a strange issue, While printing a page using window.print(), I am
I'm creating a invoice and have option to print the invoice. While printing i
I tried the following code to print all the white pixels of this binary
I need to do this trick while printing huge HTML tables: +-----+---------+-----+ | ID
While deveoping a site (using Forms authentication and InProc sessionstate) a frequently run into
While developing an application using gwt in ecliplse crashed. Now the server is running
While reading this class BitmapFactory I noticed that almost all methods inside are static.
Related question: Time Complexity of InOrder Tree Traversal of Binary Tree O(N)? , however
I got an exception with message No printers are installed. while printing a report

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.