(COMP2012)[2014](s)final_=in8gpimcc^_56359.pdf

# Figure 1 an example of family tree you are given root

• Test Prep
• 23

This preview shows pages 19–22. Sign up to view the full content.

Figure 1 An example of family tree. You are given root , the pointer to the root of the tree (the eldest ascendant of all nodes) in the family tree. The node structure is the following: struct Node { string Name; vector<Node *> children; int numOfChildren; // simply the size of children vector }; That is, each node contains a Name string which is his/her name, an integer field numOfChildren which is the number of his/her children, and an array (vector) of Node pointers called children which points to his/her children (if any) in decreasing seniority with array index. For example, in Figure 1, the node George has field values Name = “George”, numOfChildren = 3, and children[0], children[1] and children[2] point to Sarah node, William node and David node, respectively. George Sarah W illiam David Edward Annie Harry Charles Level 1 Level 2 Level 3 (COMP2012)[2014](s)final~=in8gpimcc^_56359.pdf downloaded by xjiae from at 2018-03-13 17:27:35. Academic use within HKUST only.

This preview has intentionally blurred sections. Sign up to view the full version.

Page 20 of 23 (a) [6 marks] Suppose that you are to transform the family tree above so that the children are ordered according to their ascending seniority. For example, Figure 2 shows the transformed family tree from its original one of Figure 1. It is clearly a mirror image of the previous tree: Figure 2 Mirrored Family Tree Describe below an algorithm which takes in the root of the tree and changes it to a mirrored tree. void treeMirror(Node* root) { Node* temp; // 1pt , If tree is empty, terminate if (root == NULL) return; // 3pt , Recursive call for all children for (int i = 0; i < root->numOfChildren; i++) treeMirror(root->children[i]); // 2pt , Reverse all children in this node for (int i = 0, j = root->numOfChildren - 1; i < j; i++, j--) { temp = root->children[i]; root->children[i] = root->children[j]; root->children[j] = temp; } } George David W illiam Sarah Charles Harry Annie Edward Level 1 Level 2 Level 3 (COMP2012)[2014](s)final~=in8gpimcc^_56359.pdf downloaded by xjiae from at 2018-03-13 17:27:35. Academic use within HKUST only.
Page 21 of 23 (b) [7 marks] Given a family tree and a member’s name (assumed valid), you are to print out all his/her ancestors, if any, in reverse chronological order. For example, given a family tree in Figure 1 and a query name “Charles”, the output should be “David George”. If the query name is “George”, the output should be empty. Describe an efficient algorithm which achieves the above given the root of the tree and the query name . bool printAncestors(Node* root, string queryName) { // 1pt , If tree is empty, terminate if (root == NULL) return false; // 1pt , Current node matches query name if (root->name == queryName) return true; // 1pt , Recursive call for all children for (int i = 0; i < root->numOfChildren; i++) { // 3pt , If return true, current node is the ancestor if (printAncestors(root->children[i], queryName)) { cout << root->name << " "; return true; } } // 1pt , Current node is not the ancestor return false; } (COMP2012)[2014](s)final~=in8gpimcc^_56359.pdf downloaded by xjiae from at 2018-03-13 17:27:35. Academic use within HKUST only.

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern