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 BinTree = class
private
root : pNode;
public
constructor Create();
procedure pushNode(data : integer);
function popNode(var data : integer) : boolean;
end;
constructor BinTree.Create();
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 : BinTree;
data, i : integer;
begin
root := BinTree.Create;
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);
root.Free
end.
{ end }
cHJvZ3JhbSBtYWluOwoKdHlwZQogIE5vZGUgPSByZWNvcmQKICAgIGRhdGEgOiBpbnRlZ2VyOwogICAgbGVmdCA6IF5Ob2RlOwogICAgcmlnaHQgOiBeTm9kZQogIGVuZDsKICBwTm9kZSA9IF5Ob2RlOwoKcHJvY2VkdXJlIHB1c2godmFyIHJvb3QgOiBwTm9kZTsgZGF0YSA6IGludGVnZXIpOwp2YXIKICByIDogcE5vZGU7CmJlZ2luCiAgaWYgcm9vdCA9IG5pbCB0aGVuIGJlZ2luCiAgICBuZXcocik7CiAgICByXi5kYXRhIDo9IGRhdGE7CiAgICByXi5sZWZ0IDo9IG5pbDsKICAgIHJeLnJpZ2h0IDo9IG5pbDsKICAgIHJvb3QgOj0gcgogIGVuZCBlbHNlIGlmIGRhdGEgPCByb290Xi5kYXRhIHRoZW4KICAgIHB1c2gocm9vdF4ubGVmdCwgZGF0Y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