import java.util.*;
import java.lang.*;
import java.io.*;
class Tree
<Key extends Comparable
<Key
>, Value
> {
private class Node {
Value value;
Node left, right;
public Node
(Key key, Value value
) { this.key = key;
this.value = value;
this.left = this.right = null;
}
}
private Node root;
return root.key;
}
public Value getRootValue(){
return root.value;
}
private Node add
(Node node,
Key key, Value value
){ //возвращает измененный узел (после добавления значения)
if (node == null){
Node newnode = new Node(key,value);
return newnode;
}
int compareResult = key.compareTo(node.key);
if(compareResult == 0) {
node.value = value;
return node;
}
else if(compareResult < 0)
node.left = add(node.left, key, value);
else
node.right = add(node.right, key, value);
return node;
}
Node cur = root;
// спускаемся к самому левому листу, по свойству дерева - наименьший ключ там
for(; cur.left != null; cur = cur.left){}
return cur.key;
}
Node cur = root;
// спускаемся к самому правому листу, по свойству дерева - наибольший ключ там
for(; cur.right != null; cur = cur.right){}
return cur.key;
}
//вспомогательная функция, которая нигде не используется.
private Node NodeMinKey(Node node){
Node cur = node;
for(; cur.left != null; cur = cur.left){}
return cur;
}
public void add
(Key key, Value value
) { root = add(root, key, value);
}
//удаление вершины
public void delete
(Key key
) { Node cur = root;
Node prev = null;
//спускаемся по дереву, пока не найдем вершину или не уткнемся в null
for(; true; ){
if(cur == null) return;
if(key.compareTo(cur.key) < 0){
prev = cur; cur = cur.left;
}
else if(key.compareTo(cur.key) > 0){
prev = cur;
cur = cur.right;
}
else break;
}
/*
* Итак, у нас есть элемент, который нужно удалить (cur) и его предок (prev)
* Теперь нас ждет разбор частных случаев:
* 1)Если правый сын удаляемого элемента не существует, мы можем просто выкинуть этот элемент из
* цепи prev -> cur -> left, оставив prev -> left.
* 2)Если же правый сын существует, так просто выкинуть вершину мы не можем.
* Чтобы сохранить свойство дерева мы найдем узел с наименьшим ключом в правом поддереве cur,
* присоединим к найденному узлу левого сына cur и заменим cur на этот узел.
*/
if(cur.right == null)
if(cur == prev.right)
prev.right = cur.left;
else
prev.left = cur.left;
else{
Node mostLeft = cur.right;
prev = null;
for(; mostLeft.left != null;){
prev = mostLeft;
mostLeft = mostLeft.left;
}
if(prev != null)
prev.left = mostLeft.right;
else
cur.right = mostLeft.right;
cur.key = mostLeft.key;
cur.value = mostLeft.value;
}
}
public Value get
(Key key
) { Node cur = root;
for(; true; ){
if(cur == null) break;
if(key.compareTo(cur.key) < 0)
cur = cur.left;
else if(key.compareTo(cur.key) > 0)
cur = cur.right;
else break;
}
return cur != null? cur.value : (Value)null;
}
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);
}
public static void main
(String[] args
){ int n = 100;
for(int i = 0; i < n; i++){
a.
add( Math.
round(Math.
random() * 1000.0) + 1.0*i,
Math.
round(Math.
random() * 1000000.0) + 1.0*i
); }
System.
out.
println(a.
MaxKey() + " => " + a.
get(a.
MaxKey())); System.
out.
println(a.
MinKey() + " => " + a.
get(a.
MinKey())); System.
out.
println("First print"); a.print();
a.delete(a.getRootKey());
System.
out.
println("Second print"); a.print();
}
}
CmltcG9ydCBqYXZhLnV0aWwuKjsKaW1wb3J0IGphdmEubGFuZy4qOwppbXBvcnQgamF2YS5pby4qOwogCmNsYXNzIFRyZWUgPEtleSBleHRlbmRzIENvbXBhcmFibGU8S2V5PiwgVmFsdWU+IHsKCQoJcHJpdmF0ZSBjbGFzcyBOb2RlIHsKCQlLZXkga2V5OwoJCVZhbHVlIHZhbHVlOwoJCU5vZGUgbGVmdCwgcmlnaHQ7CgkJcHVibGljIE5vZGUgKEtleSBrZXksIFZhbHVlIHZhbHVlKSB7CgkJCXRoaXMua2V5ID0ga2V5OwoJCQl0aGlzLnZhbHVlID0gdmFsdWU7CgkJCXRoaXMubGVmdCA9IHRoaXMucmlnaHQgPSBudWxsOwoJCX0KCX0KCQoJcHJpdmF0ZSBOb2RlIHJvb3Q7CgkKCXB1YmxpYyBLZXkgZ2V0Um9vdEtleSgpewoJCXJldHVybiByb290LmtleTsKCX0KCXB1YmxpYyBWYWx1ZSBnZXRSb290VmFsdWUoKXsKCQlyZXR1cm4gcm9vdC52YWx1ZTsKCX0KCQoJcHJpdmF0ZSBOb2RlIGFkZCAoTm9kZSBub2RlLEtleSBrZXksIFZhbHVlIHZhbHVlKXsKCQkvL9Cy0L7Qt9Cy0YDQsNGJ0LDQtdGCINC40LfQvNC10L3QtdC90L3Ri9C5INGD0LfQtdC7ICjQv9C+0YHQu9C1INC00L7QsdCw0LLQu9C10L3QuNGPINC30L3QsNGH0LXQvdC40Y8pCgkJaWYgKG5vZGUgPT0gbnVsbCl7CgkJCU5vZGUgbmV3bm9kZSA9IG5ldyBOb2RlKGtleSx2YWx1ZSk7CgkJCXJldHVybiBuZXdub2RlOwoJCX0KCQlpbnQgY29tcGFyZVJlc3VsdCA9IGtleS5jb21wYXJlVG8obm9kZS5rZXkpOwogICAgICAgICAgICAgICAgaWYoY29tcGFyZVJlc3VsdCA9PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgbm9kZS52YWx1ZSA9IHZhbHVlOwogICAgICAgICAgICAgICAgICAgIHJldHVybiBub2RlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZSBpZihjb21wYXJlUmVzdWx0IDwgMCkKICAgICAgICAgICAgICAgICAgICBub2RlLmxlZnQgPSBhZGQobm9kZS5sZWZ0LCBrZXksIHZhbHVlKTsKICAgICAgICAgICAgICAgIGVsc2UgCiAgICAgICAgICAgICAgICAgICAgbm9kZS5yaWdodCA9IGFkZChub2RlLnJpZ2h0LCBrZXksIHZhbHVlKTsKICAgICAgICByZXR1cm4gbm9kZTsKCX0KICAgICAgICAKICAgICAgICBwdWJsaWMgS2V5IE1pbktleSgpewogICAgICAgICAgIE5vZGUgY3VyID0gcm9vdDsKICAgICAgICAgICAvLyDRgdC/0YPRgdC60LDQtdC80YHRjyDQuiDRgdCw0LzQvtC80YMg0LvQtdCy0L7QvNGDINC70LjRgdGC0YMsINC/0L4g0YHQstC+0LnRgdGC0LLRgyDQtNC10YDQtdCy0LAgLSDQvdCw0LjQvNC10L3RjNGI0LjQuSDQutC70Y7RhyDRgtCw0LwKICAgICAgICAgICBmb3IoOyBjdXIubGVmdCAhPSBudWxsOyBjdXIgPSBjdXIubGVmdCl7fQogICAgICAgICAgIHJldHVybiBjdXIua2V5OwogICAgICAgIH0KICAgICAgICBwdWJsaWMgS2V5IE1heEtleSgpewogICAgICAgICAgIE5vZGUgY3VyID0gcm9vdDsKICAgICAgICAgICAvLyDRgdC/0YPRgdC60LDQtdC80YHRjyDQuiDRgdCw0LzQvtC80YMg0L/RgNCw0LLQvtC80YMg0LvQuNGB0YLRgywg0L/QviDRgdCy0L7QudGB0YLQstGDINC00LXRgNC10LLQsCAtINC90LDQuNCx0L7Qu9GM0YjQuNC5INC60LvRjtGHINGC0LDQvAogICAgICAgICAgIGZvcig7IGN1ci5yaWdodCAhPSBudWxsOyBjdXIgPSBjdXIucmlnaHQpe30KICAgICAgICAgICByZXR1cm4gY3VyLmtleTsKICAgICAgICB9CiAgICAgICAKICAgICAgICAvL9Cy0YHQv9C+0LzQvtCz0LDRgtC10LvRjNC90LDRjyDRhNGD0L3QutGG0LjRjywg0LrQvtGC0L7RgNCw0Y8g0L3QuNCz0LTQtSDQvdC1INC40YHQv9C+0LvRjNC30YPQtdGC0YHRjy4KICAgICAgICBwcml2YXRlIE5vZGUgTm9kZU1pbktleShOb2RlIG5vZGUpewogICAgICAgICAgIE5vZGUgY3VyID0gbm9kZTsKICAgICAgICAgICBmb3IoOyBjdXIubGVmdCAhPSBudWxsOyBjdXIgPSBjdXIubGVmdCl7fQogICAgICAgICAgIHJldHVybiBjdXI7CiAgICAgICAgfQoJCglwdWJsaWMgdm9pZCBhZGQoS2V5IGtleSwgVmFsdWUgdmFsdWUpIHsKCQlyb290ID0gYWRkKHJvb3QsIGtleSwgdmFsdWUpOwoJfQoJCiAgICAgICAgLy/Rg9C00LDQu9C10L3QuNC1INCy0LXRgNGI0LjQvdGLCglwdWJsaWMgdm9pZCBkZWxldGUoS2V5IGtleSkgewogICAgICAgICAgICBOb2RlIGN1ciA9IHJvb3Q7CiAgICAgICAgICAgIE5vZGUgcHJldiA9IG51bGw7CiAgICAgICAgICAgIC8v0YHQv9GD0YHQutCw0LXQvNGB0Y8g0L/QviDQtNC10YDQtdCy0YMsINC/0L7QutCwINC90LUg0L3QsNC50LTQtdC8INCy0LXRgNGI0LjQvdGDINC40LvQuCDQvdC1INGD0YLQutC90LXQvNGB0Y8g0LIgbnVsbAogICAgICAgICAgICBmb3IoOyB0cnVlOyApewogICAgICAgICAgICAgICAgaWYoY3VyID09IG51bGwpIHJldHVybjsKICAgICAgICAgICAgICAgIGlmKGtleS5jb21wYXJlVG8oY3VyLmtleSkgPCAwKXsKICAgICAgICAgICAgICAgICAgICBwcmV2ID0gY3VyOyBjdXIgPSBjdXIubGVmdDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UgaWYoa2V5LmNvbXBhcmVUbyhjdXIua2V5KSA+IDApewogICAgICAgICAgICAgICAgICAgIHByZXYgPSBjdXI7CiAgICAgICAgICAgICAgICAgICAgY3VyID0gY3VyLnJpZ2h0OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZSBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgICAvKgogICAgICAgICAgICAgKiDQmNGC0LDQuiwg0YMg0L3QsNGBINC10YHRgtGMINGN0LvQtdC80LXQvdGCLCDQutC+0YLQvtGA0YvQuSDQvdGD0LbQvdC+INGD0LTQsNC70LjRgtGMIChjdXIpINC4INC10LPQviDQv9GA0LXQtNC+0LogKHByZXYpCiAgICAgICAgICAgICAqINCi0LXQv9C10YDRjCDQvdCw0YEg0LbQtNC10YIg0YDQsNC30LHQvtGAINGH0LDRgdGC0L3Ri9GFINGB0LvRg9GH0LDQtdCyOgogICAgICAgICAgICAgKiAgICAgIDEp0JXRgdC70Lgg0L/RgNCw0LLRi9C5INGB0YvQvSDRg9C00LDQu9GP0LXQvNC+0LPQviDRjdC70LXQvNC10L3RgtCwINC90LUg0YHRg9GJ0LXRgdGC0LLRg9C10YIsINC80Ysg0LzQvtC20LXQvCDQv9GA0L7RgdGC0L4g0LLRi9C60LjQvdGD0YLRjCDRjdGC0L7RgiDRjdC70LXQvNC10L3RgiDQuNC3CiAgICAgICAgICAgICAqICAgICAgICDRhtC10L/QuCBwcmV2IC0+IGN1ciAtPiBsZWZ0LCDQvtGB0YLQsNCy0LjQsiBwcmV2IC0+IGxlZnQuCiAgICAgICAgICAgICAqICAgICAgMinQldGB0LvQuCDQttC1INC/0YDQsNCy0YvQuSDRgdGL0L0g0YHRg9GJ0LXRgdGC0LLRg9C10YIsINGC0LDQuiDQv9GA0L7RgdGC0L4g0LLRi9C60LjQvdGD0YLRjCDQstC10YDRiNC40L3RgyDQvNGLINC90LUg0LzQvtC20LXQvC4gCiAgICAgICAgICAgICAqICAgICAgICDQp9GC0L7QsdGLINGB0L7RhdGA0LDQvdC40YLRjCDRgdCy0L7QudGB0YLQstC+INC00LXRgNC10LLQsCDQvNGLINC90LDQudC00LXQvCDRg9C30LXQuyDRgSDQvdCw0LjQvNC10L3RjNGI0LjQvCDQutC70Y7Rh9C+0Lwg0LIg0L/RgNCw0LLQvtC8INC/0L7QtNC00LXRgNC10LLQtSBjdXIsCiAgICAgICAgICAgICAqICAgICAgICDQv9GA0LjRgdC+0LXQtNC40L3QuNC8INC6INC90LDQudC00LXQvdC90L7QvNGDINGD0LfQu9GDINC70LXQstC+0LPQviDRgdGL0L3QsCBjdXIg0Lgg0LfQsNC80LXQvdC40LwgY3VyINC90LAg0Y3RgtC+0YIg0YPQt9C10LsuCiAgICAgICAgICAgICAqLwogICAgICAgICAgICBpZihjdXIucmlnaHQgPT0gbnVsbCkKICAgICAgICAgICAgICAgIGlmKGN1ciA9PSBwcmV2LnJpZ2h0KQogICAgICAgICAgICAgICAgICAgIHByZXYucmlnaHQgPSBjdXIubGVmdDsKICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICBwcmV2LmxlZnQgPSBjdXIubGVmdDsKICAgICAgICAgICAgZWxzZXsKICAgICAgICAgICAgICAgIE5vZGUgbW9zdExlZnQgPSBjdXIucmlnaHQ7CiAgICAgICAgICAgICAgICBwcmV2ID0gbnVsbDsKICAgICAgICAgICAgICAgIGZvcig7IG1vc3RMZWZ0LmxlZnQgIT0gbnVsbDspewogICAgICAgICAgICAgICAgICAgIHByZXYgPSBtb3N0TGVmdDsKICAgICAgICAgICAgICAgICAgICBtb3N0TGVmdCA9IG1vc3RMZWZ0LmxlZnQ7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZihwcmV2ICE9IG51bGwpCiAgICAgICAgICAgICAgICAgICAgcHJldi5sZWZ0ID0gbW9zdExlZnQucmlnaHQ7CiAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgY3VyLnJpZ2h0ID0gbW9zdExlZnQucmlnaHQ7CiAgICAgICAgICAgICAgICBjdXIua2V5ID0gbW9zdExlZnQua2V5OwogICAgICAgICAgICAgICAgY3VyLnZhbHVlID0gbW9zdExlZnQudmFsdWU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIAoJfQoJCglwdWJsaWMgVmFsdWUgZ2V0KEtleSBrZXkpIHsKICAgICAgICAgICAgTm9kZSBjdXIgPSByb290OwogICAgICAgICAgICBmb3IoOyB0cnVlOyApewogICAgICAgICAgICAgICAgaWYoY3VyID09IG51bGwpIGJyZWFrOwogICAgICAgICAgICAgICAgaWYoa2V5LmNvbXBhcmVUbyhjdXIua2V5KSA8IDApCiAgICAgICAgICAgICAgICAgICAgY3VyID0gY3VyLmxlZnQ7CiAgICAgICAgICAgICAgICBlbHNlIGlmKGtleS5jb21wYXJlVG8oY3VyLmtleSkgPiAwKQogICAgICAgICAgICAgICAgICAgIGN1ciA9IGN1ci5yaWdodDsKICAgICAgICAgICAgICAgIGVsc2UgYnJlYWs7CiAgICAgICAgICA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gICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCk7CiAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkZpcnN0IHByaW50Iik7CgkJYS5wcmludCgpOwoJCWEuZGVsZXRlKGEuZ2V0Um9vdEtleSgpKTsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigpOwogICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCk7CiAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oKTsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiU2Vjb25kIHByaW50Iik7CgkJYS5wcmludCgpOwoJfQp9