How to print out Tree Structure?

Pascal binery tree search problem.?

  • Every time I run this darn program I get a weird 216 exitcode error. Can anybody help me figure it out? Tha is a Pascal DFS(Depth First Search) binery search app. program prg; uses crt; type ptrnode=^node; node = record data: char; l,r: ptrnode; end; pnode=^rnode; rnode=record open:char; {initialising tree} link:pnode; end; procedure add(var p: ptrnode; x :char); begin {adding new elements} 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 {procedure that would show tat order of elements on the monitor} 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; {calculates the depth (number of levels) of a tree} 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); {goes through every level until stop level} 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; {so we have stack structure here and pop(FIFO)} begin new(p); {in these two functions we check if we're on 'the right way' if yes that the function} p^.open:=open;p^.link:=top; {rembers the point, if not dispose} 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'); {initializes the tree and puts the given data in a right order} 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 {the result should smt like a chain if nodes} {for instace a->b->d->f} else del(root); end; end; end; { write;} readln; del(root); readkey; end.

  • Answer:

    Hey Sende, :D I feel like a complete fool. :) I didn't realise the e-mails from "me, Oley" = "Sende"! :\ Anyway, I'm now going through your app. I've already found and fixed two bugs (one in the depth procedure, where you have not initialised the vars dr and lr to zero, did you know you had a tree with 1789 levels!! ;) I will update this question later, with a fixed program, and I will send you the code to the "me, Oley" mail address. (I'm so sorry about that, lol) na

sende at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.