Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Java by registered user SKYDOS ( 17 years ago )
import java.io.*;
public class Main {
    public static class node {
        private int key, count;
        node left, right;
        node (int k, int c) {
            key=k;
            count=c;
            left=right=null;
        }
    }

    static node root;
    static StreamTokenizer in;
    static PrintWriter out;

    public static void insert (int num, node r) {
        for (;;) {
            if (r.key==num) { r.count++; break; }
            else if (r.key>num && r.left==null) {
                r.left = new node (num, 1); break;
            } else if (r.key<num && r.right==null) {
                r.right = new node (num, 1); break;
            }

            if (r.key>num) {
                r = r.left;
            } else r = r.right;
        }
    }

    public static void walkTree (node root) {
        if (root!=null) {
            walkTree (root.left);
            out.println (root.key+" "+root.count);
            walkTree (root.right);
        }
    }

    public static void main (String[] args) throws IOException {
        in = new StreamTokenizer (new BufferedReader(new InputStreamReader (System.in, "ISO-8859-1")));
        out = new PrintWriter (new OutputStreamWriter (System.out, "ISO-8859-1"));

        for (int i=0;;i++) {
            in.nextToken();
            if ((int)in.nval==0) break;

            if (i==0) {
                root = new node ((int)in.nval, 1);
                continue;
            }
            insert ((int)in.nval, root);
        }

        walkTree (root);

        out.close();
    }
}

 

Revise this Paste

Your Name: Code Language: