sirko 0 Newbie Poster

I've been massing with this problem for almost a week. It's about DFS search in a binary tree. It compiles well but when i run it I get exitcode 216 error. Can anybody help?

program prg;
uses crt;
type
   ptrnode=^node;
   node = record
        data: char;
        l,r: ptrnode;
   end;

   pnode=^rnode;
     rnode=record
       open:char;
       link:pnode;
     end;



procedure add(var p: ptrnode; x :char);
begin
     if p <> nil then begin
        if x > p^.data then add(p^.r,x);
        if x < p^.data then add(p^.l,x);
        if x = p^.data then writeln('This element already exist');
    end
    else begin
         p:=new(ptrnode);
         p^.data:=x;
         p^.l:=nil;
         p^.r:=nil;
    end;
end;
procedure print(p: ptrnode; n: integer);
var i: integer;
begin
     if p <> nil then begin
        if p^.r <> nil then print(p^.r,n+1);
        for i:=1 to n do write('   '); writeln(p^.data);
        if p^.l <> nil then print(p^.l,n+1);
     end;
end;

procedure del(var p: ptrnode);
begin
     if p <> nil then begin
        if p^.r <> nil then del(p^.r);
        if p^.l <> nil then del(p^.l);
        dispose(p);
     end;
end;

function depth(p: ptrnode): integer; {вычисляет глубину дерева}
var dr,dl: integer;
begin
     if p <> nil then begin
        if (p^.r <> nil) then dr:=depth(p^.r)+1;
        if (p^.l <> nil) then dl:=depth(p^.l)+1;
     end;
     if dr > dl then  depth:=dr else depth:=dl;
end;

procedure step(p: ptrnode;lavel,stop: integer); {эта поцидура распечатывает все узлы уровня stop}
begin
     if p <> nil then begin
        if lavel=stop then writeln(p^.data);
        if (p^.r <> nil) then step(p^.r,lavel+1,stop);
        if (p^.l <> nil) then step(p^.l,lavel+1,stop);
     end;
end;

function push(top:pnode; open:char):pnode;
var p:pnode;
begin
new(p);
 p^.open:=open;p^.link:=top;
 push:=p;
 end;

function pop(top:pnode; var open:char):pnode;
var p:pnode;
begin
 open:=top^.open;
 pop:=top^.link;
 dispose(top);
end;


var
   root: ptrnode;
    i: integer;
     close:string;
      top:pnode;
      x:char;
begin
     clrscr;
     close:='';
     top:=nil;

     root:=nil;
     add(root,'a');
     add(root,'c');
     add(root,'d');
     add(root,'b');
     add(root,'h');
     add(root,'e');
     add(root,'o');
     print(root,0);
     for i:=1 to depth(root) do step(root,1,i);
top:=push(top,root^.data);
      write(top^.open);
  while x<>'o' do begin

    while root<>nil do begin
        top:=pop(top,x); close:=close+x;
if root^.l<>nil then begin
top:=push(top,root^.l^.data);
root:=root^.l;  end
else  begin
if root^.r<>nil then begin
 top:=push(top,root^.r^.data);
  root:=root^.r;  end
else  del(root);
    end;  end; end;

     write(close);
     readln;
    { del(root); }
    readkey;
end.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.