/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.security.Key;
/**
* Created by green on 26.11.15.
*/
class BST
<Key extends Comparable
<Key
>, Value
> {
private class Node {
Value value;
Node left, right, father;
public Node
(Key key, Value value
) { this.key = key;
this.value = value;
this.left = this.right = null;
this.father = null;
}
public Node
(Key key, Value value, Node father
) { this.key = key;
this.value = value;
this.left = this.right = null;
this.father = father;
}
}
private Node root;
private Node add
(Node node,
Key key, Value value, Node father
){ if (node == null){
Node newnode = new Node(key,value, father);
return newnode;
}
int compareResult = key.compareTo(node.key);
if (compareResult > 0) node.right = add(node.right, key, value, node);
else if(compareResult < 0) node.left = add(node.left, key, value, node);
else{
node.value = value;
}
return node;
}
public void add
(Key key, Value value
) { root = add(root, key, value, root);
}
private Node delete
(Node node,
Key key
){ if(node == null) return null;
int compareResult = key.compareTo(node.key);
if(compareResult > 0){
node.right = delete(node.right, key);
}else if(compareResult < 0){
node.left = delete(node.left, key);
}else{
if(node.right == null && node.left == null){
return node = null;
}else if(node.right == null){
return node = node.left;
}else if(node.left == null){
return node = node.right;
}else{
if(node.right.left == null){
node.right.left = node.left;
node.right.father = node.father;
return node = node.right;
}else{
Node res = min(node.right);
node.key = res.key;
node.value = res.value;
delete(node.right, node.key);
return node;
}
}
}
return node;
}
public void delete
(Key key
) { root = delete(root, key);
}
public Value minKey(){
return min(root).value;
}
public Value maxKey(){
return max(root).value;
}
private Node min(Node node){
if(node.left == null) return node;
return min(node.left);
}
private Node max(Node node){
if(node.right == null) return node;
return min(node.right);
}
private Value get
(Node node,
Key key
){ if(node == null) return null;
int compareResult = key.compareTo(node.key);
if(compareResult == 0){
return node.value;
}else if(compareResult > 0){
return get(node.right, key);
}else{
return get(node.left, key);
}
}
public Value get
(Key key
) { return get(root, key);
}
private void print(Node node, int level) {
if (node != null) {
print(node.right,level+1);
for (int i=0;i<level;i++) {
}
System.
out.
println(node.
key + "->" + node.
value); print(node.left,level+1);
}
}
public void print() {
print(root,0);
}
}
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
BST
<Integer, String
> tree
= new BST
<>(); tree.add(10, "Test1");
tree.add(12, "Test2");
tree.add(1, "Test3");
tree.add(5, "Test4");
tree.add(6, "Test5");
tree.add(3, "Test6");
tree.print();
tree.delete(5);
tree.print();
System.
out.
println(tree.
get(1)); System.
out.
println(tree.
minKey()); System.
out.
println(tree.
maxKey()); System.
out.
println(tree.
get(3)); }
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CmltcG9ydCBqYXZhLnNlY3VyaXR5LktleTsKCi8qKgogKiBDcmVhdGVkIGJ5IGdyZWVuIG9uIDI2LjExLjE1LgogKi8KY2xhc3MgQlNUIDxLZXkgZXh0ZW5kcyBDb21wYXJhYmxlPEtleT4sIFZhbHVlPiB7CgogICAgcHJpdmF0ZSBjbGFzcyBOb2RlIHsKICAgICAgICBLZXkga2V5OwogICAgICAgIFZhbHVlIHZhbHVlOwogICAgICAgIE5vZGUgbGVmdCwgcmlnaHQsIGZhdGhlcjsKICAgICAgICBwdWJsaWMgTm9kZSAoS2V5IGtleSwgVmFsdWUgdmFsdWUpIHsKICAgICAgICAgICAgdGhpcy5rZXkgPSBrZXk7CiAgICAgICAgICAgIHRoaXMudmFsdWUgPSB2YWx1ZTsKICAgICAgICAgICAgdGhpcy5sZWZ0ID0gdGhpcy5yaWdodCA9IG51bGw7CiAgICAgICAgICAgIHRoaXMuZmF0aGVyID0gbnVsbDsKICAgICAgICB9CiAgICAgICAgcHVibGljIE5vZGUgKEtleSBrZXksIFZhbHVlIHZhbHVlLCBOb2RlIGZhdGhlcikgewogICAgICAgICAgICB0aGlzLmtleSA9IGtleTsKICAgICAgICAgICAgdGhpcy52YWx1ZSA9IHZhbHVlOwogICAgICAgICAgICB0aGlzLmxlZnQgPSB0aGlzLnJpZ2h0ID0gbnVsbDsKICAgICAgICAgICAgdGhpcy5mYXRoZXIgPSBmYXRoZXI7CiAgICAgICAgfQogICAgfQoKICAgIHByaXZhdGUgTm9kZSByb290OwoKICAgIHByaXZhdGUgTm9kZSBhZGQgKE5vZGUgbm9kZSxLZXkga2V5LCBWYWx1ZSB2YWx1ZSwgTm9kZSBmYXRoZXIpewogICAgICAgIGlmIChub2RlID09IG51bGwpewogICAgICAgICAgICBOb2RlIG5ld25vZGUgPSBuZXcgTm9kZShrZXksdmFsdWUsIGZhdGhlcik7CiAgICAgICAgICAgIHJldHVybiBuZXdub2RlOwogICAgICAgIH0KICAgICAgICBpbnQgY29tcGFyZVJlc3VsdCA9IGtleS5jb21wYXJlVG8obm9kZS5rZXkpOwogICAgICAgIGlmIChjb21wYXJlUmVzdWx0ID4gMCkgbm9kZS5yaWdodCA9IGFkZChub2RlLnJpZ2h0LCBrZXksIHZhbHVlLCBub2RlKTsKICAgICAgICBlbHNlIGlmKGNvbXBhcmVSZXN1bHQgPCAwKSBub2RlLmxlZnQgPSBhZGQobm9kZS5sZWZ0LCBrZXksIHZhbHVlLCBub2RlKTsKICAgICAgICBlbHNlewogICAgICAgICAgICBub2RlLnZhbHVlID0gdmFsdWU7CiAgICAgICAgfQogICAgICAgIHJldHVybiBub2RlOwogICAgfQoKICAgIHB1YmxpYyB2b2lkIGFkZChLZXkga2V5LCBWYWx1ZSB2YWx1ZSkgewogICAgICAgIHJvb3QgPSBhZGQocm9vdCwga2V5LCB2YWx1ZSwgcm9vdCk7CiAgICB9CgogICAgcHJpdmF0ZSBOb2RlIGRlbGV0ZShOb2RlIG5vZGUsIEtleSBrZXkpewogICAgICAgIGlmKG5vZGUgPT0gbnVsbCkgcmV0dXJuIG51bGw7CiAgICAgICAgaW50IGNvbXBhcmVSZXN1bHQgPSBrZXkuY29tcGFyZVRvKG5vZGUua2V5KTsKICAgICAgICBpZihjb21wYXJlUmVzdWx0ID4gMCl7CiAgICAgICAgICAgIG5vZGUucmlnaHQgPSBkZWxldGUobm9kZS5yaWdodCwga2V5KTsKICAgICAgICB9ZWxzZSBpZihjb21wYXJlUmVzdWx0IDwgMCl7CiAgICAgICAgICAgIG5vZGUubGVmdCA9IGRlbGV0ZShub2RlLmxlZnQsIGtleSk7CiAgICAgICAgfWVsc2V7CiAgICAgICAgICAgIGlmKG5vZGUucmlnaHQgPT0gbnVsbCAmJiBub2RlLmxlZnQgPT0gbnVsbCl7CiAgICAgICAgICAgICAgICByZXR1cm4gbm9kZSA9IG51bGw7CiAgICAgICAgICAgIH1lbHNlIGlmKG5vZGUucmlnaHQgPT0gbnVsbCl7CiAgICAgICAgICAgICAgICByZXR1cm4gbm9kZSA9IG5vZGUubGVmdDsKICAgICAgICAgICAgfWVsc2UgaWYobm9kZS5sZWZ0ID09IG51bGwpewogICAgICAgICAgICAgICAgcmV0dXJuIG5vZGUgPSBub2RlLnJpZ2h0OwogICAgICAgICAgICB9ZWxzZXsKICAgICAgICAgICAgICAgIGlmKG5vZGUucmlnaHQubGVmdCA9PSBudWxsKXsKICAgICAgICAgICAgICAgICAgICBub2RlLnJpZ2h0LmxlZnQgPSBub2RlLmxlZnQ7CiAgICAgICAgICAgICAgICAgICAgbm9kZS5yaWdodC5mYXRoZXIgPSBub2RlLmZhdGhlcjsKICAgICAgICAgICAgICAgICAgICByZXR1cm4gbm9kZSA9IG5vZGUucmlnaHQ7CiAgICAgICAgICAgICAgICB9ZWxzZXsKICAgICAgICAgICAgICAgICAgICBOb2RlIHJlcyA9IG1pbihub2RlLnJpZ2h0KTsKICAgICAgICAgICAgICAgICAgICBub2RlLmtleSA9IHJlcy5rZXk7CiAgICAgICAgICAgICAgICAgICAgbm9kZS52YWx1ZSA9IHJlcy52YWx1ZTsKICAgICAgICAgICAgICAgICAgICBkZWxldGUobm9kZS5yaWdodCwgbm9kZS5rZXkpOwogICAgICAgICAgICAgICAgICAgIHJldHVybiBub2RlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBub2RlOwogICAgfQoKICAgIHB1YmxpYyB2b2lkIGRlbGV0ZShLZXkga2V5KSB7CiAgICAgICAgcm9vdCA9IGRlbGV0ZShyb290LCBrZXkpOwogICAgfQoKICAgIHB1YmxpYyBWYWx1ZSBtaW5LZXkoKXsKICAgICAgICByZXR1cm4gbWluKHJvb3QpLnZhbHVlOwogICAgfQoKICAgIHB1YmxpYyBWYWx1ZSBtYXhLZXkoKXsKICAgICAgICByZXR1cm4gbWF4KHJvb3QpLnZhbHVlOwogICAgfQoKICAgIHByaXZhdGUgTm9kZSBtaW4oTm9kZSBub2RlKXsKICAgICAgICBpZihub2RlLmxlZnQgPT0gbnVsbCkgcmV0dXJuIG5vZGU7CiAgICAgICAgcmV0dXJuIG1pbihub2RlLmxlZnQpOwogICAgfQoKICAgIHByaXZhdGUgTm9kZSBtYXgoTm9kZSBub2RlKXsKICAgICAgICBpZihub2RlLnJpZ2h0ID09IG51bGwpIHJldHVybiBub2RlOwogICAgICAgIHJldHVybiBtaW4obm9kZS5yaWdodCk7CiAgICB9CgogICAgcHJpdmF0ZSBWYWx1ZSBnZXQoTm9kZSBub2RlLCBLZXkga2V5KXsKICAgICAgICBpZihub2RlID09IG51bGwpIHJldHVybiBudWxsOwogICAgICAgIGludCBjb21wYXJlUmVzdWx0ID0ga2V5LmNvbXBhcmVUbyhub2RlLmtleSk7CiAgICAgICAgaWYoY29tcGFyZVJlc3VsdCA9PSAwKXsKICAgICAgICAgICAgcmV0dXJuIG5vZGUudmFsdWU7CiAgICAgICAgfWVsc2UgaWYoY29tcGFyZVJlc3VsdCA+IDApewogICAgICAgICAgICByZXR1cm4gZ2V0KG5vZGUucmlnaHQsIGtleSk7CiAgICAgICAgfWVsc2V7CiAgICAgICAgICAgIHJldHVybiBnZXQobm9kZS5sZWZ0LCBrZXkpOwogICAgICAgIH0KICAgIH0KCiAgICBwdWJsaWMgVmFsdWUgZ2V0KEtleSBrZXkpIHsKICAgICAgICByZXR1cm4gZ2V0KHJvb3QsIGtleSk7CiAgICB9CgogICAgcHJpdmF0ZSB2b2lkIHByaW50KE5vZGUgbm9kZSwgaW50IGxldmVsKSB7CiAgICAgICAgaWYgKG5vZGUgIT0gbnVsbCkgewogICAgICAgICAgICBwcmludChub2RlLnJpZ2h0LGxldmVsKzEpOwogICAgICAgICAgICBmb3IgKGludCBpPTA7aTxsZXZlbDtpKyspIHsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnQoIlx0Iik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKG5vZGUua2V5ICsgIi0+IiArIG5vZGUudmFsdWUpOwogICAgICAgICAgICBwcmludChub2RlLmxlZnQsbGV2ZWwrMSk7CiAgICAgICAgfQogICAgfQoKICAgIHB1YmxpYyB2b2lkIHByaW50KCkgewogICAgICAgIHByaW50KHJvb3QsMCk7CiAgICB9Cgp9CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluIChTdHJpbmdbXSBhcmdzKSB0aHJvd3MgamF2YS5sYW5nLkV4Y2VwdGlvbgoJewoJCUJTVDxJbnRlZ2VyLCBTdHJpbmc+IHRyZWUgPSBuZXcgQlNUPD4oKTsKICAgICAgICB0cmVlLmFkZCgxMCwgIlRlc3QxIik7CiAgICAgICAgdHJlZS5hZGQoMTIsICJUZXN0MiIpOwogICAgICAgIHRyZWUuYWRkKDEsICJUZXN0MyIpOwogICAgICAgIHRyZWUuYWRkKDUsICJUZXN0NCIpOwogICAgICAgIHRyZWUuYWRkKDYsICJUZXN0NSIpOwogICAgICAgIHRyZWUuYWRkKDMsICJUZXN0NiIpOwogICAgICAgIHRyZWUucHJpbnQoKTsKICAgICAgICB0cmVlLmRlbGV0ZSg1KTsKICAgICAgICB0cmVlLnByaW50KCk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKHRyZWUuZ2V0KDEpKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4odHJlZS5taW5LZXkoKSk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKHRyZWUubWF4S2V5KCkpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbih0cmVlLmdldCgzKSk7Cgl9Cn0=