Question

write a 3 recursive functions in C that return the sum, min and max of a...

write a 3 recursive functions in C that return the sum, min and max of a binary tree.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

struct Node {
int data;
struct Node *left;
struct Node *right;
};

int sum (struct Node *node) {
if (node == 0)
return 0;

return node->data + sum (node->left) + sum (node->right);
}

// Returns maximum value in a given Binary Tree
int max(struct node* root)
{
// Base case
if (root == NULL)
return INT_MIN;

// Return maximum of 3 values:
// 1) Root's data 2) Max in Left Subtree
// 3) Max in right subtree
int res = root->data;
int lres = max(root->left);
int rres = max(root->right);
if (lres > res)
res = lres;
if (rres > res)
res = rres;
return res;
}


// Returns minimum value in a given Binary Tree
int min(struct node* root)
{
// Base case
if (root == NULL)
return INT_MAX;

// Return minimum of 3 values:
// 1) Root's data 2) Max in Left Subtree
// 3) Max in right subtree
int res = root->data;
int lres = min(root->left);
int rres = min(root->right);
if (lres < res)
res = lres;
if (rres < res)
res = rres;
return res;
}

Add a comment
Know the answer?
Add Answer to:
write a 3 recursive functions in C that return the sum, min and max of a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Considering the binary search tree data structure, give non-recursive implementations of min(), max(), floor(), ceiling(), rank(),...

    Considering the binary search tree data structure, give non-recursive implementations of min(), max(), floor(), ceiling(), rank(), and select().

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • 7) Recursion Write a recursive function max_tuple(a tree which takes a tree as a parameter and...

    7) Recursion Write a recursive function max_tuple(a tree which takes a tree as a parameter and returns the max values of the right and left subtree. You can assume that the parameter is a full binary tree (in a pyramid shape like with more than 3 nodes. Your solution should not have any iteration. You can use a helper function if needed. Your code should be indented correctly. The expected return value of max_tuple(tree) for the tree below should be(...

  • Write a recursive function that returns the minimum key value in a binary search tree of...

    Write a recursive function that returns the minimum key value in a binary search tree of distinct (positive) integers. Return -1 in case the tree is empty. (b) Write a recursive function that returns the predecessor of the key value k in a binary search tree of distinct (positive) integers. This is the key value that precedes k in an inorder traversal of the tree. If k does not exist, or if k has no predecessor, your function should return...

  • C. What is the running time for the following approaches to solving the max sum of...

    C. What is the running time for the following approaches to solving the max sum of a consecutive subset of the array? Approach 1. initialize the max sum to be the first element in the array for a subset of size 1 for each consecutive subset of that size, calc the sum of the subset if the subset is larger than the current max, update the current max repeat for subset sizes 2 through n Approach 2. Divide the array...

  • Statistics Macro Assignment Write a macro to find the number of observations, max, min, sum, aver...

    Statistics Macro Assignment Write a macro to find the number of observations, max, min, sum, average and standard deviation for a column of numbers with any number of observations starting in cell A1 and proceeding downward. The number set will be of any length and include negative, zero, and positive integers. Display the results as shown below. Use the numbers below as an example. You may use the key board code “Selection.End(xlDown).Select” if you wish. Otherwise, use only VBA code...

  • c++ Write the following 2 functions and test them. Both of them need to be recursive...

    c++ Write the following 2 functions and test them. Both of them need to be recursive functions. int sum(int n); // recursive version to calculate the sum of 1 + 2 + ..... + n int str_length(char s[]; // Returns length of the string s[] and the null character, '0\', is not counted in the length). Example of program execution; Enter a positive integer: 10 (user input) The sum of 1+ 2+....+10 is: 55 Enter a sentence: Hello World! (user...

  • Rules: You may NOT use the following built-in functions. sort() sum() max() min() Except for format()...

    Rules: You may NOT use the following built-in functions. sort() sum() max() min() Except for format() all string methods are banned. Except for append() and extend() all list methods are banned. You may not import any module. Make sure to test your code with a variety of inputs. Do not assume the examples in these directions are reflective of the hidden test cases. Part 3 Echo (5 Points) Write a function called echo that takes as argument a list and...

  • C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise...

    C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise a number to a power. The function should accept two arguments, the number to be raised and the exponent. Assume that the exponent is a non-negative integer. String Reverser function that accepts a string object as its argument and prints the string in reverse order. Sum of Numbers this function accepts an integer argument and returns the sum of all the integers from 1...

  • Write a recursive function (C++) that searches a binary tree that is NOT ordered like a...

    Write a recursive function (C++) that searches a binary tree that is NOT ordered like a BST. In other words, there is no ordering relationship among the parent, child or sibling node values. Use the following prototype and data structure: struct node { node *left; node *right; int val; }; // First parameter: pointer to a node // Second parameter: the value to find bool searchValue(node *, int); The function searchValue() should return true if the integer value in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT