From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,WEIRD_QUOTING autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,5d80f40bdd51947e X-Google-Attributes: gid103376,public From: "David C. Hoos, Sr." Subject: Re: binary tree and files Date: 2000/11/04 Message-ID: X-Deja-AN: 689717744 Content-Transfer-Encoding: 7bit References: <8tuk7l$aq5$1@wanadoo.fr> Content-Type: text/plain; charset="iso-8859-1" X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 X-Abuse-Info: Please forward ALL headers when reporting abuse. X-Complaints-To: abuse@telocity.net X-Trace: MjE3IE5vQXV0aFVzZXIgVEVMT0NJVFktUkVBREVSUyAyMTYuMjI3LjQ3LjQ5ICBTYXQsIDA0uE5v!diAyMDAwIDEzOjM3OjI0IFBTVA== MIME-Version: 1.0 NNTP-Posting-Date: Sat, 04 Nov 2000 13:37:24 PST Newsgroups: comp.lang.ada Date: 2000-11-04T00:00:00+00:00 List-Id: My earlier post had a couple of errors which are corrected in this version of how I would handle your problem: with Ada.Streams.Stream_IO; with Ada.Text_IO; with Ada.Unchecked_Deallocation; procedure Binary_Tree_Stream is type Node; type Node_Access is access Node; -- Since writing access values to a stream has no value, we want to -- write, instead, the designated object to the stream, or if the -- access value is null, an indication that the object is null. -- To that end, we override the default stream attributes for the -- Node_Access type; procedure Read (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : out Node_Access); procedure Write (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : in Node_Access); for Node_Access'Read use Read; for Node_Access'Write use Write; type Node is record Name : String (1 .. 18); Left : Node_Access; Right : Node_Access; end record; ---------------- -- Deallocate -- ---------------- procedure Deallocate (Item : in out Node_Access) is procedure Free is new Ada.Unchecked_Deallocation (Object => Node, Name => Node_Access); begin if Item /= null then Deallocate (Item.Left); Deallocate (Item.Right); Free (Item); end if; end Deallocate; ---------- -- Read -- ---------- procedure Read (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : out Node_Access) is Is_Non_Null : Boolean; begin Boolean'Read (Stream, Is_Non_Null); if Is_Non_Null then Item := new Node'(Node'Input (Stream)); else Item := null; end if; end Read; ----------- -- Write -- ----------- procedure Write (Stream : access Ada.Streams.Root_Stream_Type'Class; Item : in Node_Access) is begin Boolean'Write (Stream, Item /= null); if Item /= null then Node'Output (Stream, Item.all); end if; end Write; ------------- -- Display -- ------------- procedure Display (Item : Node_Access; Level : Natural := 0) is Indent : String (1 .. 3 * Level) := (others => ' '); begin -- Ada.Text_IO.Put (Indent); if Item /= null then Ada.Text_IO.Put_Line ("Name : """ & Item.Name & """"); if Level /= 0 then Ada.Text_IO.Put (" "); end if; Ada.Text_IO.Put (Indent & "Left : "); Display (Item.Left, Level + 1); if Level /= 0 then Ada.Text_IO.Put (" "); end if; Ada.Text_IO.Put (Indent & "Right : "); Display (Item.Right, Level + 1); else Ada.Text_IO.Put_Line ("null"); end if; end Display; -- Since we haven't defined operations for manipulating the tree, we -- declare an initialized tree object, just so we can demonstrate -- writing the tree to the stream and reading it back. Tree : Node_Access := new Node' (Name => "Parent ", Left => new Node' (Name => "Child 1 ", Left => null, Right => null), Right => new Node' (Name => "Child 2 ", Left => new Node' (Name => "Grandchild 1 ", Left => null, Right => null), Right => new Node' (Name => "Grandchild 2 ", Left => new Node' (Name => "Great-grandchild 1", Left => null, Right => null), Right => null) ) ); The_File : Ada.Streams.Stream_IO.File_Type; The_Stream : Ada.Streams.Stream_IO.Stream_Access; Reconstructed_Tree : Node_Access; begin -- Display the tree Ada.Text_IO.Put_Line ("The tree:"); Display (Tree); -- Create the file Ada.Streams.Stream_IO.Create (Name => "tree.dat", File => The_File, Mode => Ada.Streams.Stream_IO.Out_File); -- Associate the file with a stream The_Stream := Ada.Streams.Stream_IO.Stream (The_File); -- Write the tree to the file Node_Access'Write (The_Stream, Tree); -- Close the file Ada.Streams.Stream_IO.Close (The_File); -- Deallocate the memory used by the tree Deallocate (Tree); -- Display the tree after deallocation Ada.Text_IO.Put_Line ("The tree after deallocation:"); Display (Tree); -- Open the file Ada.Streams.Stream_IO.Open (Name => "tree.dat", File => The_File, Mode => Ada.Streams.Stream_IO.In_File); -- Associate the file with a stream The_Stream := Ada.Streams.Stream_IO.Stream (The_File); -- Read the tree from the file Node_Access'Read (The_Stream, Reconstructed_Tree); -- Close the file Ada.Streams.Stream_IO.Close (The_File); -- Display the reconstructed tree Ada.Text_IO.Put_Line ("The reconstructed tree:"); Display (Reconstructed_Tree); -- Create a second file to which we will write the reconstructed -- tree for comparison of the files Ada.Streams.Stream_IO.Create (Name => "reconstructed_tree.dat", File => The_File, Mode => Ada.Streams.Stream_IO.Out_File); -- Associate the file with a stream The_Stream := Ada.Streams.Stream_IO.Stream (The_File); -- Write the tree to the file Node_Access'Write (The_Stream, Reconstructed_Tree); -- Close the file Ada.Streams.Stream_IO.Close (The_File); -- Deallocate the memory used by the reconstructed tree Deallocate (Reconstructed_Tree); -- Display the reconstructed tree after deallocation Ada.Text_IO.Put_Line ("The reconstructed tree after deallocation:"); Display (Reconstructed_Tree); end Binary_Tree_Stream;