import java.util.*;
public class Main
{
class CountingBST
<Key extends Comparable
<Key
>, Value
> {
// --------------------------------------------------------------------------
public class Node
{
Value value;
Node left, right;
int count;
public Node
(Key key, Value value,
int count
) {
this.key = key;
this.value = value;
this.left = this.right = null;
this.count = count;
}
}
private Node root;
// --------------------------------------------------------------------------
public void put
(Key key, Value val
) {
root = put(root, key, val);
}
// --------------------------------------------------------------------------
private Node put
(Node x,
Key key, Value val
) {
if (x == null)
return new Node(key, val, 1);
int cmp = key.compareTo(x.key);
if (cmp < 0)
x.left = put(x.left, key, val);
else if (cmp > 0)
x.right = put(x.right, key, val);
else if (cmp == 0)
x.value = val;
x.count = 1 + size(x.left) + size(x.right);
return x;
}
// --------------------------------------------------------------------------
public Value get
(Key key
) {
Node x = root;
while (x != null)
{
int cmp = key.compareTo(x.key);
if (cmp < 0)
x = x.left;
else if (cmp > 0)
x = x.right;
else if (cmp == 0)
return x.value;
}
return null;
}
// --------------------------------------------------------------------------
public int size()
{
return size(root);
}
// --------------------------------------------------------------------------
private int size(Node x)
{
if (x == null)
return 0;
return x.count;
}
// --------------------------------------------------------------------------
public Node min()
{
return min(root);
}
// --------------------------------------------------------------------------
private Node min(Node x)
{
if (x != null)
{
if (x.left == null)
{
return x;
}
return min(x.left);
}
return null;
}
// --------------------------------------------------------------------------
public Node max()
{
return max(root);
}
// --------------------------------------------------------------------------
private Node max(Node x)
{
if (x != null)
{
if (x.right == null)
{
return x;
}
return max(x.right);
}
return null;
}
// --------------------------------------------------------------------------
/* public void deleteMin()
{
root = deleteMin(root);
}*/
// --------------------------------------------------------------------------
private Node deleteMin(Node x)
{
if (x != null)
{
if (x.left == null)
return x.right;
x.left = deleteMin(x.left);
x.count = 1 + size(x.left) + size(x.right);
return x;
}
return null;
}
// --------------------------------------------------------------------------
public void delete
(Key key
) {
root = delete(root, key);
}
// --------------------------------------------------------------------------
private Node delete
(Node x,
Key key
) {
if (x == null)
return null;
int cmp = key.compareTo(x.key);
if (cmp < 0)
x.left = delete(x.left, key);
else if (cmp > 0)
x.right = delete(x.right, key);
else
{
if (x.right == null)
return x.left;
if (x.left == null)
return x.right;
Node t = x;
x = min(t.right);
x.right = deleteMin(t.right);
x.left = t.left;
}
x.count = size(x.left) + size(x.right) + 1;
return x;
}
// --------------------------------------------------------------------------
private void print(Node node, int level)
{
if (node != null)
{
for (int i = 0; i < level; i++)
{
}
System.
out.
printf("L%d: (", level
); System.
out.
printf("/%d", node.
count);
print(node.left, level + 1);
print(node.right, level + 1);
}
}
// --------------------------------------------------------------------------
public void print()
{
print(root, 0);
}
// also maxKey, minKey,
}
// parse text line into tokens, e.g. "put salary 120.22" to "put" "salary"
// "120.22"
public static Vector
<String
> parseTextToTokens
(String text
) {
Vector<String> tokens = new Vector<String>();
while (st.hasMoreTokens())
{
tokens.add(st.nextToken());
}
return tokens;
}
// helper function: converts string to double if possible or returns null
{
try
{
}
{
}
return x;
}
// print content of one node into human readable form
public static void printTreeNode
(CountingBST
<String, Double
>.
Node node
) {
if (node != null)
{
}
else
{
}
}
// main method with command line interpreter
public static void main
(String[] args
) {
Main obj = new Main();
CountingBST
<String, Double
> tree
= obj.
new CountingBST
<String, Double
>();
Scanner in
= new Scanner
(System.
in);
for (;;)
{
String inputLine
= in.
nextLine(); if (inputLine.isEmpty())
{
continue;
}
{
Vector<String> inputTokens = parseTextToTokens(inputLine);
if (inputTokens.size() > 0)
cmd = inputTokens.get(0);
if (inputTokens.size() > 1)
arg1 = inputTokens.get(1);
if (inputTokens.size() > 2)
arg2 = inputTokens.get(2);
}
if (cmd.equals("exit"))
{
break;
}
else if (cmd.equals("print"))
{
tree.print();
}
else if (cmd.equals("size"))
{
System.
out.
println(tree.
size()); }
else if (cmd.equals("put"))
{
String key
= (arg1
!= null && !arg1.
isEmpty()) ? arg1
: null; Double value
= (arg2
!= null) ? strToDouble
(arg2
) : null; if (key == null || value == null)
{
System.
out.
printf("*** ERROR: wrong arguments: <%s> <%s> <%s>\n", cmd, arg1, arg2
); continue;
}
tree.put(key, value);
}
else if (cmd.equals("get"))
{
String key
= (arg1
!= null && !arg1.
isEmpty()) ? arg1
: null; if (key == null)
{
System.
out.
printf("*** ERROR: wrong arguments: <%s> <%d>\n", cmd, arg1
); continue;
}
}
else if (cmd.equals("delete"))
{
String key
= (arg1
!= null && !arg1.
isEmpty()) ? arg1
: null; if (key == null)
{
System.
out.
printf("*** ERROR: wrong arguments: <%s> <%d>\n", cmd, arg1
); continue;
}
tree.delete(key);
}
else if (cmd.equals("min"))
{
printTreeNode(tree.min());
}
else if (cmd.equals("max"))
{
printTreeNode(tree.max());
}
}
in.close();
}
}