Question

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( 80,200)

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

There are two functions maxOfRightElement and maxOfleftElement to find maximum elements in right and left subtree.

Node* newNode(int data)
{
   Node* temp = new Node;
   temp->data = data;
   temp->left = temp->right = NULL;
   return temp;
}  
int maxOfRightElement(Node* root)
{
  
   int res = INT_MIN;
   if (root == NULL)
       return -1;

   if (root->right != NULL)
       res = root->right->data;

   return max({ maxOfRightElement(root->right),
               res,
               maxOfRightElement(root->left) });
}
int maxOfleftElement(Node* root)
{
  
   int res = INT_MIN;
   if (root == NULL)
       return -1;

   if (root->left != NULL)
       res = root->left->data;

   return max({ maxOfleftElement(root->left),
               res,
               maxOfleftElement(root->right) });
}

Add a comment
Know the answer?
Add Answer to:
7) Recursion Write a recursive function max_tuple(a tree which takes a tree as a parameter and...
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
  • 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 python function that takes a binary tree as its only parameter. Return the value...

    Write a python function that takes a binary tree as its only parameter. Return the value stored in the shallowest leaf, anywhere in the tree. If there is a tie, then return the "leftmost" of the leaves (that is, the one that you would encounter first in an in-order traversal). You are allowed (and encouraged) to use a helper function. You may assume that the tree nodes have public fields left,right. The code cannot import anything from the Python standard...

  • Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that...

    Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that it is tail-recursive. You may use the letrec form but do not use any additional forms and functions besides those we've talked about in class. (define filter (lambda (input-list func)     (cond ((null? input-list) '())           ((func (car input-list))            (cons (car input-list) (filter (cdr input-list) func)))           (else            (filter (cdr input-list) func))))) 2. Test your filter function on '(25 -22 44 56...

  • In Java: Given the following binary tree class, write a recursive method called size() which will...

    In Java: Given the following binary tree class, write a recursive method called size() which will find the number of nodes in the subtree rooted at the current node: class myBinaryTree{ int myValue; myBinaryTree left; myBinaryTree right; myBinaryTree(int inValue) {myValue = inValue;} public int size(){ <CODE WRITTEN HERE> } }

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

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

  • 1) (10 pts) Write a recursive function that counts and returns the number of nodes in...

    1) (10 pts) Write a recursive function that counts and returns the number of nodes in a binary tree with the root root, that store an even value. Please use the struct shown and function prototype shown below. (For example, if the tree rooted at root stored 2, 3, 4, 8, 13, 18 and 20, the function should return 5, since there are five even values [2,4,8,18,20] stored in the tree. typedef struct node { int data; struct node* left;...

  • recursion java help 5. write a recursive program which takes a int number as a parameter...

    recursion java help 5. write a recursive program which takes a int number as a parameter and squares this int number until the result is greater than or equal to 18560. E.g., method(10)- 100000000; method 200)- 40000; method(20000)- 20000;

  • a) You must write a recursive function that takes something of the form: ([(Int, Int, Int),...

    a) You must write a recursive function that takes something of the form: ([(Int, Int, Int), Int, Int) as an argument and returns something of the form: LE(Int, Int, Int)]] which should be interpreted as a list of lists of 3-tuples of RGB values. You may use helper functions if you wish, but your solution must be recursive. You must document the name of your function at the beginning of your code using a comment like -- foo :: ([(Int,...

  • 2. Write a recursive algorithm which computes the number of nodes in a general tree. 3....

    2. Write a recursive algorithm which computes the number of nodes in a general tree. 3. Show a tree achieving the worst-case running time for algorithm depth. 4. Let T be a tree whose nodes store strings. Give an efficient algorithm that computes and prints, for every node v of T, the string stored at v and the height of the subtree rooted at v. Hin Consider 'decorating' the tree, and add a height field to each node (initialized to...

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