Question: JAVA My code: class Node { char ch; Node left, right; } class Decode { Node root; StringBuilder sb = new StringBuilder(); public Decode(String preOrder)

JAVA

My code:

class Node { char ch; Node left, right; } class Decode { Node root; StringBuilder sb = new StringBuilder(); public Decode(String preOrder) { root = buildTree(preOrder); } public Node buildTree(String preOrder) { return buildTree(preOrder, 0, preOrder.length()); } public Node buildTree(String preOrder, int start, int end) { if (start >= end) { return null; } Node node = new Node(); char ch = preOrder.charAt(start); node.ch = ch; if (ch == '*') { int i = start + 1; while (i < end && preOrder.charAt(i) == '*') { i++; } node.left = buildTree(preOrder, i, end); node.right = buildTree(preOrder, i + 1, end); } return node; } public void decode(String message) { Node node = root; for (char ch : message.toCharArray()) { if (node == null) { break; } node = ch == '0' ? node.left : node.right; if (node != null && node.left == null && node.right == null) { sb.append(node.ch); node = root; } } } public String getDecodedMessage() { return sb.toString(); } } public class DecodeMessage { public static void main(String[] args) { //Scanner scanner = new Scanner(System.in); // Read preOrder string from input // String preOrder = scanner.nextLine(); String preOrder = "*a**!*dc*rb"; // Read compressed message from input //String message = scanner.nextLine(); String message = "0111110010110101001111100100"; Decode decoder = new Decode(preOrder); decoder.decode(message); String decodedMessage = decoder.getDecodedMessage(); System.out.println("Decoded message: " + decodedMessage); } } 

this will output Decoded message: acda!d!!acda!a , but i need "abracadabra!".

Please edit code if necessary, provide input examplesm (maybe they are real problem) and also result to ensure that your edit works correctly.

One other person did edit even worse thats why i would ask for full result not only "oh see, i got".

Upvoating guaranted if work's as expected

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 Databases Questions!