I'm trying to get printouts of the tree (i.e. st.keys()) in an old java program of a red black binary search tree using:
import java.util.Scanner;
AND
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
It doesn't seem to be working for reasons unknown to me. I tried debugging as a well. The program stops after the first for loop in both versions of main(). Any help in the right direction would be great.
Here are some images of the code with terminal output:
alternative version of main()
initial version of main()
Initial main() using
import edu.princeton.cs.algs4.StdIn:
public static void main(String[] args) {
RedBlackBST<String, Integer> st = new RedBlackBST<>();
for (int i = 0; !StdIn.isEmpty(); i++) {
String key = StdIn.readString();
st.put(key, i);
}
StdOut.println();
for (String s : st.keys())
StdOut.println(s + " " + st.get(s));
StdOut.println();
//StdOut.println(st.check());
st.delete("t");
System.out.println(st.keys());
}
Alternative main() using
import java.util.Scanner:
public static void main(String[] args) {
RedBlackBST<String, Integer> st = new RedBlackBST<>();
Scanner input = new Scanner(System.in);
for(int i=0; input.nextLine() != ""; i++){
String key = input.nextLine(); // Use in.nextLine() for line-by-line reading
st.put(key, i);
System.out.println(input);
}
input.close();
for (String s : st.keys()){
System.out.println(s + " " + st.get(s));
System.out.println();
//StdOut.println(st.check());
st.delete("t");
System.out.println(st.keys());
}
}
Terminal Output (including user input):
/usr/lib/jvm/java-11-openjdk-amd64/bin/java -Dfile.encoding=UTF-8 @/tmp/cp_do3cqf4bgpqm37l69q1dfdnql.argfile RedBlackBST
t
a
l
l
t
r
e
e
s
^Z
[1]+ Stopped /usr/lib/jvm/java-11-openjdk-amd64/bin/java -Dfile.encoding=UTF-8 @/tmp/cp_do3cqf4bgpqm37l69q1dfdnql.argfile RedBlackBST
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…