program main;
type
Node = record
data : integer;
left : ^Node;
right : ^Node
end;
pNode = ^Node;
procedure push(var root : pNode; data : integer);
var
r : pNode;
begin
if root = nil then begin
new(r);
r^.data := data;
r^.left := nil;
r^.right := nil;
root := r
end else if data < root^.data then
push(root^.left, data)
else
push(root^.right, data)
end;
function pop(var root : pNode; var data : integer) : boolean;
var
r : pNode;
begin
if root = nil then
pop := false
else if root^.left <> nil then
pop := pop(root^.left, data)
else begin
r := root;
root := r^.right;
data := r^.data;
dispose(r);
pop := true
end
end;
type
pBinTree = ^BinTree;
BinTree = object
private
root : pNode;
public
procedure init;
procedure pushNode(data : integer);
function popNode(var data : integer) : boolean;
end;
procedure BinTree.init;
begin
root := nil
end;
procedure BinTree.pushNode(data : integer);
begin
push(root, data);
end;
function BinTree.popNode(var data : integer) : boolean;
begin
popNode := pop(root, data);
end;
const
N = 10;
MAX = 1000;
var
root : pBinTree;
data, i : integer;
begin
new(root);
root^.init;
randomize;
i := 0;
while (i < 10) do begin
data := random(MAX);
root^.pushNode(data);
i := i + 1
end;
while (root^.popNode(data) = true) do
writeln(data);
dispose(root);
end.
{ end }
cHJvZ3JhbSBtYWluOwoKdHlwZQogIE5vZGUgPSByZWNvcmQKICAgIGRhdGEgOiBpbnRlZ2VyOwogICAgbGVmdCA6IF5Ob2RlOwogICAgcmlnaHQgOiBeTm9kZQogIGVuZDsKICBwTm9kZSA9IF5Ob2RlOwoKcHJvY2VkdXJlIHB1c2godmFyIHJvb3QgOiBwTm9kZTsgZGF0YSA6IGludGVnZXIpOwp2YXIKICByIDogcE5vZGU7CmJlZ2luCiAgaWYgcm9vdCA9IG5pbCB0aGVuIGJlZ2luCiAgICBuZXcocik7CiAgICByXi5kYXRhIDo9IGRhdGE7CiAgICByXi5sZWZ0IDo9IG5pbDsKICAgIHJeLnJpZ2h0IDo9IG5pbDsKICAgIHJvb3QgOj0gcgogIGVuZCBlbHNlIGlmIGRhdGEgPCByb290Xi5kYXRhIHRoZW4KICAgIHB1c2gocm9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