import java.io.*;
import java.util.*;
class BST {
public static void main(String[] sbgs) throws IOException {
st.nextToken();
c1 = (int) st.nval;
for(int i = 0; i < c1; i++) {
BST bst = new BST();
sbl.append("test " + (i+1) + "\n");
st.nextToken();
c2 = (int) st.nval;
for(int j = 0; j < c2; j++) {
st.nextToken();
String query = st.sval;
st.nextToken();
int q = (int) st.nval;
if("I".equals(query)) bst.put(q);
else if("D".equals(query)) bst.delete(q);
else if("S".equals(query))
if(bst.contains(q)) sbl.append(q + "\n");
else sbl.append("-" + "\n");
else if("X".equals(query))
if(q == 0) sbl.append(bst.min() + "\n");
else sbl.append(bst.max() + "\n");
else if("N".equals(query))
if((k = bst.succ(q)) != -1) sbl.append(k + "\n");
else sbl.append("-" + "\n");
else if("P".equals(query))
if((k = bst.pred(q)) != -1) sbl.append(k + "\n");
else sbl.append("-" + "\n");
else if("R".equals(query)) {
if(q == 0) {
StringBuilder temp = bst.in();
sbl.append(temp.deleteCharAt(temp.length() - 1) + "\n");
}
else if(q == 1) {
StringBuilder temp = bst.pre();
sbl.append(temp.deleteCharAt(temp.length() - 1) + "\n");
}
else if(q == 2) {
StringBuilder temp = bst.post();
sbl.append(temp.deleteCharAt(temp.length() - 1) + "\n");
}
}
}
}
out.print(sbl.deleteCharAt(sbl.length() - 1));
out.flush();
}
static int k;
static int c1, c2;
static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static StringBuilder sbl = new StringBuilder();
private Node root;
private class Node {
private int key;
private int val = -1;
private Node left, right;
private int N;
public Node(int key, int val, int N) {
this.key = key;
this.val = val;
this.N = N;
}
}
public boolean isEmpty() { return size() == 0; }
public int size() { return size(root); }
private int size(Node x) {
if (x == null) return 0;
return x.N;
}
public boolean contains(int key) { return get(key) != -1; }
public int get(int key) { return get(root, key); }
private int get(Node x, int key) {
if (x == null) return -1;
if (key < x.key) return get(x.left, key);
else if (key > x.key) return get(x.right, key);
else return x.val;
}
public void put(int key) { root = put(root, key, 1); }
private Node put(Node x, int key, int val) {
if (x == null) return new Node(key, val, 1);
if (key < x.key) x.left = put(x.left, key, val);
else if (key > x.key) x.right = put(x.right, key, val);
else x.val = val;
x.N = 1 + size(x.left) + size(x.right);
return x;
}
public void deleteMin() { root = deleteMin(root); }
private Node deleteMin(Node x) {
if (x.left == null) return x.right;
x.left = deleteMin(x.left);
x.N = size(x.left) + size(x.right) + 1;
return x;
}
public void deleteMax() { root = deleteMax(root); }
private Node deleteMax(Node x) {
if (x.right == null) return x.left;
x.right = deleteMax(x.right);
x.N = size(x.left) + size(x.right) + 1;
return x;
}
public void delete(int key) { root = delete(root, key); }
private Node delete(Node x, int key) {
if (x == null) return null;
if (key < x.key) x.left = delete(x.left, key);
else if (key > x.key) 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 = pred(x);
x.left = deleteMax(t.left);
x.right = t.right;
}
x.N = size(x.left) + size(x.right) + 1;
return x;
}
public int min() { return min(root).key; }
private Node min(Node x) {
if (x.left == null) return x;
return min(x.left);
}
public int max() { return max(root).key; }
private Node max(Node x) {
if (x.right == null) return x;
return max(x.right);
}
public int succ(int key) {
if(!contains(key)) return -1;
return ceiling(key + 1);
}
private Node pred(Node t) {
Node x = floor(root, t.key - 1);
if (x == null) return null;
return x;
}
public int pred(int key) {
if(!contains(key)) return -1;
return floor(key - 1);
}
public int floor(int key) {
Node x = floor(root, key);
if (x == null) return -1;
return x.key;
}
private Node floor(Node x, int key) {
if (x == null) return null;
int cmp = key - x.key;
if (cmp == 0) return x;
if (cmp < 0) return floor(x.left, key);
Node t = floor(x.right, key);
if (t != null) return t;
return x;
}
public int ceiling(int key) {
Node x = ceiling(root, key);
if (x == null) return -1;
return x.key;
}
private Node ceiling(Node x, int key) {
if (x == null) return null;
int cmp = key - x.key;
if (cmp == 0) return x;
if (cmp < 0) {
Node t = ceiling(x.left, key);
if (t != null) return t;
return x;
}
return ceiling(x.right, key);
}
public StringBuilder pre() {
StringBuilder sb = new StringBuilder();
pre(root, sb);
return sb;
}
public StringBuilder post() {
StringBuilder sb = new StringBuilder();
post(root, sb);
return sb;
}
public StringBuilder in() {
StringBuilder sb = new StringBuilder();
in(root, sb);
return sb;
}
public void pre(Node x, StringBuilder sb) {
if(x == null) return;
sb.append(x.key + " ");
pre(x.left, sb);
pre(x.right, sb);
}
public void post(Node x,StringBuilder sb) {
if(x == null) return;
post(x.left, sb);
post(x.right, sb);
sb.append(x.key + " ");
}
public void in(Node x, StringBuilder sb) {
if(x == null) return;
in(x.left, sb);
sb.append(x.key + " ");
in(x.right, sb);
}
}
Wyszukiwarka
Podobne podstrony:
9 01 07 drzewa binarne08 Drzewa binarneDrzewa binarneDrzewa binarne definicjeLekcja drzewa binarnych poszukiwańOptymalizacja serwisow internetowych Tajniki szybkosci, skutecznosci i wyszukiwarekDarmowa wyszukiwarka styl TIGERDrzewaLOGAiSD w4 sortowanie209 Drzewa wyższych rzędówwyszukiwanieGotowa wyszukiwarka do wstawienia na chomika(1)(1)utk uklad binarnyDarmowa wyszukiwarka Chomikowa Avatar 2automaty 4 drzewa wyprowadzenwięcej podobnych podstron