{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

19.BinaryTrees-PracticeProblems

19.BinaryTrees-PracticeProblems - 5 2 7 1 3 6 9 1 3 6 8 9 1...

This preview shows pages 1–3. Sign up to view the full content.

(c) Execute the algorithm shown below using the tree shown above. Show the exact output produced by the algorithm. Assume that the initial call is: prob3(root) and that the tree nodes and pointers are defined as shown. struct treeNode{ int data; struct treeNode *left, *right; } struct treeNode *tree_ptr; void prob3(struct tree_ptr *node_ptr) { if (node_ptr != NULL){ if (node_ptr->data % 3 == 0){ printf(“%d “, node_ptr->data); prob3(node_ptr->left); } else{ if (node_ptr->data % 3 == 1){ printf(“%d “, node_ptr->data+2); prob3(node_ptr->right); } else{ if (node_ptr->data % 3 == 2){ prob3(node_ptr->left); prob3(node_ptr->right); } } } } } Output: 12 18 30 72 90 87 5 2 7 1 3 1 1 3 2 3 6 6 6 9 8 9

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

View Full Document
1. [ 9 pts ] For the binary tree given below root is a pointer to the root of the tree. Redraw (on the following page) the tree shown above when the following function is executed. Assume that the initial call is modifyT(root, 7, 65). struct treeNode { int data; struct treeNode *left; struct treeNode *right; }; void modifyT(struct treeNode* node_ptr, int key, int num) { if (node_ptr != NULL) { if (node_ptr->data % 3 == 0) { node_ptr->data += key; modifyT(node_ptr->left, key + 2, num - key); modifyT(node_ptr)->right, key – 3, num + key); } else if (node_ptr->data % 5 == 0) { node_ptr->data -= key; modifyT(node_ptr->right, key - 4, num); modifyT(node_ptr->left, key, num + 5); } else { node_ptr->data = num;
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