program exp; uses crt; Type PTree = ^TTree; TTree = Record Data : Integer; Left, Right : PTree; end; var Tree : PTree; f1 :text; i, sum, y1 : integer; Procedure InsTree(var ANode : PTree; n : integer); Begin if ANode = nil then Begin new(ANode); With ANode^ do Begin Left := nil; Right := nil; Data := n; end; end else if n< ANode^.Data then InsTree(ANode^.Left, n) else InsTree(ANode^.Right, n); End; Procedure PrintList(ANode : PTree); Begin if ANode <> nil then Begin if (ANode^.Left=nil) and (ANode^.Right=nil) then write(' ', ANode^.Data); PrintList(ANode^.Left); PrintList(ANode^.Right); End; End; begin clrScr; assign(f1,'t1.txt'); reset(f1); while not eof(f1) do begin readln(f1,i); InsTree(Tree, i); end; writeln('Листья дерева'); PrintList(Tree); close(f1); end. 

for some reason it does not remove the leaves, I have so scored my head that I cannot figure out the simple. Who can help

Closed due to the fact that Nicolas Chabanovsky 12 Nov '16 at 5:33 am off topic.

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Nicolas Chabanovsky
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Code format. Where is the ladder? It is impossible to read. - iluxa1810
  • I vote against this issue, because It does not reflect the desire to understand - 4per
  • In my opinion, you need to figure out what is in your file ... Here is an example with clearly stitched data - it seems to work: ideone.com/tXAAce - Harry
  • @ 4per thanks for the help, you were very helpful! - Indentikon
  • @ iluxa1810 formatted. - Indentikon

1 answer 1

Error in 'PrintList' procedure. Because of the condition

if (ANode^.Left=nil) and (ANode^.Right=nil) then

Only the last leaves will be displayed. Must be so

 procedure PrintList(ANode : PTree); begin if ANode <> nil then begin PrintList(ANode^.Left); Write(' ', ANode^.Data); PrintList(ANode^.Right); end; end; 

UPDATE

Before the loop, it would be nice to explicitly initialize the Tree variable.

 reset(f1); Tree = nil; while not eof(f1) do 

And of course, the allocated memory must be freed. For each call to New() must be a call to Dispose()

 procedure DestroyTree(ANode: PTree); begin if ANode <> nil then begin DestroyTree(ANode^.Left); DestroyTree(ANode^.Right); Dispose(ANode); end; end; 
  • He needs to bring out the leaves; you deduce everything . - Harry
  • @Harry Read the title of the topic "Write a recursive procedure that prints the elements from all the leaves of the tree" - Anton Shchyrov
  • @Harry but for some reason, it does not display a number at all ... why? - Indentikon
  • @ Indentikon and my example does not display? - Anton Shchyrov
  • And what do you enter? In my opinion, you have a problem not in the conclusion, but in the construction of the tree. - Harry