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
  • C++ Data Structures and Algorithms Binary Trees: Implementation Answer the following question(s) concerning implementing recursive functions...

    C++ Data Structures and Algorithms Binary Trees: Implementation Answer the following question(s) concerning implementing recursive functions to perform operations on linked lists of nodes arranged as binary trees. For these questions, use the following struct definition for the nodes of the tree (we will not templatize the node here, so you do not have to write template functions for these questions, just assume trees of <int>values): struct BinaryTreeNode { int item; BinaryTreeNode* left; BinaryTreeNode* right; }; Write a recursive function...

  • 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....

  • Write a recursive algorithm in a pseudo code, Min-Max, for finding both the minimum and the...

    Write a recursive algorithm in a pseudo code, Min-Max, for finding both the minimum and the maximum elements in an array A of n elements. Your algorithm calls itself only once within the algorithm and should return a pair (a, b) where a is the minimum element and b is the maximum element.

  • PROBLEM: Write a recursive method named Addition that takes two integers X and Y returns their...

    PROBLEM: Write a recursive method named Addition that takes two integers X and Y returns their sum. Please comment on every line of code. EXISTING CODE: int AddToN(int n) { if (n == 0) { // base case return 0; } else { // recursive case return n + AddToN(n-1); //make recursive call } } void printStars (int n) { if (n==0 ) { //base case cout <<""; } else{ printStars(n-1); cout << ""; } } } int sumRange (...

  • 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...

  • 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...

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