using System;
class Node {
private int data;
private Node left, right;
public static void push(ref Node root, int data) {
if (root == null) {
Node p = new Node();
p.data = data;
p.left = p.right = null;
root = p;
} else if (data < root.data)
push(ref root.left, data);
else
push(ref root.right, data);
}
public static bool pop(ref Node root, ref int data) {
if (root == null)
return false;
if (root.left != null)
return pop(ref root.left, ref data);
else {
data = root.data;
root = root.right;
return true;
}
}
}
class BinTree {
private Node root;
public BinTree() { root = null; }
public void pushNode(int data) { Node.push(ref root, data); }
public bool popNode(ref int data) { return Node.pop(ref root, ref data); }
}
class Sample {
public static void Main() {
const int n = 10;
const int max = 1000;
const int seed = 31415926;
Random r = new Random(seed);
BinTree root = new BinTree();
for (int i = 0; i < n; i++)
root.pushNode(r.Next(max));
int data = 0;
Console.WriteLine("C#");
while (root.popNode(ref data))
Console.WriteLine(data);
}
}
/* end */
dXNpbmcgU3lzdGVtOwoKY2xhc3MgTm9kZSB7CiAgcHJpdmF0ZSBpbnQgZGF0YTsKICBwcml2YXRlIE5vZGUgbGVmdCwgcmlnaHQ7CiAgcHVibGljIHN0YXRpYyB2b2lkIHB1c2gocmVmIE5vZGUgcm9vdCwgaW50IGRhdGEpIHsKICAgIGlmIChyb290ID09IG51bGwpIHsKICAgICAgTm9kZSBwID0gbmV3IE5vZGUoKTsKICAgICAgcC5kYXRhID0gZGF0YTsKICAgICAgcC5sZWZ0ID0gcC5yaWdodCA9IG51bGw7CiAgICAgIHJvb3QgPSBwOwogICAgfSBlbHNlIGlmIChkYXRhIDwgcm9vdC5kYXRhKQogICAgICBwdXNoKHJlZiByb290LmxlZnQsIGRhdGEpOwogICAgZWxzZQogICAgICBwdXNoKHJlZiByb290LnJpZ2h0LCBkYXRhKTsKICB9CiAgcHVibGljIHN0YXRpYyBib29sIHBvcChyZWYgTm9kZSByb290LCByZWYgaW50IGRhdGEpIHsKICAgIGlmIChyb290ID09IG51bGwpCiAgICAgIHJldHVybiBmYWxzZTsKICAgIGlmIChyb290LmxlZnQgIT0gbnVsbCkKICAgICA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