I have a code that is supposed to output whether a mobile tree is balanced or unbalanced. The input format is id w id_l d_l id_r d_r.
import java.util.*;
import java.io.*;
public class Solution {
static class Node {
int id;
int weight;
Node leftChild;
Node rightChild;
int leftDistance = -1;
int rightDistance = -1;
public Node(int id, int weight) {
this.id = id;
this.weight = weight;
this.leftDistance = leftDistance;
this.rightDistance = rightDistance;
}
}
// Method to read input and create tree
public static Node createTree() {
Scanner sc = new Scanner(System.in);
TreeMap<Integer, Node> map = new TreeMap<>();
//Map<Node, Node> parentMap = new HashMap<>();
// inputs
int id, w, id_l, d_l, id_r, d_r;
Node node, leftNode, rightNode;
// check all nodes
while (sc.hasNextInt()) {
id = sc.nextInt();
w = sc.nextInt();
node = map.get(id);
if (node == null) {
node = new Node(id, w);
map.put(id, node);
} else {
node.weight = w;
}
id_l = sc.nextInt();
d_l = sc.nextInt();
id_r = sc.nextInt();
d_r = sc.nextInt();
// leftChild input doesn't equal -1
if (id_l != -1) {
leftNode = map.get(id_l);
if (leftNode == null) {
leftNode = new Node(id_l, w);
map.put(id_l, leftNode);
}
node.leftChild = leftNode;
node.leftDistance = d_l;
node.weight = w;
//parentMap.put(leftNode, node);
}
else {
node.leftChild = null;
node.leftDistance = -1;
}
// rightChild input doesn't = -1
if (id_r != -1) {
rightNode = map.get(id_r);
if (rightNode == null) {
rightNode = new Node(id_r, w);
map.put(id_r, rightNode);
}
node.rightChild = rightNode;
node.rightDistance = d_r;
node.weight = w;
//parentMap.put(rightNode, node);
}
else {
node.rightChild = null;
node.rightDistance = -1;
}
}
for (Map.Entry<Integer, Node> entry : map.entrySet()) {
System.out.println("Node " + entry.getKey() + ": " + entry.getValue().leftDistance + " " + entry.getValue().rightDistance + " " + entry.getValue().weight);
}
// return root of tree
return map.firstEntry().getValue();
}
// Recursive method to determine if tree is balanced
public static int isBalanced(Node node) {
if (node == null) {
return 0;
}
int leftWeight = isBalanced(node.leftChild);
int rightWeight = isBalanced(node.rightChild);
int leftTorque = node.leftDistance * leftWeight ;
int rightTorque = node.rightDistance * rightWeight;
//int leftTorque = (node.leftChild != null) ? leftWeight * node.leftDistance : -1;
//int rightTorque = (node.rightChild != null) ? rightWeight * node.rightDistance : -1;
System.out.printf("Node %d: Left distance = %d, Right distance = %d\n", node.id, node.leftDistance, node.rightDistance);
System.out.printf("Node %d: Left weight = %d, Right weight = %d\n", node.id, leftWeight, rightWeight);
System.out.printf("Node %d: Left torque = %d, Right torque = %d\n", node.id, leftTorque, rightTorque);
//if (leftTorque != rightTorque)
if (Math.abs(leftTorque - rightTorque) > 1) {
return -1;
}
int nodeWeight = Math.max(leftWeight, rightWeight) + node.weight;
System.out.printf("Node %d weight = %d\n", node.id, nodeWeight);
return nodeWeight;
}
public static void main(String[] args) {
Node root = createTree();
Map<Node, Node> parentMap = new HashMap<>();
int result = isBalanced(root);
if (result == -1) {
System.out.println("Unbalanced");
} else {
System.out.println("Balanced");
}
}
}
It's not working correctly, for example the input given below is supposed to output unbalanced meaning isBalanced needs to return -1. My code keeps saying that it is balanced with a weight of 3. I wanted ask why my code is incorrect and how to fix it to work correctly. The constraints is that I have to use a TreeMap or hashMap to consctruct the tree and use a recursive method that takes one pass through the tree to determine if it is balanced.
9 3 -1 -1 -1 -1
12 14 23 15 94 5
23 1 -1 -1 -1 -1
35 1 56 10 9 20
41 5 12 10 35 20
56 2 77 1 88 1
77 2 -1 -1 -1 -1
88 2 -1 -1 -1 -1
94 3 -1 -1 -1 -1
Step by stepSolved in 2 steps
- I need help with this Java problem to output as it's explained in the image below: public class BST { private Node root; public void insert(int data) { root = insertRec(root, data); } private Node insertRec(Node root, int data) { if (root == null) { root = new Node(data); return root; } if (data < root.data) { root.left = insertRec(root.left, data); } else if (data > root.data) { root.right = insertRec(root.right, data); } return root; } public int getHeight() { return getHeightRec(root); } private int getHeightRec(Node root) { if (root == null) { return 0; } else { int leftHeight = getHeightRec(root.left); int rightHeight = getHeightRec(root.right); return Math.max(leftHeight, rightHeight) + 1; } } public int search(int data) { return searchRec(root, data, 0); }…arrow_forwardimport java.util.*;import java.io.*; public class HuffmanCode { private Queue<HuffmanNode> queue; private HuffmanNode overallRoot; public HuffmanCode(int[] frequencies) { queue = new PriorityQueue<HuffmanNode>(); for (int i = 0; i < frequencies.length; i++) { if (frequencies[i] > 0) { HuffmanNode node = new HuffmanNode(frequencies[i]); node.ascii = i; queue.add(node); } } overallRoot = buildTree(); } public HuffmanCode(Scanner input) { overallRoot = new HuffmanNode(-1); while (input.hasNext()) { int asciiValue = Integer.parseInt(input.nextLine()); String code = input.nextLine(); overallRoot =…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education