and to count no. of paths, you can do this void printPath(node *p,node *child) { static int iCountPath = 0; // or pass it as an argument non-const ref. if( p && child) { ++iCountPath; std::cout << "Path: " << p->data << " => " << child->data << std::endl; } }
-- Amitesh On Thu, Jun 28, 2012 at 11:01 AM, Amitesh Singh <singh.amit...@gmail.com>wrote: > void printPath(node *p,node *child) > { > if( p && child) > std::cout << "Path: " << p->data << " => " << child->data << > std::endl; > } > > use this before you assign 'current' to its children. > e.g. > > printPath(p,p->left) > or > printPath(p,p->right); > > > -- > Amitesh > > > > > On Mon, Jun 25, 2012 at 11:34 PM, Nishant Pandey < > nishant.bits.me...@gmail.com> wrote: > >> >> thiss is iterative inorder code i am using for printing all the paths of >> the Btree . >> >> but i am not able to manipulate the lengths properly when its perform pop >> up so that i >> can print the paths properly .. >> >> Can any one help by modifying the changes in this code . >> >> >> >> void inorder(struct node *root) >> { >> stack<struct node*>stack_temp; >> >> struct node *current,*temp; >> int path[20]; >> int i =0 ,j=0; >> if(!root)return ; >> >> current=root; >> bool flag=false; >> //bool ctrl=false; >> while(!flag) >> { >> >> if(current) >> { >> //cout<<current->data<<endl; >> stack_temp.push(current); >> path[i++]=current->data; >> if(current->left == NULL && current->right == NULL) >> { >> for(j=0;j<i;j++) >> cout<<path[j]<<" "; >> //i--; >> >> } >> >> cout<<endl; >> current=current->left; >> >> } >> else >> { >> if(stack_temp.empty()) >> { >> flag=true; >> } >> else >> { >> current=stack_temp.top(); >> stack_temp.pop(); >> >> i--; >> >> >> //if(current->right) >> //cout<<current->data<<endl; >> current=current->right; >> if(current) >> i++; >> } >> >> } >> >> } >> >> >> } >> >> >> >> -- >> Cheers, >> >> Nishant Pandey |Specialist Tools Development | >> npan...@google.com<gvib...@google.com> | >> +91-9911258345 >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.