Question: why is my code is not working import java.util.ArrayList; class TreeNode { E element; TreeNode left; TreeNode right; TreeNode ( E element ) { this.element

why is my code is not working import java.util.ArrayList;
class TreeNode {
E element;
TreeNode left;
TreeNode right;
TreeNode(E element){
this.element = element;
this.left = null;
this.right = null;
}
}
class BinaryTree>{
TreeNode root;
public BinaryTree(){
this.root = null;
}
public boolean insert(E e){
if (root == null){
root = new TreeNode<>(e);
return true;
} else {
TreeNode parent = null;
TreeNode current = root;
while (current != null){
parent = current;
if (e.compareTo(current.element)<0){
current = current.left;
} else if (e.compareTo(current.element)>0){
current = current.right;
} else {
return false; // Duplicate node not inserted
}
}
if (e.compareTo(parent.element)<0){
parent.left = new TreeNode<>(e);
} else {
parent.right = new TreeNode<>(e);
}
return true; //element inserted successfully
}
}
// Method to find depth of the tree
public int depth(TreeNode node){
if (node == null) return -1;
return 1+ Math.max(depth(node.left), depth(node.right));
}
// Method to find maximum value in the tree
public E max(TreeNode node){
if (node == null) return null;
E leftMax = max(node.left);
E rightMax = max(node.right);
E max = node.element;
if (leftMax != null && leftMax.compareTo(max)>0) max = leftMax;
if (rightMax != null && rightMax.compareTo(max)>0) max = rightMax;
return max;
}
// Method to find the sum of all nodes
public double treeSum(TreeNode node){
if (node == null)
return 0;
return (Double) node.element + treeSum(node.left)+ treeSum(node.right);
}
// Method to find the average of all nodes
public double treeAverage(TreeNode node, int[] count){
if (node == null) return 0;
count[0]++;
return (Double) node.element + treeAverage(node.left, count)+ treeAverage(node.right, count);
}
// Method to check if the tree is balanced
public boolean isBalanced(TreeNode node){
if (node == null) return true;
int leftDepth = depth(node.left);
int rightDepth = depth(node.right);
return (Math.abs(leftDepth - rightDepth)>1||!isBalanced(node.left)||!isBalanced(node.right));
}
public void build_from_node_lists(double[] pre, double[] in, int size){
}
}public class BinaryTreeTest {
public static void main(String[] args){
// Sample data arrays for pre-order and in-order traversal
double[] pre1={2,1,3};
double[] in1={1,2,3};
BinaryTree tree = new BinaryTree();
//tree.build_from_node_lists(pre1,in1);
int[] pre2={1,2,4,8,5,9,3,6,10,11,7,12} ; // pre order
int[] in2={8,4,2,9,5,1,10,6,11,3,7,12} ;
// in order
//return build_from_node_lists(pre2, in2,12);
int size3=57;
double[] pre3={0.185038,0.394342,0.291092,0.289448,0.101709,0.0611125,0.193208,0.13517,0.0404296,0.410769,0.0198583,0.432688,0.0674109,0.151873,0.14349,0.286818,0.071119,0.170354,0.0670367,0.44319,0.0682843,0.270964,0.369207,0.461848,0.427017,0.377756,0.0485934,0.0667142,0.108895,0.153658,0.00263633,0.222634,0.094924,0.195702,0.170448,0.364803,0.378102,0.440015,0.147172,0.0208332,0.340395,0.131731,0.275594,0.390628,0.317047,0.108642,0.393182,0.307225,0.159842,0.292226,0.126009,0.363135,0.183877,0.228193,0.449523,0.042972,0.462529};
double []in3={0.193208,0.13517,0.0611125,0.101709,0.289448,0.410769,0.0198583,0.151873,0.0674109,0.432688,0.0404296,0.286818,0.071119,0.170354,0.44319,0.0670367,0.14349,0.291092,0.0682843,0.270964,0.394342,0.377756,0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!