class BinTree {
private BinNode root;
public BinTree(BinNode root) {
this.root = root;
}
public void recreate(int key) {
if(root != null) { root = root.getNewRoot(key); }
if(root != null) { root.search(root, key); }
}
@Override
public String toString
() { return "{" + (root == null ? "" : "" + root.getKey() + (root.getLeft() == null ? "" : ", left: " + root.getLeft()) + (root.getRight() == null ? "" : ", right: " + root.getRight())) + "}";
}
}
class BinNode {
private int key;
private BinNode left;
private BinNode right;
private BinNode parent;
public BinNode(int key, BinNode left, BinNode right) {
this.key = key;
this.left = left;
this.right = right;
if(this.left != null) { this.left.parent = this; }
if(this.right != null) { this.right.parent = this; }
}
public BinNode getNewRoot(int key) {
BinNode newRoot = this;
for( ; newRoot != null; ) {
if(key < newRoot.key) { newRoot = newRoot.left; }
else { break; }
}
return newRoot;
}
public void search(BinNode p, int key) {
if(p == null) { return; }
else if(p.key < key) {
search(p.right, key);
} else if(p.key > key) {
if(p.parent != null) { p.parent.right = p.left; }
search(p.left, key);
} else {
}
}
public int getKey() { return key; }
public BinNode getLeft() { return left; }
public BinNode getRight() { return right; }
@Override
public String toString
() { return "[" + key + (left == null ? "" : ", left: " + left) + (right == null ? "" : ", right: " + right) + "]";
}
}
public class BSTMinDistiller {
public static void main
(String[] args
) { BinNode n02 = new BinNode( 2, null, null);
BinNode n10 = new BinNode(10, null, null);
BinNode n14 = new BinNode(14, null, null);
BinNode n22 = new BinNode(22, null, null);
BinNode n26 = new BinNode(26, null, null);
BinNode n34 = new BinNode(34, null, null);
BinNode n38 = new BinNode(38, null, null);
BinNode n46 = new BinNode(46, null, null);
BinNode n52 = new BinNode(52, null, null);
BinNode n60 = new BinNode(60, null, null);
BinNode n64 = new BinNode(64, null, null);
BinNode n72 = new BinNode(72, null, null);
BinNode n76 = new BinNode(76, null, null);
BinNode n84 = new BinNode(84, null, null);
BinNode n88 = new BinNode(88, null, null);
BinNode n96 = new BinNode(96, null, null);
BinNode n06 = new BinNode( 6, n02, n10);
BinNode n18 = new BinNode(18, n14, n22);
BinNode n30 = new BinNode(30, n26, n34);
BinNode n42 = new BinNode(42, n38, n46);
BinNode n56 = new BinNode(56, n52, n60);
BinNode n68 = new BinNode(68, n64, n72);
BinNode n80 = new BinNode(80, n76, n84);
BinNode n92 = new BinNode(92, n88, n96);
BinNode n12 = new BinNode(12, n06, n18);
BinNode n36 = new BinNode(36, n30, n42);
BinNode n62 = new BinNode(62, n56, n68);
BinNode n86 = new BinNode(86, n80, n92);
BinNode n25 = new BinNode(25, n12, n36);
BinNode n75 = new BinNode(75, n62, n86);
BinTree tree01 = new BinTree(new BinNode(50, n25, n75));
BinTree tree02 = new BinTree(new BinNode(50, n25, n75));
tree02.recreate(65);
System.
out.
println("tree02: 65");
BinTree tree03 = new BinTree(new BinNode(50, n25, n75));
tree03.recreate(49);
System.
out.
println("tree03: 49");
BinTree tree04 = new BinTree(new BinNode(50, n25, n75));
tree04.recreate(1);
System.
out.
println("tree04: 1");
BinTree tree05 = new BinTree(new BinNode(50, n25, n75));
tree05.recreate(97);
System.
out.
println("tree05: 97");
BinTree tree06 = new BinTree(new BinNode(50, n25, n75));
tree06.recreate(9);
System.
out.
println("tree06: 9");
BinTree tree07 = new BinTree(new BinNode(2, null, null));
BinTree tree08 = new BinTree(new BinNode(2, null, null));
tree08.recreate(3);
System.
out.
println("tree08: 3");
BinTree tree09 = new BinTree(new BinNode(2, null, null));
tree09.recreate(1);
System.
out.
println("tree09: 1");
BinTree tree10 = new BinTree(null);
BinTree tree11 = new BinTree(null);
tree11.recreate(99);
System.
out.
println("tree11: 99"); }
}
Y2xhc3MgQmluVHJlZSB7CiAgICBwcml2YXRlIEJpbk5vZGUgcm9vdDsKCiAgICBwdWJsaWMgQmluVHJlZShCaW5Ob2RlIHJvb3QpIHsKICAgICAgICB0aGlzLnJvb3QgPSByb290OwogICAgfQoKICAgIHB1YmxpYyB2b2lkIHJlY3JlYXRlKGludCBrZXkpIHsKICAgICAgICBpZihyb290ICE9IG51bGwpIHsgcm9vdCA9IHJvb3QuZ2V0TmV3Um9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5rZXkpIHsgdGhyb3cgbmV3IElsbGVnYWxBcmd1bWVudEV4Y2VwdGlvbigpOyB9CiAgICAgICAgICAgIGlmKGtleSA8IG5ld1Jvb3Qua2V5KSB7IG5ld1Jvb3QgPSBuZXdSb290LmxlZnQ7IH0KICAgICAgICAgICAgZWxzZSB7IGJyZWFrOyB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBuZXdSb290OwogICAgfQoKICAgIHB1YmxpYyB2b2lkIHNlYXJjaChCaW5Ob2RlIHAsIGludCBrZXkpIHsKICAgICAgICBpZihwID09IG51bGwpIHsgcmV0dXJuOyB9CiAgICAgICAgZWxzZSBpZihwLmtleSA8IGtleSkgewogICAgICAgICAgICBzZWFyY2gocC5yaWdodCwga2V5KTsKICAgICAgICB9IGVsc2UgaWYocC5rZXkgPiBrZXkpIHsKICAgICAgICAgICAgaWYocC5wYXJlbnQgIT0gbnVsbCkgeyBwLnBhcmVudC5yaWdodCA9IHAubGVmdDsgfQogICAgICAgICAgICBzZWFyY2gocC5sZWZ0LCBrZXkpOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHRocm93IG5ldyBJbGxlZ2FsQXJndW1lbnRFeGNlcHRpb24oKTsKICAgICAgICB9CiAgICB9CgogICAgcHVibGljIGludCBnZXRLZXkoKSB7IHJldHVybiBrZXk7IH0KICAgIHB1YmxpYyBCaW5Ob2RlIGdldExlZnQoKSB7IHJldHVybiBsZWZ0OyB9CiAgICBwdWJsaWMgQmluTm9kZSBnZXRSaWdodCgpIHsgcmV0dXJuIHJpZ2h0OyB9CgogICAgQE92ZXJyaWRlIHB1YmxpYyBTdHJpbmcgdG9TdHJpbmcoKSB7CiAgICAgICAgcmV0dXJuICJbIiArIGtleSArIChsZWZ0ID09IG51bGwgPyAiIiA6ICIsIGxlZnQ6ICIgKyBsZWZ0KSArIChyaWdodCA9PSBudWxsID8gIiIgOiAiLCByaWdodDogIiArIHJpZ2h0KSArICJdIjsKICAgIH0KfQoKcHVibGljIGNsYX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