Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

Unsuccessful Java Print Out of Red Black Binary Search Tree with Imports algs4.StdIn and util.Scanner in Visual Studio

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

i get the proper output with ctrl+d, i was using ctrl+z to exit


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...