void PrintFamilyTree(const short& generation) { printf("Name : %s Generation = %d\n", m_Name.c_str(),generation);
vector<Human*>::iterator it; for (it = this.begin(); it< this.end() ;it++) { it->PrintFamilyTree(generation+1); } } On Tue, Nov 24, 2009 at 9:20 PM, Aditya Shankar < iitm.adityashan...@gmail.com> wrote: > > Hi, > > On Tue, Nov 24, 2009 at 6:38 PM, Ankur <ankuraror...@gmail.com> wrote: > >> What would be the best algorithm for the undermentioned problem. >> >> Implement method PrintFamilyTree() that prints out the Name and >> Generation of the tree >> Output should resemble >> Name: Jan Generation:0 >> Name: Mike Generation:1 >> Name: Greg Generation:2 >> Name: Carol: Generation: 2 >> Name: Peter Generation: 3 >> Name: Marcia Generation: 3 >> Name: Bobby Generation: 1 >> >> The order you have mentioned is preorder traversal of the tree. > > Regards > Aditya Shankar > >> class Human : public std::vector<Human *> >> { >> public: >> Human(const std::string &name) : m_Name(name) {}; >> virtual void PrintFamilyTree(const short &generation = 0) const; >> protected: >> std::string m_Name; >> }; >> >> class Male: public Human >> { >> public: >> Male(const std::string &name) : Human(name) {}; >> }; >> >> class Female: public Human >> { >> public: >> Female(const std::string &name) : Human(name) {}; >> }; >> >> void main() >> { >> Male m1("Mike"), m2("Greg"), m3("Peter"), m4("Bobby"); >> Female f1("Carol"), f2("Marcia"), f3("Jan"); >> >> m1.push_back(&m2); >> f1.push_back(&m3); >> f1.push_back(&f2); >> m1.push_back(&f1); >> f3.push_back(&m1); >> f3.push_back(&m4); >> >> f3.PrintFamilyTree(); >> >> -- >> >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algoge...@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@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 algoge...@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@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 algoge...@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.