Write a public method in java called average that finds the average of all values in a Binary Search Tree. You may assume items are always integer.
Here is the completed code for this problem based on the limited info provided. Assumptions are included in comments. Comprehensive comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
/**
* method to calculate the average of all values in a BST. Assuming there is
* a variable called root that denotes the root node of the binary search
* tree. And assuming that Node class has a data field (int) and left and
* right Node pointers (Node type). this method uses two helper methods to
* assist the process, those are attached below.
*
* @return the average value.
*/
public double average() {
// finding sum of all values
double total = sum(root);
// finding count of nodes
double count_nodes = count(root);
// dividing sum by count to get average, assuming BST is not empty
double avg = (double) total / count_nodes;
return avg;
}
// helper method to find the sum of all nodes, recursively
private double sum(Node n) {
// if n is null, returning 0 (base case)
if (n == null) {
return 0;
}
// adding data of node n with sum values of left and righ subtrees and
// returning this value.
return n.data + sum(n.left) + sum(n.right);
}
// helper method to find the number of nodes, recursively
private int count(Node n) {
if (n == null) {
// base case
return 0;
}
// finding number of nodes on left and right, adding 1 to it and
// returning it
return 1 + count(n.left) + count(n.right);
}
Write a public method in java called average that finds the average of all values in...
answer in java
Given a Binary Search Tree containing integer values, write a method to print the values in descending order. public class Treenode { int val; Treenode left; Treenode right; Treenode (int x) { val = x; } The method signature is: public void reverseSorted(Treenode root){ // your code
Write a public static method called ave in java to return the average of two doubles.
bool binarySearch(int a[], int size, int k); Write a function called binarySearch that finds an occurrence of an integer k in an array a using binary search. The function signature is given above. Write your own implementation of binary search; do not use the search function available in the C++ standard library. Binary search assumes that the sequence of values (stored in an array in our case) is either non-decreasing or non-increasing. Assume that the array passed into binarySearch is...
I need java code for the following problem.
Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...
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
in java ..write all complete program from a- e
1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and the resulting trees. Note: To test your algorithm, first create a binary search tree. Write a method called singleParent,...
Implement a method (in java) to print all the element of a binary search tree in pre-order traversal order. (CANNOT TAKE A PARAMETER) Thank you! public void printTree() { //chegg code here only! }
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> } }
Java: Write an application that has an array of at least 20 integers. It should call a method that uses the sequential search algorithm to locate one of the values. The method should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another method that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these...
Problem 16)(Java) Write a complete definition for a public static method called closeEnough that takes a double and an integer and returns true if the double is within one unit of the integer. For example closeEnough(3.9, 5) -> false closeEnough(4.1, 5) -> true closeEnough(6.0, 5) -> true