home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog4 / answers / ch23_2.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  5.6 KB  |  190 lines

  1.                          -- Chapter 23 - Programming exercise 2
  2. with Text_IO, Unchecked_Deallocation;
  3. use Text_IO;
  4.  
  5. procedure CH23_2 is
  6.  
  7.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  8.    use Int_IO;
  9.  
  10.    Test_String : constant STRING := "DBCGIF";
  11.    Data_String : constant STRING := "This tests ADA";
  12.  
  13.    type B_TREE_NODE;                 -- Incomplete declaration
  14.  
  15.    type NODE_POINT is access B_TREE_NODE;
  16.  
  17.    type B_TREE_NODE is               -- Complete declaration
  18.       record
  19.          One_Letter      : CHARACTER;
  20.          Character_Count : INTEGER;
  21.          Left            : NODE_POINT;
  22.          Right           : NODE_POINT;
  23.       end record;
  24.  
  25.    pragma CONTROLLED(NODE_POINT);
  26.  
  27.    procedure Free is new Unchecked_Deallocation(B_TREE_NODE,
  28.                                                       NODE_POINT);
  29.  
  30.    Root  : NODE_POINT;    -- Always points to the root of the tree
  31.  
  32.    procedure Final_Traverse_List(Start_Node : NODE_POINT) is
  33.    begin
  34.       if Start_Node.Left /= null then
  35.          Final_Traverse_List(Start_Node.Left);
  36.       end if;
  37.       Put(Start_Node.One_Letter);
  38.       Put(" is character");
  39.       Put(Start_Node.Character_Count,3);
  40.       Put_Line(" in the string.");
  41.       if Start_Node.Right /= null then
  42.          Final_Traverse_List(Start_Node.Right);
  43.       end if;
  44.    end Final_Traverse_List;
  45.  
  46.    procedure Traverse_List(Start_Node : NODE_POINT) is
  47.    begin
  48.       if Start_Node.Left /= null then
  49.          Traverse_List(Start_Node.Left);
  50.       end if;
  51.       Put(Start_Node.One_Letter);
  52.       if Start_Node.Right /= null then
  53.          Traverse_List(Start_Node.Right);
  54.       end if;
  55.    end Traverse_List;
  56.  
  57.    procedure Store_Character(Char_Count : INTEGER;
  58.                              In_Char    : CHARACTER) is
  59.    Temp : NODE_POINT;
  60.  
  61.       procedure Locate_And_Store(Begin_Node : in out NODE_POINT) is
  62.       begin
  63.          if In_Char < Begin_Node.One_Letter then
  64.             if Begin_Node.Left = null then
  65.                Begin_Node.Left := Temp;
  66.             else
  67.                Locate_And_Store(Begin_Node.Left);
  68.             end if;
  69.          else
  70.             if Begin_Node.Right = null then
  71.                Begin_Node.Right := Temp;
  72.             else
  73.                Locate_And_Store(Begin_Node.Right);
  74.             end if;
  75.          end if;
  76.       end Locate_And_Store;
  77.  
  78.    begin
  79.       Temp := new B_TREE_NODE;
  80.          Temp.One_Letter := In_Char; -- New record is now defined
  81.                                      -- The system sets Next_Rec
  82.                                      -- to the value of null
  83.          Temp.Character_Count := Char_Count;
  84.       if Root = null then
  85.          Root := Temp;
  86.       else
  87.          Locate_And_Store(Root);
  88.       end if;
  89.       Put("Ready to traverse list. --->");
  90.       Traverse_List(Root);
  91.       New_Line;
  92.    end Store_Character;
  93.  
  94. begin
  95.             -- Store the characters in Data_String in a Binary Tree
  96.    for Index in Data_String'RANGE loop
  97.       Store_Character(Index,Data_String(Index));
  98.    end loop;
  99.  
  100.             -- Traverse the list
  101.    New_Line;
  102.    Put_Line("Now for the final traversal of Data_String.");
  103.    Final_Traverse_List(Root);
  104.    New_Line(2);
  105.  
  106.    Root := null;    -- Needed to clear out the last tree
  107.  
  108.             -- Store the characters in Test_String in a Binary Tree
  109.    for Index in Test_String'RANGE loop
  110.       Store_Character(Index,Test_String(Index));
  111.    end loop;
  112.  
  113.             -- Traverse the list
  114.    New_Line;
  115.    Put_Line("Now for the final traversal of Test_String.");
  116.    Final_Traverse_List(Root);
  117.    New_Line;
  118.  
  119.             -- Now deallocate the tree
  120.    declare
  121.       procedure Free_Up(Current_Node : in out NODE_POINT) is
  122.       begin
  123.          if Current_Node.Left /= null then
  124.             Free_Up(Current_Node.Left);
  125.          end if;
  126.          if Current_Node.Right /= null then
  127.             Free_Up(Current_Node.Right);
  128.          end if;
  129.          Free(Current_Node);
  130.       end Free_Up;
  131.    begin
  132.       if Root /= null then
  133.          Free_Up(Root);
  134.       end if;
  135.    end;
  136.  
  137. end CH23_2;
  138.  
  139.  
  140.  
  141.  
  142. -- Result of execution
  143. --
  144. -- Ready to traverse list. --->T
  145. -- Ready to traverse list. --->Th
  146. -- Ready to traverse list. --->Thi
  147. -- Ready to traverse list. --->This
  148. -- Ready to traverse list. ---> This
  149. -- Ready to traverse list. ---> Thist
  150. -- Ready to traverse list. ---> Tehist
  151. -- Ready to traverse list. ---> Tehisst
  152. -- Ready to traverse list. ---> Tehisstt
  153. -- Ready to traverse list. ---> Tehissstt
  154. -- Ready to traverse list. --->  Tehissstt
  155. -- Ready to traverse list. --->  ATehissstt
  156. -- Ready to traverse list. --->  ADTehissstt
  157. -- Ready to traverse list. --->  AADTehissstt
  158. --
  159. -- Now for the final traversal of Data_String.
  160. --   is character  5 in the string.
  161. --   is character 11 in the string.
  162. -- A is character 12 in the string.
  163. -- A is character 14 in the string.
  164. -- D is character 13 in the string.
  165. -- T is character  1 in the string.
  166. -- e is character  7 in the string.
  167. -- h is character  2 in the string.
  168. -- i is character  3 in the string.
  169. -- s is character  4 in the string.
  170. -- s is character  8 in the string.
  171. -- s is character 10 in the string.
  172. -- t is character  6 in the string.
  173. -- t is character  9 in the string.
  174.  
  175. -- Ready to traverse list. --->D
  176. -- Ready to traverse list. --->BD
  177. -- Ready to traverse list. --->BCD
  178. -- Ready to traverse list. --->BCDG
  179. -- Ready to traverse list. --->BCDGI
  180. -- Ready to traverse list. --->BCDFGI
  181.  
  182. -- Now for the final traversal of Test_String.
  183. -- B is character  2 in the string.
  184. -- C is character  3 in the string.
  185. -- D is character  1 in the string.
  186. -- F is character  6 in the string.
  187. -- G is character  4 in the string.
  188. -- I is character  5 in the string.
  189.  
  190.