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

# Page 22 of 23 c 8 marks we would like to print out

• Test Prep
• 23

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

Page 22 of 23 (c) [8 marks] We would like to print out the names in the family tree according to the order of the generations (or levels). The output should be in spiral order, where the names should be printed from the left to the right on the even levels, and in the opposite direction on the odd levels. We show an example in Figure 3, where the output sequence has been indicated as the dashed line. Accordingly, the output is “George Sarah William David Charles Harry Annie Edward“. Figure 3 Spiral Traversal of a family tree. (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 23 of 23 Describe an efficient algorithm which prints out the names in spiral order given the root to the tree. void spiralLevelOrderTraversal(Node* root) { // 1pt , If tree is empty, terminate if(root == NULL) return; // 2pt , Use two stacks stack<Node*> stack1; stack<Node*> stack2; // 1pt, Insert root and start loop stack1.push(root); while (stack1.empty() == false || stack2.empty() == false) { // 2pt , Current odd level // Push their children from right to left into stack2 while (stack1.empty() == false) { Node* node = stack1.top(); stack1.pop(); cout << node->name << " "; for (int i = node->numOfChildren - 1; i >= 0; i--) stack2.push(node->children[i]); } // 2pt , Current even level // Push their children from left to right into stack1 while (stack2.empty() == false) { Node* node = stack2.top(); stack2.pop(); cout << node->name << " "; for (int i = 0; i < node->numOfChildren; i++) stack1.push(node->children[i]); } } } === END OF PAPER === (COMP2012)[2014](s)final~=in8gpimcc^_56359.pdf downloaded by xjiae from at 2018-03-13 17:27:35. Academic use within HKUST only.
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