//Family Tree Example by Caner Koca for MAT 2540
/*
Based on the following tree:
Adam
.____|___.______________.
Ben Charlie David
| .__|__.
Ed Fred Greg
*/
#include <iostream>
#include <string>
using namespace std;
//each node will contain 4 information: name of the person
//and addresses of up to 3 children
class node{
public:
string name;
node* child1;
node* child2;
node* child3;
};
//The following function answers the question: Is X a child of Y?
bool IsChild(node &X, node &Y){
if(Y.child1==&X || Y.child2==&X || Y.child3==&X){//if the address of one of the children of Y is the address of X,
cout<<X.name<<" is a child of "<<Y.name<<endl;//print a statement "X is a child of Y"
return true;
}
else{
cout<<X.name<<" is not a child of "<<Y.name<<endl;
return false;
}
}
int main() {
//initializing the nodes
node A,B,C,D,E,F,G;
//furnishing nodes with names
A.name="Adam";
B.name="Ben";
C.name="Charlie";
D.name="David";
E.name="Ed";
F.name="Fred";
G.name="Greg";
//establishing parent-child relations
A.child1=&B;
A.child2=&C;
A.child3=&D;
C.child1=&E;
D.child1=&F;
D.child2=&G;
//checking whether the function IsChild is working properly.
IsChild(B,A);
IsChild(A,B);
IsChild(F,D);
IsChild(F,A);
return 0;
}
Ly9GYW1pbHkgVHJlZSBFeGFtcGxlIGJ5IENhbmVyIEtvY2EgZm9yIE1BVCAyNTQwCi8qCkJhc2VkIG9uIHRoZSBmb2xsb3dpbmcgdHJlZToKCiAgICAgQWRhbQogIC5fX19ffF9fXy5fX19fX19fX19fX19fXy4KQmVuCQlDaGFybGllCQkJRGF2aWQKICAgICAgICAgIHwgICAgICAgICAgICAgLl9ffF9fLgoJCSAgRWQJCSAgIEZyZWQgIEdyZWcKKi8KCgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxzdHJpbmc+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovL2VhY2ggbm9kZSB3aWxsIGNvbnRhaW4gNCBpbmZvcm1hdGlvbjogbmFtZSBvZiB0aGUgcGVyc29uCi8vYW5kIGFkZHJlc3NlcyBvZiB1cCB0byAzIGNoaWxkcmVuCmNsYXNzIG5v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