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...
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,...
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> } }
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
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.
write a complete Java program that defines a main method and another method called sum. method sum takes 2 integer values, say a and b, and returns the sum of all values between a and b. a must be less than b for the method to return the sum
Write a Java program named BSTree.java that will: 1) Generate 20 random integer numbers ranging from 1-99. 2) Build a Binary Search Tree using this set of numbers. Write the add method. 3) After building the tree, display the data into three formats: prefix order, infix order, and postfix order. 4) Write a method to delete an element from the Binary Search Tree. First search the item in your TREE and then delete it.