19.BinaryTrees-PracticeProblems

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

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

View Full Document Right Arrow Icon
(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
Background image of page 1

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

View Full DocumentRight Arrow Icon
[ 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);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/22/2010 for the course EECS COP 3502 taught by Professor Guha during the Spring '09 term at University of Central Florida.

Page1 / 8

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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online