import java.util.Random;
class Indirect {
Indirect() { data = null; }
}
class Node {
private Node left;
private Node right;
this.data = data;
this.left = this.right = null;
}
public static Node pushNode
(Node root,
Integer data
) { if (root == null) {
return new Node(data);
} else if (data.intValue() < root.data.intValue()) {
root.left = pushNode(root.left, data);
return root;
} else {
root.right = pushNode(root.right, data);
return root;
}
}
public static Node popNode(Node root, Indirect idata) {
if (root == null) {
idata = null;
return null;
}
if (root.left != null) {
root.left = popNode(root.left, idata);
return root;
}
idata.data = root.data;
return root.right;
}
}
class BinTree {
private Node root;
BinTree() { root = null; }
void pushNode
(Integer data
) { root
= Node.
pushNode(root, data
); } Indirect idata;
root = Node.popNode(root, idata = new Indirect());
if (root == null && idata == null)
return null;
return idata.data;
}
}
class Main {
final static int N = 10;
final static int MAX = 10000;
public static void main
(String[] args
) { BinTree root = new BinTree();
for (int i = 0; i < N; i++) {
data
= new Integer(random.
nextInt(MAX
)); root.pushNode(data);
}
while ((data = root.popNode()) != null) {
}
}
}
/* end */
aW1wb3J0IGphdmEudXRpbC5SYW5kb207CgpjbGFzcyBJbmRpcmVjdCB7CiAgSW50ZWdlciBkYXRhOwogIEluZGlyZWN0KCkgeyBkYXRhID0gbnVsbDsgfQp9CgpjbGFzcyBOb2RlIHsKICBwcml2YXRlIEludGVnZXIgZGF0YTsKICBwcml2YXRlIE5vZGUgbGVmdDsKICBwcml2YXRlIE5vZGUgcmlnaHQ7CiAgcHJpdmF0ZSBOb2RlKEludGVnZXIgZGF0YSkgewogICAgdGhpcy5kYXRhID0gZGF0YTsKICAgIHRoaXMubGVmdCA9IHRoaXMucmlnaHQgPSBudWxsOwogIH0KICBwdWJsaWMgc3RhdGljIE5vZGUgcHVzaE5vZGUoTm9kZSByb290LCBJbnRlZ2VyIGRhdGEpIHsKICAgIGlmIChyb290ID09IG51bGwpIHsKICAgICAgcmV0dXJuIG5ldyBOb2RlKGRhdGEpOwogICAgfSBlbHNlIGlmIChkYXRhLmludFZhbHVlKCkgPCByb290LmRhdGEua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