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

import heapq class Node: def __init__(self, left=None, right=None, val=0): self.

ID: 3592337 • Letter: I

Question

import heapq


class Node:
def __init__(self, left=None, right=None, val=0):
self.val = val
self.left = left
self.right = right
self.st = ''

t = int(input())
while t > 0:
t -= 1
ls = list(input())
arr = list(map(int, input().split()))
brr = []
n = len(ls)
for i in range(n):
brr.append([arr[i], Node(None, None, i)])

heapq.heapify(brr)
while len(brr) >= 2:
y = heapq.heappop(brr)
x = heapq.heappop(brr)
z = Node(None, None, x[0] + y[0])
if x[0] > y[0]:
z.right = x[1]
z.left = y[1]
else:
z.left = x[1]
z.right = y[1]
heapq.heappush(brr, [z.val, z])
def assign(root, st):
if root is None:
return
root.st = st
if root.left is None and root.right is None:
del root
print(st, end = " ")
return
assign(root.left, st + '0')
assign(root.right, st + '1')

assign(brr[0][1], '')
print()

how would i adjust the above code to read input from terminal and print the output to an output.txt file

Explanation / Answer

If you want to read input from terminal you have to compile your program fist

C:> javac name.java //write your program name at name.java

Now what are the arguments you want to pass write at the command prompt

The arguments which is passed at command prompt will received by main method

Class Name

{

public static void main(String args[])

{

val=args[0];

left=args[1];

right=args[2];

self.val=Integer.parseInt(val);

self.left=Integer.parseInt(left);

self.right=Integet.parseInt(right);

self.st=";

// Now write your code up-to if root.left is none and root.right is none del root

File myFile = new File("Data.txt"); //you have to create this Data.txt file in your project folder

if (myFile.exists())

{

System.out.println(st, end=" ");

return;

assign(root.left, st + '0');

assign(root.right, st + '1');

assign(brr[0][1], ");

System.out.println();

}

else

{

State.out.println("File does not exist"); // this message will appear if you haven't create txt file in your project folder

}

}