Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Looking for some java help with my program its a binary search tree not sure how

ID: 3606964 • Letter: L

Question

Looking for some java help with my program its a binary search tree not sure how extactly how to finsh my program off I have bolded the code that is causing issue

MyBSt.java

class MyBST {

/* Class containing left and right child of current node and key value*/

class Node {

int key;

Node left, right;

public Node(int item) {

key = item;

left = right = null;

}

}

// Root of BST

Node root;

MyBST() {

root = null;

}

void insert(int key) {

root = insertRec(root, key);

}

Node insertRec(Node root, int key) {

if (root == null) {

root = new Node(key);

return root;

}

if (key < root.key)

root.left = insertRec(root.left, key);

else if (key > root.key)

root.right = insertRec(root.right, key);

return root;

}

void inorder() {

inorderRec(root);

}

void inorderRec(Node root) {

if (root != null) {

inorderRec(root.left);

System.out.print(root.key + " ");

inorderRec(root.right);

}

}

void calculatedeviation(int mu){

if (root != null) {

inorderRec(root.left);

System.out.println(root.key + " - " +(root.key - mu)*(root.key - mu));

inorderRec(root.right);

}

}

public static void main(String[] args) {

MyBST tree = new MyBST();

int sum = 0;

int count = 0;

int avg = 0;

for(int i = 100; i<= 12800; i*2){

tree.insert(i);

sum += i;

count++;

}

System.out.println("Inorder Tree Travesal:");

tree.inorder();

avg = sum/count;

System.out.println("Average Node Value: "+ avg);

System.out.println("Standard Deviation:");

tree.calculatedeviation(avg);

}

}

Explanation / Answer

You program is almost right but in the bold content you wrote i*2, in java you need to assign the value to the variable after any mathematical operation, I had modified your code and now you are able to ge the output.

For your reference here I am adding the output also.

MyBST.java// instead of MyBSt.java the class name and file name should be same

class MyBST {

/* Class containing left and right child of current node and key value */

class Node {

int key;

Node left, right;

public Node(int item) {

key = item;

left = right = null;

}

}

// Root of BST

Node root;

MyBST() {

root = null;

}

void insert(int key) {

root = insertRec(root, key);

}

Node insertRec(Node root, int key) {

if (root == null) {

root = new Node(key);

return root;

}

if (key < root.key)

root.left = insertRec(root.left, key);

else if (key > root.key)

root.right = insertRec(root.right, key);

return root;

}

void inorder() {

inorderRec(root);

}

void inorderRec(Node root) {

if (root != null) {

inorderRec(root.left);

System.out.print(root.key + " ");

inorderRec(root.right);

}

}

void calculatedeviation(int mu) {

if (root != null) {

inorderRec(root.left);

System.out.println(root.key + " - " + (root.key - mu)

* (root.key - mu));

inorderRec(root.right);

}

}

public static void main(String[] args) {

MyBST tree = new MyBST();

int sum = 0;

int count = 0;

int avg = 0;

for (int i = 100; i <= 12800; i = i * 2) //instead of i * 2 you need to gave i = i * 2 so that the for loop will be //repeated based on the i * 2 condition

{

tree.insert(i);

sum += i;

count++;

}

System.out.println("Inorder Tree Travesal:");

tree.inorder();

avg = sum / count;

System.out.println("Average Node Value: " + avg);

System.out.println("Standard Deviation:");

tree.calculatedeviation(avg);

}

}

aftet changing the condition your program is compiled and executed succesfully. Below is output for your MyBST.java

output:

Inorder Tree Travesal:
100 200 400 800 1600 3200 6400 12800 Average Node Value: 3187
Standard Deviation:
100 - 9529569
200 400 800 1600 3200 6400 12800

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote