Click here to Skip to main content
15,902,939 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
3,1 //connected to each other directed 3 to 1
3,2
3,5
1,6
1,7
5,8



//function used to print path
void descendents(struct node * n)
{
if (n!=NULL)
{printf("-> %d",n->data);
descendents(n->next1);
descendents(n->next2);
descendents(n->next3);
}
}


for 3
output is


3->1->6->7->2->5->8







but i want output such that

3->1->6
3->1->7
3->2
3->5->8




//what changes in function i have to do
Posted
Updated 22-Aug-14 6:19am
v3
Comments
Sergey Alexandrovich Kryukov 22-Aug-14 12:14pm    
If you start your sentence from the middle, as you do, don't expect people to understand it.
—SA

you must redesign the interface

C++
int descendents(struct node * n, int index = 0);//print only the n-node path

int r = 0;

while( r > -1 )
{
  r = descendents( n, r );
}


And implement it.
It will be a bit tricky, because you must recurse somehow BUT remember where you have been.
 
Share this answer
 
You are traversing the tree in so-called in-order sequence. That's okay. To achieve what you want, you should do the following:

(a) store in every node its parent node (in addition to the links you already have)

(b) print only something when you have reached an leaf-node (one without further down-links)

(c) in that case print the entire path from the root to your node, which is a little tricky:

(c1) store the nodes from your leaf-node upward by backtracking along the parent links to the root; store them in an array or stack

(c2) print the stored nodes in reverse order, i.e. from root to leaf

Hope that helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900