import java.io.*;

public class MainBST
{
    public static void main (String[] args) throws IOException
    {
        DrzewoBST<Para<String,Double>> drzewo = new DrzewoBST<Para<String,Double>>();
        BufferedReader we = new BufferedReader(new InputStreamReader(System.in));
        System.err.print("> "); System.err.flush();
        for (String ln=we.readLine(); ln!=null; ln=we.readLine())
        {
            String[] w = ln.trim().split("\\p{Blank}+");
            if (w[0].length()==0) continue;
            if (w.length==1&&(w[0].equals("q")||w[0].equals("quit")))
            {
                System.err.println("bye");
                break;
            }
            if (w.length==1&&(w[0].equals("?")||w[0].equals("h")||w[0].equals("help")))
            {
                System.err.println("komendy:");
                System.err.println("  i/ins/insert key value - wstawienie pary klucz-wartość");
                System.err.println("  d/del/delete key - usunięcie pary klucz-wartość z BST");
                System.err.println("  s/search key - wyszukanie pary klucz-wartość w BST");
                System.err.println("  p/print - wydrukowanie BST w porządku inorder");
                System.err.println("  min - wyszukanie pary z najmniejszym kluczem w BST");
                System.err.println("  max - wyszukanie pary z największym kluczem w BST");
                System.err.println("  c/count - policzenie par w BST");
                System.err.println("  ?/h/help - pomoc");
                System.err.println("  q/quit - wyjście z programu");
                System.err.println("gdzie:");
                System.err.println("  key = identyfikator (klucz)");
                System.err.println("  value = liczba rzeczywista (wartość)");
            }
            else if (w.length==1&&(w[0].equals("min")))
            {
                Para<String,Double> m = drzewo.szukajMinimum();
                if (m!=null) System.out.println(m);
                else System.out.println("drzewo jest puste");
            }
            else if (w.length==1&&(w[0].equals("max")))
            {
                Para<String,Double> m = drzewo.szukajMaksimum();
                if (m!=null) System.out.println(m);
                else System.out.println("drzewo jest puste");
            }
            else if (w.length==1&&(w[0].equals("c")||w[0].equals("count")))
                System.out.println(drzewo.rozmiar());
            else if (w.length==1&&(w[0].equals("p")||w[0].equals("print")))
                System.out.println(drzewo);
            else if (w.length==3&&(w[0].equals("i")||w[0].equals("ins")||w[0].equals("insert")))
                try
                {
                    if (!w[1].matches("\\p{Alpha}\\p{Alnum}*")) throw new IllegalArgumentException();
                    Double l = new Double(w[2]);
                    Para<String,Double> p = new Para<String,Double>(w[1],l);
                    Para<String,Double> q = drzewo.wstaw(p);
                    if (q!=p) System.out.println("poprzednia wartość: "+q);
                    else System.out.println(":)");
                }
                catch (NumberFormatException ex)
                    { System.err.println("wartość nie jest poprawną liczbą rzeczywistą"); }
                catch (IllegalArgumentException ex)
                    { System.err.println("klucz nie jest poprawnym identyfikatorem"); }
            else if (w.length==2&&(w[0].equals("d")||w[0].equals("del")||w[0].equals("delete")))
                try
                {
                    if (!w[1].matches("\\p{Alpha}\\p{Alnum}*")) throw new IllegalArgumentException();
                    Para<String,Double> p = new Para<String,Double>(w[1],new Double(0));
                    Para<String,Double> q = drzewo.usuń(p);
                    if (q==null) System.out.println("nie znaleziono klucza "+p.podajKlucz());
                    else System.out.println(":)");
                }
                catch (IllegalArgumentException ex)
                    { System.err.println("klucz nie jest poprawnym identyfikatorem"); }
            else if (w.length==2&&(w[0].equals("s")||w[0].equals("search")))
                try
                {
                    if (!w[1].matches("\\p{Alpha}\\p{Alnum}*")) throw new IllegalArgumentException();
                    Para<String,Double> p = new Para<String,Double>(w[1],new Double(0));
                    Para<String,Double> q = drzewo.szukaj(p);
                    if (q==null) System.out.println("nie znaleziono klucza "+p.podajKlucz());
                    else System.out.println(q);
                }
                catch (IllegalArgumentException ex)
                    { System.err.println("klucz nie jest poprawnym identyfikatorem"); }
            else System.err.println("nieznana komenda (wpisz 'help', 'h' albo '?')");
            System.err.print("> "); System.err.flush();
        }
    }
}
