From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
Subject: Re: binary tree and files
Date: 2000/11/04
Date: 2000-11-04T00:00:00+00:00 [thread overview]
Message-ID: <ok%M5.45808$Q8.8086474@newsrump.sjc.telocity.net> (raw)
In-Reply-To: 8tuk7l$aq5$1@wanadoo.fr
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;
next prev parent reply other threads:[~2000-11-04 0:00 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-11-03 15:06 binary tree and files Franck
2000-11-03 15:20 ` Franck
2000-11-03 15:53 ` Arthur G. Duncan
2000-11-03 16:47 ` Franck
2000-11-03 0:00 ` John Cupak
2000-11-03 0:00 ` Franck
2000-11-03 0:00 ` Pascal Obry
2000-11-03 0:00 ` Franck
2000-11-03 0:00 ` Larry Hazel
2000-11-03 0:00 ` tmoran
2000-11-04 0:00 ` David C. Hoos, Sr.
2000-11-04 0:00 ` David C. Hoos, Sr. [this message]
2000-11-05 1:01 ` Jeff Carter
2000-11-05 0:00 ` David C. Hoos, Sr.
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox