Question

Write a public method “getSum()” that returns the sum of all elements in binary tree. [Please...

Write a public method “getSum()” that returns the sum of all elements in binary tree. [Please check that the data are integers, otherwise throws exception]. Note: you should use internal recursive private method to print all paths.

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

Below is the Java function getSum which will recursively compute sum of all the nodes data value if all of them are integer, otherwise throw the exception if any of the data is not integer.

int getSum(node *root)

{

if (root == NULL) //if the root is NULL then simply return 0

return (0) ;

try

{

if ( root->data == (int) root->data) //then data is integer

return (root->data + getSum(root->left)+getSum(root->right));

else //throw the exception

throw new NotAnIntegerException();

}

catch (NotAnIntegerException )

{

System.out.Println("Data is not an integer");

}

}

Please comment for any clarification

Add a comment
Know the answer?
Add Answer to:
Write a public method “getSum()” that returns the sum of all elements in binary tree. [Please...
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
  • Write a public method “printRootToLeafPath()” that prints out ALL its root-to-leaf paths. Note: you should use...

    Write a public method “printRootToLeafPath()” that prints out ALL its root-to-leaf paths. Note: you should use internal recursive private method to print all paths java code

  • JAVA Write a method that returns the sum of all the elements in a specified column...

    JAVA Write a method that returns the sum of all the elements in a specified column in a matrix using the following header: public static double sumColumn(double [][] m, int columnIndex) Write another method that returns the sum of all the elements in a specified row in a matrix using the following header: public static double sumRow( double [][] m, int rowIndex) Write a test program that reads a 3-by-4 matrix and displays the sum of each column and sum...

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

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

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

  • This is for java programming. We need a public method for our Binary Search Tree ADT...

    This is for java programming. We need a public method for our Binary Search Tree ADT that returns a reference to the information in the node with the "smallest" value in the tree. The signature of the method is public T min() a. Design an iterative version of the method. b. Design a recursive version of the method. c. Which approach is better? Explain.

  • Suppose a binary tree stores integers. Write efficient methods (and give their Big-Oh running tim...

    java language please Suppose a binary tree stores integers. Write efficient methods (and give their Big-Oh running times) that root T and compute a. The number of even data items b. The sum of all the items in the tree 18.10 take a reference to a binary tree public static <AnyTypes void mysteryPrintC BinaryNode-AnyType t) If 1 2 3 4 5 6 7 if( t nu11) System.out.println( t.getElement); mysteryPrint( t.getleft()); System.out.println( t.getElement; mysteryPrint( t.getRight ); System.out.println( t.getElement()); 9 Suppose a...

  • Write A JAVA mergesort method that mergesorts array elements in descending order. Write A JAVA binary...

    Write A JAVA mergesort method that mergesorts array elements in descending order. Write A JAVA binary search method that searches x in a sorted array with n elements and returns its position if exist, -1 otherwise.

  • Consider a binary search tree of positive integers without duplicates. Implement (write out) a program to...

    Consider a binary search tree of positive integers without duplicates. Implement (write out) a program to find the second greatest element in the tree. The Second function is a member function of class BinarySearchTree. The function returns zero if the tree is empty or has only one element, otherwise it returns the value of the second greatest element. Based on the classes provided below, write the implementation of Second in the solution box, including any helper functions (if any) that...

  • EVERYTHING IN C# Write a program that includes a method that returns the sum of all...

    EVERYTHING IN C# Write a program that includes a method that returns the sum of all the elements of an ArrayList of Integer Objects. Allow the user to enter the integers to be added to your ArrayList from the console (max of 10 integers). And also write a program that includes a method that returns the sum of all the elements of a Linked List of Integers. Allow the user to enter the integers to be added to your Linked...

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