program Main;
type
Node = record
data : integer;
left : ^Node;
right : ^Node
end;
pNode = ^Node;
function push(root : pNode; data : integer) : pNode;
var
r : pNode;
begin
if root = nil then begin
new(r);
r^.data := data;
r^.left := nil;
r^.right := nil;
push := r;
end else if data < root^.data then begin
root^.left := push(root^.left, data);
push := root
end else begin
root^.right := push(root^.right, data);
push := root
end
end;
function pop(root : pNode; var data : integer) : pNode;
var
r : pNode;
begin
if root = nil then begin { never }
pop := nil
end else if root^.left <> nil then begin
root^.left := pop(root^.left, data);
pop := root
end else begin
r := root^.right;
data := root^.data;
dispose(root);
pop := r
end
end;
type
BinTree = object
private
root : pNode;
flag : boolean;
public
procedure init;
procedure pushNode(data : integer);
function popNode(var data : integer) : boolean;
end;
pBinTree = ^BinTree;
procedure BinTree.init;
begin
root := nil;
flag := false;
end;
procedure BinTree.pushNode(data : integer);
begin
root := push(root, data);
flag := true
end;
function BinTree.popNode(var data : integer) : boolean;
begin
root := pop(root, data);
if (root = nil) then
if (flag = true) then begin
flag := false;
popNode := true
end else
popNode := false
else
popNode := true
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 }
cHJvZ3JhbSBNYWluOwoKdHlwZQogIE5vZGUgPSByZWNvcmQKICAgIGRhdGEgOiBpbnRlZ2VyOwogICAgbGVmdCA6IF5Ob2RlOwogICAgcmlnaHQgOiBeTm9kZQogIGVuZDsKICBwTm9kZSA9IF5Ob2RlOwoKZnVuY3Rpb24gcHVzaChyb290IDogcE5vZGU7IGRhdGEgOiBpbnRlZ2VyKSA6IHBOb2RlOwp2YXIKICByIDogcE5vZGU7CmJlZ2luCiAgaWYgcm9vdCA9IG5pbCB0aGVuIGJlZ2luCiAgICBuZXcocik7CiAgICByXi5kYXRhIDo9IGRhdGE7CiAgICByXi5sZWZ0IDo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