home *** CD-ROM | disk | FTP | other *** search
/ Programmer's ROM - The Computer Language Library / programmersrom.iso / ada / dbase / mims.src < prev   
Encoding:
Text File  |  1988-05-03  |  79.4 KB  |  2,346 lines

  1. --\\BALANCED_TREES_VIS.ADA
  2. --< PACKAGE : Balanced_Trees.
  3. --  AUTHOR : SSgt R. Kirchner
  4. --           SSgt C. Rasmussen
  5. --           Sgt  D. Hamm
  6. --  LAST MODIFIED : 12 Nov 85
  7. --  BY : Lt Brooke
  8. --  CHANGES MADE : Made Position_in_Tree a PRIVATE type (it used to
  9. --                 be a LIMITED type).
  10. --<
  11. --< PACKAGE DESCRIPTION :
  12. --<
  13. --<      Provides the neccessary functions to create, use and
  14. --< maintain balanced-tree indexing.  The trees created by this
  15. --< package must have a minimum node size of Four keys. (Anything
  16. --< less than four will cause errors in Procedure Delete_Key at the
  17. --< present time.)
  18. --<
  19. --<      The package is generic, instantiated for the Type_Definition
  20. --< you want ASSOCIATED with the STRING data stored within the trees.
  21. --< The string data evaluated within the trees are known as KEYS, and
  22. --< the generic parameter type associated with each KEY is called ITEMS.
  23. --< The data is stored as strings because of the need for searching
  24. --< on partial values.  When creating a new_tree, a length of the
  25. --< strings to be stored within that tree must be specified.  All
  26. --< values inserted into or deleted from that tree must be of that
  27. --< length, and searching for a value of greater length is not allowed.
  28. --< The Exception KEY_LENGTH_ERROR is raised in such cases.
  29. --<
  30. --< PACKAGE DEPENDENCIES :
  31. --<
  32. --<      None.
  33. --<
  34. generic
  35.   type Items is private;
  36.   No_Of_Keys : Positive := 7;
  37.     -- Must not be less than four or Program_Error is raised.
  38.  
  39. package Balanced_Trees is
  40.  
  41.   subtype Keys  is String;
  42.  
  43.   type Trees            is private;
  44.   type Position_In_Tree is private;
  45.  
  46.  
  47.   Key_Length_Error   : exception;
  48.   Key_Not_Found      : exception;
  49.   Key_Already_Exists : exception;
  50.   End_Of_Tree        : exception;
  51.  
  52.   function New_Tree (With_Key_Length : Integer) return Trees;
  53.         -- DESCRIPTION : Creates a tree for strings of KEY_LENGTH.
  54.         -- INPUTS      : With_Key_Length - length of strings.
  55.         -- OUTPUTS     : Trees type pointing to the newly createed tree.
  56.         -- EXCEPTIONS  : None.
  57.  
  58.   procedure Delete_Tree (Tree : in out Trees);
  59.         -- DESCRIPTION : Deletes a tree and deallocates the space
  60.         --               previously used by that tree.
  61.         -- INPUTS      : Tree - tree to be deleted.
  62.         -- OUTPUTS     : Null Trees type.
  63.         -- EXCEPTIONS  : None.
  64.  
  65.   function Item_Where (Key_Is : Keys; In_Tree : Trees) return Items;
  66.         -- DESCRIPTION : Returns the item associated to the first
  67.         --               instance (partial Key searches) of the
  68.         --               Key in a Tree.
  69.         -- INPUTS      : In_Tree - The tree to be searched.
  70.         --               Key_Is - The key to be searched for.
  71.         -- OUTPUTS     : The keys related item (if key is found).
  72.         -- EXCEPTIONS  : Key_Length_Error when Using_Key is of greater
  73.         --               length than strings stored in tree.
  74.         --               Key_Not_Found when Using_Key is not found.
  75.  
  76.   procedure Delete_Key (With_Value : Keys; From : Trees);
  77.         -- DESCRIPTION : Deletes a key from a tree.
  78.         -- INPUTS      : With_Value - Key to be deleted.
  79.         --               From - Tree where key is to be deleted.
  80.         -- OUTPUTS     : None.
  81.         -- EXCEPTIONS  : Key_Length_Error when Key_Value is not the same
  82.         --               length as keys stored in tree.
  83.         --               Key_Not_Found when Key_Value is not found.
  84.  
  85.   procedure Insert (Key_Value : Keys; And_Item : Items; Into : Trees);
  86.         -- DESCRIPTION : Inserts a key into a tree.
  87.         -- INPUTS      : Key_Value - Key to be inserted.
  88.         --               And_Item - Item to be associated with Key_Value
  89.         --               Into - Tree where Key_Value is inserted.
  90.         -- OUTPUTS     : None.
  91.         -- EXCEPTIONS  : Key_Length_Error when Key_Value is not the same
  92.         --               length as keys stored in tree.
  93.         --               Key_Already_Exists when Key_Value already
  94.         --               exists in the tree.
  95.  
  96.   procedure Get_First (From            : Trees;
  97.                Giving_Position : out Position_In_Tree;
  98.                Giving_Item     : out Items);
  99.         -- DESCRIPTION : Returns the FIRST position within a tree
  100.         --               and the item at that position.
  101.         -- INPUTS      : From - Tree you want the first
  102.         --               position/item from.
  103.         -- OUTPUTS     : Giving_Position - First Position_In_Tree.
  104.         --               Giving_Item - Item associated with the
  105.         --               Giving_Position.
  106.         -- EXCEPTIONS  : End_Of_Tree when tree has no entries.
  107.  
  108.   procedure Get_First_Key (From            : Trees;
  109.                With_Value      : Keys;
  110.                Giving_Position : out Position_In_Tree;
  111.                Giving_Item     : out Items);
  112.         -- DESCRIPTION : Returns the position/item assosiated with the
  113.         --               first instance of With_Value.
  114.         -- INPUTS      : From - Tree you want the position/item from.
  115.         --               With_Value - Key you are looking for the
  116.         --               first instance of.
  117.         -- OUTPUTS     : Giving_Position - Position_In_Tree of
  118.         --               With_Value.
  119.         --               And_Item - Item associated with the Key.
  120.         -- EXCEPTIONS  : Key_Length_Error when Key_Value is of greater
  121.         --               length than values stored in tree.
  122.         --               Key_Not_Found when With_Value is not found.
  123.  
  124.   procedure Get_Last (From            : Trees;
  125.               Giving_Position : out Position_In_Tree;
  126.               Giving_Item     : out Items);
  127.         -- DESCRIPTION : Returns the LAST position within a tree
  128.         --               and the item at that position.
  129.         -- INPUTS      : From - Tree you want the last
  130.         --               position/item from
  131.         -- OUTPUTS     : Giving_Position - Last Position_In_Tree.
  132.         --               And_Item - Item associated with the
  133.         --               Giving_Position.
  134.         -- EXCEPTIONS  : End_Of_Tree when tree has no entries.
  135.  
  136.   procedure Get_Last_Key (From            : Trees;
  137.               With_Value      : Keys;
  138.               Giving_Position : out Position_In_Tree;
  139.               Giving_Item     : out Items);
  140.         -- DESCRIPTION : Returns the position/item associated with the
  141.         --               last instance of With_Value.
  142.         -- INPUTS      : From - Tree you want position/item from.
  143.         --               With_Value - Key you are looking for the
  144.         --               last instance of.
  145.         -- OUTPUTS     : Giving_Position - Position_In_Tree of
  146.         --               With_Value.
  147.         --               Giving_Item - Item associated with the key.
  148.         -- EXCEPTIONS  : Key_Length_Error when Key_Value is of greater
  149.         --               length than keys stored in tree.
  150.         --               Key_Not_Found when Key_Value is not found.
  151.  
  152.   procedure Get_Next (From        : in out Position_In_Tree;
  153.               Giving_Item : out Items);
  154.         -- DESCRIPTION : Allows you to get the NEXT sequential position
  155.         --               in a tree and the item at that position.
  156.         -- INPUTS      : From - your current position within a tree.
  157.         -- OUTPUTS     : From - the NEXT position in the tree.
  158.         --               Giving_Item  - The item at the NEXT position.
  159.         -- EXCEPTIONS  : End_Of_Tree when there is not another position.
  160.  
  161.   procedure Get_Prior (From        : in out Position_In_Tree;
  162.                Giving_Item : out Items);
  163.         -- DESCRIPTION : Allows you to get the PRIOR sequential position
  164.         --               in a tree and the item at that position.
  165.         -- INPUTS      : From - Your current position within a tree.
  166.         -- OUTPUTS     : From - The PRIOR position in the tree.
  167.         --               Giving_Item  - The item at the PRIOR position.
  168.         -- EXCEPTIONS  : End_Of_Tree when there is not a previous position.
  169.  
  170.   procedure Change_Item (For_Key : Keys; In_Tree : Trees; To : Items);
  171.         -- DESCRIPTION : Changes the item associated with a key.
  172.         -- INPUTS      : For_Key - Key of item to be changed.
  173.         --               In_Tree - Tree where item is to be changed.
  174.         --               To - New item to replace the old item.
  175.         -- OUTPUTS     : None.
  176.         -- EXCEPTIONS  : Key_Length_Error when Key_Value is not the same
  177.         --               length as keys stored in tree.
  178.         --               Key_Not_Found when Key_Value is not found.
  179.  
  180.   procedure Change_Item (At_Position : Position_In_Tree; To : Items);
  181.         -- DESCRIPTION : Changes the item at a certain tree_position.
  182.         -- INPUTS      : At_Position - Position of item to be changed.
  183.         --               To - New item to replace the old item.
  184.         -- OUTPUTS     : None.
  185.         -- EXCEPTIONS  : None.
  186.  
  187.   function Key_At (Position : Position_In_Tree) return Keys;
  188.         -- DESCRIPTION : Returns the key located at a position within
  189.         --               a tree.
  190.         -- INPUTS      : Position - Position where the key is at.
  191.         -- OUTPUTS     : The key at that position.
  192.         -- EXCEPTIONS  : None.
  193.  
  194.   function Item_At (Position : Position_In_Tree) return Items;
  195.         -- DESCRIPTION : Returns the item located at a position within
  196.         --               a tree.
  197.         -- INPUTS      : Position - Position where the item is at.
  198.         -- OUTPUTS     : The item at that position.
  199.         -- EXCEPTIONS  : None.
  200.  
  201.   function Inclusive_Subtree (Of_Tree  : Trees;
  202.                   From_Key : Keys;
  203.                   To_Key   : Keys) return Trees;
  204.         -- DESCRIPTION : Creates a tree from part of another tree of
  205.         --               everythig between two keys (INCLUSIVE).
  206.         -- INPUTS      : Of_Tree - Initial tree.
  207.         --               From_Key - Starting key of new tree
  208.         --               To_Key - Ending key of new tree.
  209.         -- OUTPUTS     : Trees type pointing to the new tree.
  210.         -- EXCEPTIONS  : Key_Length_Error when either keys are greater
  211.         --               length than keys stored in the original tree.
  212.  
  213.   function Exclusive_Subtree (Of_Tree  : Trees;
  214.                   From_Key : Keys;
  215.                   To_Key   : Keys) return Trees;
  216.         -- DESCRIPTION : Creates a tree from part of another tree of
  217.         --               everything between two keys (EXCLUSIVE).
  218.         -- INPUTS      : Of_Tree - Initial tree.
  219.         --               From_Key - Starting key of new tree
  220.         --               To_Key - Ending key of new tree.
  221.         -- OUTPUTS     : New tree.
  222.         -- EXCEPTIONS  : Key_Length_Error when either keys are greater
  223.         --               length than keys stored in the original tree.
  224.  
  225. private
  226.   type Acc_Keys  is access Keys;
  227.     -- Pointer to a Key.
  228.   type Acc_Items is access Items;
  229.     -- Pointer to a Item.
  230.   type Datas is
  231.     record
  232.       Key  : Acc_Keys;
  233.       Item : Acc_Items;
  234.     end record;
  235.       -- A record containing a matched pair of a Key and a Item.
  236.   type Acc_Data   is access Datas;
  237.     -- A pointer to a Datas containg the Key and Item
  238.   type Tree_Array is array (1 .. No_Of_Keys + 1) of Trees;
  239.     -- An Array of pointers.
  240.   type Data_Array is array (1 .. No_Of_Keys) of Acc_Data;
  241.     -- An Array of pointers to Datas.
  242.   type Node;
  243.   type Trees is access Node;
  244.     -- A pointer to a Node.
  245.   type Node is
  246.     record
  247.       Mother  : Trees;
  248.       Pointer : Tree_Array;
  249.       Data    : Data_Array;
  250.     end record;
  251.       -- A record containing a pointer to the parent Node, an array
  252.       -- of pointers to lower Nodes and an array of pointers to the
  253.       -- Data within the Node.
  254.   type Position_In_Tree is
  255.     record
  256.       Node_In_Tree     : Trees;
  257.       Position_In_Node : Integer;
  258.     end record;
  259.       -- A record containing a pointer to a node and a position
  260.       -- within that node.
  261. end Balanced_Trees;
  262. --\\HaMM.ADA
  263. package Source_Scanner is
  264.   Scanner_Quote       : constant Character := '"';
  265.   Scanner_Under_Score : constant Character := '_';
  266.  
  267.   Scanner_End_Of_File : exception;
  268.  
  269.   subtype Columns is Integer range 1 .. 81;
  270.  
  271.   type Symbols is (Id, Op, Literal, End_Of_Input);
  272.  
  273.   Input_Symbol         : Symbols;
  274.   Scanner_All_Capitals : Boolean := True;
  275.   Token_Upto           : Columns := Columns'First;
  276.   Token80              : String (Columns) := (Columns => ' ');
  277.  
  278.   procedure Get_Next_Token;
  279.   procedure Initialize_Scanner (File_Name : String);
  280.  
  281. end Source_Scanner;
  282.  
  283. with Text_Io;
  284.  
  285. package body Source_Scanner is
  286.  
  287.   subtype Lines is String (1 .. 80);
  288.  
  289.   Upto             : Integer; -- col within row, or row in file
  290.   Line             : Lines;
  291.   Next_Label       : Integer := 100;
  292.   Current          : Character;
  293.   Scanner_File     : Text_Io.File_Type;
  294.   Last             : Integer;
  295.   Still_More_Stuff : Boolean := True;
  296.  
  297.  
  298.   function Capitals_Of (S : String) return String is
  299.     Temp : String (S'range) := S;
  300.   begin
  301.     for A in Temp'range loop
  302.       case Temp (A) is
  303.         when 'a' .. 'z' =>
  304.       Temp (A) := Character'Val (Character'Pos (Temp (A)) - 32);
  305.  
  306.         when others =>
  307.       null;
  308.       end case;
  309.     end loop;
  310.  
  311.     return Temp;
  312.   end Capitals_Of;
  313.  
  314.  
  315.   function String_Of (Message      : String;
  316.                       Until_Length : Integer) return String is
  317.   begin
  318.     if Message'Length >= Until_Length then
  319.       return Message (Message'First .. Message'First + Until_Length - 1);
  320.     else
  321.       return Message & (Message'Length + 1 .. Until_Length => ' ');
  322.     end if;
  323.   end String_Of;
  324.  
  325.  
  326.   procedure Get_Next_Token is
  327.  
  328.     type Scanner_States is
  329.      (Start,             Ident_String,      Ident_Under_Score,
  330.       Digit_String,      Digit_Under_Score, Decimal_Point,
  331.       Decimal_Part,      Literal_String,    Quotes,
  332.       Multi_Delims,      Accept_Token,      Commenting);
  333.  
  334.     State : Scanner_States := Start;
  335.  
  336.     Lex_Error                  : exception;
  337.     End_Of_Stuff               : exception;
  338.     Couldnt_Get_Next_Character : exception;
  339.     Inconsistent_Double_Op     : exception;
  340.  
  341.  
  342.     procedure Condense is
  343.     begin
  344.       Token80 (Token_Upto) := Current;
  345.       Token_Upto := Token_Upto + 1;
  346.     end Condense;
  347.  
  348.  
  349.     procedure Get_Next_Line is
  350.     begin
  351.       Line := (others => ' ');
  352.       Text_Io.Get_Line (Scanner_File, Line, Last);
  353.       Upto := 0;
  354.       Current := ' ';
  355.     exception
  356.       when Text_Io.End_Error => 
  357.     Text_Io.Close (Scanner_File);
  358.     Current := ' ';
  359.     Line := (others => ' ');
  360.     Upto := Line'Last;
  361.     Still_More_Stuff := False;
  362.     end Get_Next_Line;
  363.  
  364.  
  365.     procedure Get_Next_Character is
  366.     begin
  367.       if Upto = 0 then
  368.     Upto := Line'First;
  369.       else
  370.     Upto := Upto + 1;
  371.  
  372.     if State = Start then
  373.       while Line (Upto) in Character'Val (1) .. ' ' loop
  374.         Upto := Upto + 1;
  375.       end loop;
  376.     end if;
  377.       end if;
  378.  
  379.       Current := Line (Upto);
  380.     exception
  381.       when Program_Error =>  raise;
  382.  
  383.       when Constraint_Error => 
  384.     if Still_More_Stuff then
  385.       Get_Next_Line;
  386.     else
  387.       raise End_Of_Stuff;
  388.     end if;
  389.  
  390.       when others =>  raise Couldnt_Get_Next_Character;
  391.     end Get_Next_Character;
  392.   begin
  393.     Token_Upto := Columns'First;
  394.  
  395.     loop
  396.       case State is
  397.     when Start => 
  398.       case Current is
  399.         when 'a' .. 'z' | 'A' .. 'Z' => 
  400.           State := Ident_String;
  401.  
  402.         when '0' .. '9' => 
  403.           State := Digit_String;
  404.  
  405.         when '"' => 
  406.           State := Literal_String;
  407.           Get_Next_Character;
  408.  
  409.         when '-' | '<' | '>' | '=' | '*' | ':' | '/' | '.' | '+' => 
  410.           State := Multi_Delims;
  411.  
  412.         when Character'Val (1) .. ' ' => 
  413.           Get_Next_Character;
  414.  
  415.         when others =>  -- is a single delimiter
  416.           Condense;
  417.           Input_Symbol := Op;
  418.           State := Accept_Token;
  419.           Get_Next_Character;
  420.       end case;
  421.  
  422.     when Ident_String => 
  423.       case Current is
  424.         when 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' => 
  425.           Condense;
  426.           Get_Next_Character;
  427.  
  428.         when Scanner_Under_Score => 
  429.           Condense;
  430.           Get_Next_Character;
  431.           State := Ident_Under_Score;
  432.  
  433.         when others => 
  434.           if Scanner_All_Capitals then
  435.         Token80 (1 .. Token_Upto) :=
  436.           Capitals_Of (Token80 (1 .. Token_Upto));
  437.           end if;
  438.  
  439.           Input_Symbol := Id;
  440.           State := Accept_Token;
  441.       end case;
  442.  
  443.     when Ident_Under_Score => 
  444.       case Current is
  445.         when 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' => 
  446.           State := Ident_String;
  447.  
  448.         when others => 
  449.           raise Lex_Error;
  450.       end case;
  451.  
  452.     when Digit_String => 
  453.       case Current is
  454.         when '0' .. '9' => 
  455.           Condense;
  456.           Get_Next_Character;
  457.  
  458.         when Scanner_Under_Score => 
  459.           Condense;
  460.           Get_Next_Character;
  461.           State := Digit_Under_Score;
  462.  
  463.         when '.' => 
  464.           Get_Next_Character;
  465.           State := Decimal_Point;
  466.  
  467.         when others => 
  468.           Input_Symbol := Literal;
  469.           State := Accept_Token;
  470.       end case;
  471.  
  472.     when Digit_Under_Score => 
  473.       case Current is
  474.         when '0' .. '9' => 
  475.           State := Digit_String;
  476.  
  477.         when others => 
  478.           raise Lex_Error;
  479.       end case;
  480.  
  481.     when Decimal_Point => 
  482.       case Current is
  483.         when '.' => 
  484.           State := Accept_Token;
  485.           Input_Symbol := Literal;
  486.           Line := String_Of ("..", Line'Length);
  487.           Upto := 0;
  488.           Get_Next_Character;
  489.  
  490.         when '0' .. '9' => 
  491.           Token80 (Token_Upto) := '.';
  492.           Token_Upto := Token_Upto + 1;
  493.           State := Decimal_Part;
  494.  
  495.         when others => 
  496.           raise Lex_Error;
  497.       end case;
  498.  
  499.     when Decimal_Part => 
  500.       case Current is
  501.         when '0' .. '9' => 
  502.           Condense;
  503.           Get_Next_Character;
  504.  
  505.         when others => 
  506.           Input_Symbol := Literal;
  507.           State := Accept_Token;
  508.       end case;
  509.  
  510.     when Literal_String => 
  511.       case Current is
  512.         when Scanner_Quote => 
  513.           Get_Next_Character;
  514.           State := Quotes;
  515.  
  516.         when others => 
  517.           Condense;
  518.           Get_Next_Character;
  519.       end case;
  520.  
  521.     when Quotes => 
  522.       case Current is
  523.         when Scanner_Quote => 
  524.           Condense;
  525.           Get_Next_Character;
  526.           State := Literal_String;
  527.  
  528.         when others => 
  529.           Input_Symbol := Literal;
  530.           State := Accept_Token;
  531.       end case;
  532.  
  533.     when Multi_Delims => 
  534.       case Current is
  535.         when '-' => 
  536.           Get_Next_Character;
  537.  
  538.           if Current = '-' then
  539.         State := Commenting;
  540.           else
  541.         Token80 (1) := '-';
  542.         Token_Upto := 2;
  543.         State := Accept_Token;
  544.           end if;
  545.  
  546.         when '>' | '/' => 
  547.           Condense;
  548.           Get_Next_Character;
  549.  
  550.           if Current = '=' then
  551.         Condense;
  552.         Get_Next_Character;
  553.           end if;
  554.  
  555.           State := Accept_Token;
  556.  
  557.         when '<' => 
  558.           Condense;
  559.           Get_Next_Character;
  560.  
  561.           if (Current = '>') or (Current = '=') then
  562.         Condense;
  563.         Get_Next_Character;
  564.           end if;
  565.  
  566.           State := Accept_Token;
  567.  
  568.         when ':' => 
  569.           Condense;
  570.           Get_Next_Character;
  571.  
  572.           case Current is
  573.         when '=' => 
  574.           Condense;
  575.           Get_Next_Character;
  576.  
  577.         when ':' => 
  578.           Condense;
  579.           Get_Next_Character;
  580.  
  581.           if Current = '=' then
  582.             Condense;
  583.             Get_Next_Character;
  584.           end if;
  585.  
  586.         when others =>  null;
  587.           end case;
  588.  
  589.           State := Accept_Token;
  590.  
  591.         when '=' => 
  592.           Condense;
  593.           Get_Next_Character;
  594.  
  595.           if Current = '>' then
  596.         Condense;
  597.         Get_Next_Character;
  598.           end if;
  599.  
  600.           State := Accept_Token;
  601.  
  602.         when '.' => 
  603.           Condense;
  604.           Get_Next_Character;
  605.  
  606.           if Current = '.' then
  607.         Condense;
  608.         Get_Next_Character;
  609.           end if;
  610.  
  611.           State := Accept_Token;
  612.  
  613.         when '+' => 
  614.           Condense;
  615.           Get_Next_Character;
  616.  
  617.           if Current = '*' then
  618.         Condense;
  619.         Get_Next_Character;
  620.           end if;
  621.  
  622.           State := Accept_Token;
  623.  
  624.         when '*' => 
  625.           Condense;
  626.           Get_Next_Character;
  627.  
  628.           case Current is
  629.         when '*' | '+' => 
  630.           Condense;
  631.           Get_Next_Character;
  632.  
  633.         when others =>  null;
  634.           end case;
  635.  
  636.           State := Accept_Token;
  637.  
  638.         when others =>  raise Inconsistent_Double_Op;
  639.       end case;
  640.  
  641.       Input_Symbol := Op;
  642.  
  643.     when Commenting => 
  644.       State := Start;
  645.       Get_Next_Line;
  646.  
  647.     when Accept_Token => 
  648.       Token_Upto := Token_Upto - 1;
  649.       exit;
  650.       end case;
  651.     end loop;
  652.   exception
  653.     when Scanner_End_Of_File =>  raise;
  654.  
  655.     when Lex_Error => 
  656.       Text_Io.Put_Line ("***");
  657.       Text_Io.Put_Line ("->" & Current & "<- cannot follow =>" &
  658.             Token80 (1 .. Token_Upto - 1) & "<=");
  659.       raise;
  660.  
  661.     when End_Of_Stuff => 
  662.       if Input_Symbol = End_Of_Input then
  663.     raise Scanner_End_Of_File;
  664.       else
  665.     Token80 (1) := '|';
  666.     Token_Upto := 1;
  667.     Input_Symbol := Symbols'(End_Of_Input);
  668.       end if;
  669.  
  670.     when Constraint_Error =>  -- Probably token_upto = 0.
  671.       Token_Upto := 1;
  672.       Token80 (1) := ' ';
  673.       Input_Symbol := Literal;
  674.   end Get_Next_Token;
  675.  
  676.  
  677.   procedure Closing_Procedure_Test is
  678.   begin
  679.     Text_Io.Close (Scanner_File);
  680.   exception
  681.     when others =>  null;
  682.   end Closing_Procedure_Test;
  683.  
  684.  
  685.   procedure Initialize_Scanner (File_Name : String) is
  686.     This_File : Text_Io.File_Type;
  687.   begin
  688.     Closing_Procedure_Test;
  689.     Text_Io.Open (Scanner_File, Text_Io.In_File, File_Name);
  690.     Line := (others => ' ');
  691.     Text_Io.Get_Line (Scanner_File, Line, Last);
  692.     Upto := 0;
  693.     Current := ' ';
  694.     Still_More_Stuff := True;
  695.     Input_Symbol := Literal;
  696.     Token80 := (others => ' ');
  697.     Token_Upto := 1;
  698.   exception
  699.     when Text_Io.End_Error => 
  700.       Text_Io.Close (Scanner_File);
  701.       Current := ' ';
  702.       Line := (others => ' ');
  703.       Upto := Line'Last;
  704.       Still_More_Stuff := False;
  705.   end Initialize_Scanner;
  706.  
  707. end Source_Scanner;
  708. --\\VARIABLE_LISTS.ADA
  709. -- last modified : 15 Nov 85
  710. -- by            : TCB
  711. -- changes made  : Added final touches to documentation.
  712. --
  713. --
  714. with Unchecked_Deallocation,
  715.      Text_Io;  -- This can be pulled if Self_Test is also pulled.
  716.  
  717. package body Variable_Lists is
  718. --
  719. --
  720. --
  721. -- Some notes on implementation:
  722. --
  723. -- The primary author took the cheap way out.  In order to avoid
  724. -- constantly checking to see if there are any blocks at all in a
  725. -- particular list, he insures that there is ALWAYS exactly ONE
  726. -- empty block at the end of a list. e.g., if the block size is 5,
  727. -- then when a list is declared, it will start with 0 Elements, but
  728. -- one block of 5 empty slots. As Elements are added, when this last
  729. -- empty block is used, a new empty block is added; so when item # 1
  730. -- is added to the list, it is put in slot number 1 of the "old" last
  731. -- block and a "new" last block is created. So there are 10 slots, only
  732. -- the first of which is filled. As more Elements are added, it is not
  733. -- until the last block is again touched (Element # 6) that a new block
  734. -- is again allocated.  Similarly, as Elements are deleted, it is not
  735. -- until there are TWO blank blocks at the end that one is deallocated.
  736. -- However, since we always keep 1 blank one there, as soon as another
  737. -- one BECOMES empty we know we can deallocate the last block.
  738. -- This technique may waste a little space, but it makes everyday use
  739. -- of lists run much more efficiently.
  740. --
  741. -- It may be expedient to include "Total_Elements : natural := 0" as
  742. -- a component of the Lists type in the private part, and maitain this
  743. -- value in procedures Add, Remove, and Empty.  This will make Item_At
  744. -- and Number_In run much more efficiently.  However, this redundancy
  745. -- of information may cause inconsistencies if there is an abnormal
  746. -- termination.
  747. --
  748. --
  749.  
  750.   procedure Flush is new Unchecked_Deallocation
  751.        (Object => List_Block, Name => Block_Ptr);
  752.  
  753.  
  754.   procedure Deallocate (Ptr : in out Block_Ptr) is
  755.     -- This is needed because Unchecked_Deallocation of a NULL
  756.     -- access object raises nasty errors in the ROLM / DG  ADE.
  757.   begin
  758.     if Ptr /= null then
  759.       Flush (Ptr);
  760.     end if;
  761.   end Deallocate;
  762.  
  763.  
  764.   function Block_Number (N : Positive; Within : Lists) return Block_Ptr is
  765.     List    : Lists renames Within;
  766.     Current : Block_Ptr;
  767.   begin
  768.     Current := List.First_Block;
  769.  
  770.     for Block in 2 .. N loop
  771.       Current := Current.Next;
  772.     end loop;
  773.  
  774.     return Current;
  775.   exception
  776.     when Constraint_Error =>  raise Arent_That_Many_Elements;
  777.   end Block_Number;
  778.  
  779.  
  780.   function Block_Of (N : Positive) return Positive is
  781.   begin
  782.     return ((N - 1) / Block_Size) + 1;
  783.   end Block_Of;
  784.  
  785.  
  786.   function Last_Block_In (List : Lists) return Block_Ptr is
  787.   begin
  788.     return Block_Number (List.Number_Blocks, Within => List);
  789.   end Last_Block_In;
  790.  
  791.  
  792.   procedure Reclaim (From_Here : in out Block_Ptr) is
  793.     -- Recovers ALL the blocks from this point on, not just
  794.     -- the one pointed to in FROM_HERE.
  795.     Current_Block, Next_Block : Block_Ptr;
  796.   begin
  797.     Current_Block := From_Here;
  798.     Next_Block := Current_Block.Next;
  799.  
  800.     while Next_Block /= null loop
  801.       Deallocate (Current_Block);
  802.       Current_Block := Next_Block;
  803.       Next_Block := Next_Block.Next;
  804.     end loop;
  805.  
  806.     Deallocate (Current_Block);
  807.     From_Here := null;
  808.   exception
  809.     when Constraint_Error =>  -- From_Here is ALREADY null, dummy.
  810.       null;
  811.   end Reclaim;
  812.  
  813.  
  814.   function Row_Of (N : Positive) return Positive is
  815.   begin
  816.     return ((N - 1) mod Block_Size) + 1;
  817.   end Row_Of;
  818.  
  819.  
  820.   procedure Add (Item : Element; Onto : in out Lists) is
  821.     Last_Block : Block_Ptr;
  822.     List       : Lists renames Onto;
  823.     Last_Entry : Count_Range renames List.Last_Block_Upto;
  824.   begin
  825.     Last_Block := Last_Block_In (List);
  826.     Last_Entry := Last_Entry + 1; -- this gets a C_E if we try to put
  827.                                   -- something in the last slot of a
  828.                                   -- block.
  829.     Copy (Item, Into => Last_Block.Item (Last_Entry));
  830.   exception
  831.     when Constraint_Error =>
  832.               -- filled up the block. need a new one.
  833.       Copy (Item, Into => Last_Block.Item (Block_Size));
  834.       Last_Block.Next := new List_Block;
  835.       Last_Entry := 0;
  836.       List.Number_Blocks := List.Number_Blocks + 1;
  837.   end Add;
  838.  
  839.  
  840.   procedure Copy (Value : Lists; Into : in out Lists) is
  841.     Source_List : Lists renames Value;
  842.     Destination : Lists renames Into;
  843.   begin
  844.     Empty (Destination);
  845.  
  846.     for Current in 1 .. Number_In (Source_List) loop
  847.       Add (Item_At (Current, Within => Source_List), Onto => Destination);
  848.     end loop;
  849.   end Copy;
  850.  
  851.  
  852.   procedure Empty (List : in out Lists) is
  853.   begin
  854.     List.Last_Block_Upto := 0;
  855.     List.Number_Blocks := 1;
  856.     Reclaim (From_Here => List.First_Block.Next);
  857.   end Empty;
  858.  
  859.  
  860.   function Item_At (Number : Positive; Within : Lists) return Element is
  861.     List : Lists renames Within;
  862.   begin
  863.     if Number <= Number_In( List ) then
  864.       return Block_Number (Block_Of (Number), Within => List).Item
  865.           (Row_Of (Number));
  866.     else
  867.       raise Arent_That_Many_Elements;
  868.     end if;
  869.   end Item_At;
  870.  
  871.  
  872.   function Number_In (List : Lists) return Natural is
  873.   begin
  874.     return (List.Number_Blocks - 1) * Block_Size + List.Last_Block_Upto;
  875.   end Number_In;
  876.  
  877.  
  878.   procedure Remove (Number : Positive; From : in out Lists) is
  879.     List       : Lists renames From;
  880.     Last_Block : Block_Ptr;
  881.   begin
  882.     if Number <= Number_In (List) then
  883.       for Current_Item in Number + 1 .. Number_In (List) loop
  884.     Replace (Current_Item - 1, Within => List,
  885.          By => Item_At (Current_Item, Within => List));
  886.       end loop;
  887.  
  888.       List.Last_Block_Upto := List.Last_Block_Upto - 1;
  889.     else
  890.       raise Arent_That_Many_Elements;
  891.     end if;
  892.   exception
  893.     when Constraint_Error =>
  894.                -- now have an empty block.
  895.       List.Last_Block_Upto := Count_Range'Last;
  896.       Last_Block := Last_Block_In (List);
  897.       Reclaim (Last_Block);
  898.       List.Number_Blocks := List.Number_Blocks - 1;
  899.       Last_Block_In (List).Next := null;
  900.   end Remove;
  901.  
  902.  
  903.   procedure Replace (Number : Positive;
  904.              Within : in out Lists;
  905.              By     : Element) is
  906.     List  : Lists renames Within;
  907.     Value : Element renames By;
  908.   begin
  909.     if Number <= Number_In ( List ) then
  910.       Copy (Value,
  911.         Into =>
  912.           Block_Number (Block_Of (Number), Within => List).Item
  913.              (Row_Of (Number)));
  914.     else
  915.       raise Arent_That_Many_Elements;
  916.     end if;
  917.   end Replace;
  918.  
  919.   procedure Self_Test is
  920.     List         : Lists;
  921.     Error_Caught : Boolean := False;
  922.  
  923.     procedure Error_Is (Message : String) is
  924.     begin
  925.       Text_Io.Put_Line ("*** Variable_lists Self Test : " & Message);
  926.       Error_Caught := True;
  927.     end Error_Is;
  928.  
  929.   begin
  930.     if Number_In (List) /= 0 then
  931.       Error_Is ("Number_In not 0");
  932.  
  933.     elsif List.Number_Blocks /= 1 then
  934.       Error_Is ("Number of Blocks not 1");
  935.  
  936.     elsif List.First_Block = null then
  937.       Error_Is ("First_Block is null");
  938.     end if;
  939.  
  940.     if Error_Caught then
  941.       raise Program_Error;
  942.     end if;
  943.   end Self_Test;
  944.  
  945. begin
  946.   Self_Test;
  947. end Variable_Lists;
  948. --\\VARIABLE_LISTS_VIS.ADA
  949. --< PACKAGE :        Variable_Lists
  950. --  author        : Thomas C. Brooke
  951. --  created on    : 3 Jul 85
  952. --  last modified : 5 Jul 85
  953. --  by            : TCB
  954. --<
  955. --< PACKAGE DESCRIPTION:
  956. --<
  957. --< This package provides true variable length lists.
  958. --<
  959. --< The items in a list are ordered and are given an ordinal number
  960. --< starting at 1;  e.g. the first item added to a list is item # 1,
  961. --< the second is item # 2, etc. Items can be retrieved directly by
  962. --< their position number: >>> Item_At( 45, within => list_a ) <<<.
  963. --< The length of a list is the Number_In that list, so iteration
  964. --< can occur: >>> 1 .. Number_In( list_b ) <<< will reach every element
  965. --< in list_b.  Items may be removed (although this process is in-
  966. --< efficient). Lists may be emptied, this process is efficient.
  967. --< Any item can be directly replaced by another element.
  968. --<
  969. --< Note that the implementation is a linked list of blocks.  This
  970. --< makes it a relatively efficient infinite length, but there is
  971. --< still direct retrieval of any single element.  The user may
  972. --< specify the size of blocks as best fits his needs, if he so
  973. --< desires.
  974. --<
  975. --< Lists are created merely by declaring an object of type Lists.
  976. --< This new list is initially empty, so no explicit initialization
  977. --< is required.
  978. --<
  979. --< Choosing the Block_By size may greatly affect performance.  The
  980. --< larger Block_By is, the fewer links have to be traversed to get
  981. --< to elements near the end of the list; however, a good deal more
  982. --< space is wasted on the last partially full block.  A smaller
  983. --< Block_By will waste less space, but may slow down processing in
  984. --< the back end of the lists.
  985. --<
  986. --< PACKAGE DEPENDENCIES : none.
  987. --
  988. --
  989. --
  990. generic
  991.   type Element is limited private;
  992.     -- creates lists of Element, where Element can be any
  993.         -- CONSTRAINED subtype.  Note that this specifically
  994.         -- prohibits variant records, even with a default
  995.         -- descriminant value.
  996.  
  997.   with procedure Copy (Value : Element; Into : in out Element);
  998.     -- how to copy one Element into another.
  999.  
  1000.   Block_By : Positive := 50;
  1001.     -- what size to make blocks of Elements.
  1002.  
  1003. package Variable_Lists is
  1004.  
  1005.   type Lists is limited private; -- because it may use access types.
  1006.                                  -- and we may have Lists of Lists.
  1007.  
  1008.   Arent_That_Many_Elements : exception;
  1009.  
  1010.   procedure Add       (Item : Element; Onto : in out Lists);
  1011.     -- DESCRIPTION : Adds ITEM to the list ONTO.
  1012.     -- INPUTS      : Item - The Element to be added to a list.
  1013.     --               Onto - The list to which ITEM is added.
  1014.     -- OUTPUTS     : Onto - the new list with the ITEM appended
  1015.     -- EXCEPTIONS  : should never raise any.
  1016.  
  1017.   procedure Copy      (Value : Lists; Into : in out Lists);
  1018.     -- DESCRIPTION : Empties the destination list INTO, then
  1019.     --               copies the contents of VALUE into INTO.
  1020.     -- INPUTS      : Value - the source list to copy
  1021.     --               Into  - the destination where it is copied into
  1022.     -- OUTPUTS     : Into  - the resulting list.
  1023.     -- EXCEPTIONS  : should not raise any.
  1024.  
  1025.   procedure Empty     (List : in out Lists);
  1026.     -- DESCRIPTION : Empties the LIST
  1027.     -- INPUTS      : List - the list to be emptied.
  1028.     -- OUTPUTS     : List - the resulting empty list.
  1029.     -- EXCEPTIONS  : should never raise any.
  1030.  
  1031.   function  Item_At   (Number : Positive; Within : Lists) return Element;
  1032.     -- DESCRIPTION : Returns the Element at position NUMBER in the
  1033.     --               list WITHIN.
  1034.     -- INPUTS      : Number - the ordinal position of the Element.
  1035.     --               Within - the list from which it is retrieved.
  1036.     -- OUTPUTS     : the Element at that position.
  1037.     -- EXCEPTIONS  : Arent_That_Many_Elements when NUMBER is
  1038.     --               greater than the Number_In the list.
  1039.  
  1040.   function  Number_In (List : Lists) return Natural;
  1041.     -- DESCRIPTION : Returns the number of Elements in LIST
  1042.     -- INPUTS      : List - the list to check the number in.
  1043.     -- OUTPUTS     : the number of Elements in LIST
  1044.     -- EXCEPTIONS  : should never raise any.
  1045.  
  1046.   procedure Remove    (Number : Positive; From : in out Lists);
  1047.     -- DESCRIPTION : Deletes Element NUMBER from the list FROM.
  1048.     -- INPUTS      : Number - Ordinal # of the Element to remove.
  1049.     --               From   - the list to remove it from.
  1050.     -- OUTPUTS     : From   - the list after the Element is removed.
  1051.     -- EXCEPTIONS  : Arent_That_Many_Elements when Number is
  1052.     --               greater than the Number_In the list FROM.
  1053.  
  1054.   procedure Replace   (Number : Positive;
  1055.                Within : in out Lists;
  1056.                By     : Element);
  1057.     -- DESCRIPTION : Replaces the Element at NUMBER in the list
  1058.     --               WITHIN by the Element BY.
  1059.     -- INPUTS      : Number - Ordinal number of Element to replace.
  1060.     --               Within - the list within which it is replaced.
  1061.     --               By     - the Element it is replaced by.
  1062.     -- OUTPUTS     : Within - the list with the Element replaced.
  1063.     -- EXCEPTIONS  : Arent_That_Many_Elements when Number is
  1064.     --               greater than the Number_In the list WITHIN.
  1065.  
  1066.  
  1067. private
  1068.  
  1069.   Block_Size : Positive renames Block_By;
  1070.   type Array_Of_Elements is array (1 .. Block_Size) of Element;
  1071.   type List_Block;
  1072.   type Block_Ptr is access List_Block;
  1073.   type List_Block is
  1074.     record
  1075.       Item : Array_Of_Elements;
  1076.       Next : Block_Ptr;
  1077.     end record;
  1078.  
  1079.   subtype Count_Range is Natural range 0 .. Block_Size - 1;
  1080.  
  1081.   type Lists is
  1082.     record
  1083.       Last_Block_Upto : Count_Range := 0;
  1084.       Number_Blocks   : Positive := 1;
  1085.       First_Block     : Block_Ptr := new List_Block;
  1086.     end record;
  1087.  
  1088. end Variable_Lists;
  1089. --\\BALANCED_TREES.ADA
  1090. with Unchecked_Deallocation;
  1091.  
  1092. package body Balanced_Trees is
  1093.     --   There are three key parts to any tree, Seed_Node,
  1094.     -- Root_Node, and tree Nodes.  Seed_Node is a node outside
  1095.     -- of the tree. It contains a pointer to the first node of the tree
  1096.     -- in Seed_Node.Pointer(1) and a string of blanks equal to
  1097.     -- the max key length for that tree in Seed_Node.Data.Key(Last).
  1098.     -- The Root_Node is the "actual" top Node of the tree.  It
  1099.     -- contains Data and Pointers like any other Node in the tree.
  1100.     --   All the procedures and functions in B_Trees work on the
  1101.     -- premise that any single node of the tree will never be
  1102.     -- less than half full.  Half full is defined as the max
  1103.     -- number of Keys per node  divided by two (No_Of_Keys / 2).
  1104.  
  1105.   Middle_Of_Node : constant Integer := (1 + No_Of_Keys) / 2;
  1106.  
  1107.   procedure Free_Ptr is new Unchecked_Deallocation (Node, Trees);
  1108.   procedure Free_Key is new Unchecked_Deallocation (Keys, Acc_Keys);
  1109.   procedure Free_Data is new Unchecked_Deallocation (Datas, Acc_Data);
  1110.   procedure Free_Item is new Unchecked_Deallocation (Items, Acc_Items);
  1111.  
  1112.   function Node_Is_Full (This_Node : Trees) return Boolean is
  1113.     -- Checks to see if the given node is full.
  1114.   begin
  1115.     return This_Node.Data (No_Of_Keys) /= null;
  1116.   end Node_Is_Full;
  1117.  
  1118.  
  1119.   function Key_Exists (Key         : Keys;
  1120.                At_Position : Position_In_Tree) return Boolean is
  1121.        --   Checks to see if the given Key actually exists at the given
  1122.        -- Position_In_Tree.
  1123.   begin
  1124.     Return At_Position.Node_In_Tree.Data (At_Position.Position_In_Node).Key
  1125.        (1 .. Key'Length) = Key;
  1126.     exception
  1127.       When Constraint_Error =>
  1128.         Return False;
  1129.           -- Constraint_Error will be raised if the Key location
  1130.           -- being checked against the input value is a null Key or
  1131.           -- if the position is outside the node limits.
  1132.   end Key_Exists;
  1133.  
  1134.  
  1135.   function Key_At (Position : Position_In_Tree) return Keys is
  1136.   begin
  1137.     return Position.Node_In_Tree.Data (Position.Position_In_Node)
  1138.        .Key.all;
  1139.   end Key_At;
  1140.  
  1141.  
  1142.   function Item_At (Position : Position_In_Tree) return Items is
  1143.   begin
  1144.     return Position.Node_In_Tree.Data (Position.Position_In_Node)
  1145.        .Item.all;
  1146.   end Item_At;
  1147.  
  1148.  
  1149.   function New_Tree (With_Key_Length : Integer) return Trees is
  1150.       --   New_Tree creates the "seed_node" and the "root_node"
  1151.       -- of a tree.
  1152.     Seed              : Trees := new Node;
  1153.     Key_Length : Keys (1 .. With_Key_Length) := (others => ' ');
  1154.   begin
  1155.     Seed.Data (No_Of_Keys) := new Datas;
  1156.     Seed.Data (No_Of_Keys).Key := new Keys'(Key_Length);
  1157.     Seed.Pointer (1) := new Node;
  1158.     Seed.Pointer (1).Mother := Seed;
  1159.     return Seed;
  1160.   end New_Tree;
  1161.  
  1162.  
  1163.   function Position (Of_Key : Keys; Within_Node : Trees) return Integer is
  1164.       -- Does a binary search on a node to find a position where a
  1165.       -- Key is located or where it would be if were there.
  1166.       -- It is possible that this position could be outside of
  1167.       -- the nodes limits (No_Of_Keys) by one. When used Search_For_Node
  1168.       -- this extra position subscripts the pointer to a lower node.
  1169.     First      : Integer := 1;
  1170.     Middle     : Integer;
  1171.     Last       : Integer := No_Of_Keys;
  1172.     Trees_Data : Data_Array renames Within_Node.Data;
  1173.     Key_Value  : Keys renames Of_Key;
  1174.   begin
  1175.     while First <= Last loop
  1176.       Middle := (First + Last) / 2;
  1177.  
  1178.       if Trees_Data (Middle) = null or else
  1179.         Key_Value < Trees_Data (Middle).Key (1 .. Key_Value'Length) then
  1180.     Last := Middle - 1;
  1181.  
  1182.       elsif Trees_Data (Middle).Key (1 .. Key_Value'Length) < Key_Value then
  1183.     First := Middle + 1;
  1184.       else
  1185.     return Middle;
  1186.       -- This exit returns the node_position the value was found at.
  1187.       end if;
  1188.     end loop;
  1189.  
  1190.     return First;
  1191.       -- This exit returns the node_position the value would
  1192.       -- be if it was there.
  1193.   end Position;
  1194.  
  1195.  
  1196.   function Search_For_Node (Starting_At : Trees;
  1197.                             Containing : Keys) return Trees is
  1198.       --   This will search a tree and return the first node that
  1199.       -- "this_value" is located at.  It compares "this_value"
  1200.       -- against "this_value"'range  of the values stored
  1201.       -- in the tree, enabling searches on partial Keys.
  1202.       -- When Search_For_Node returns a recursive call to itself it
  1203.       -- passes in the next lower node where the key may exist.
  1204.     Current_Data    : Acc_Data;
  1205.     Current_Pointer : Trees;
  1206.     Node_Position   : Integer;
  1207.     Value           : Keys renames Containing;
  1208.     Tree            : Trees renames Starting_At;
  1209.   begin
  1210.     Node_Position := Position (Of_Key => Value, Within_Node => Tree);
  1211.     Current_Pointer := Tree.Pointer (Node_Position);
  1212.       -- Finds the next lower node to look for the given key.
  1213.  
  1214.     if Node_Position > No_Of_Keys then
  1215.       if Tree.Pointer (Node_Position) /= null then
  1216.     return Search_For_Node (Starting_At => Current_Pointer,
  1217.                                 Containing => Value);
  1218.       else
  1219.     return Tree;
  1220.       -- Bottom level node.
  1221.       end if;
  1222.     end if;
  1223.  
  1224.     Current_Data := Tree.Data (Node_Position);
  1225.  
  1226.     if Current_Data = null then
  1227.       if Current_Pointer /= null then
  1228.     return Search_For_Node (Starting_At => Current_Pointer,
  1229.                                 Containing => Value);
  1230.       else
  1231.     return Tree;
  1232.       -- Bottom level node.
  1233.       end if;
  1234.  
  1235.     elsif Value = Current_Data.Key (1 .. Value'Length) or else
  1236.       Tree.Pointer (Node_Position) = null then
  1237.       return Tree;
  1238.         -- Only if the key being looked for in the tree is actually
  1239.         -- there will a node other than a bottom node be returned to the
  1240.         -- calling procedure.
  1241.     else
  1242.       return Search_For_Node (Starting_At => Tree.Pointer (Node_Position),
  1243.                               Containing => Value);
  1244.     end if;
  1245.   end Search_For_Node;
  1246.  
  1247.  
  1248.   function Tree_Position (In_Tree    : Trees;
  1249.               Containing : Keys) return Position_In_Tree is
  1250.       -- This combines procedures Position and Search_For_Node
  1251.       -- into a single operation.
  1252.     Tree_Position  : Position_In_Tree;
  1253.     Tree           : Trees renames In_Tree;
  1254.     Key_Value      : Keys renames Containing;
  1255.   begin
  1256.     Tree_Position.Node_In_Tree :=
  1257.          Search_For_Node (Starting_At => Tree, Containing => Key_Value);
  1258.     Tree_Position.Position_In_Node :=
  1259.          Position (Of_Key => Key_Value,
  1260.          Within_Node => Tree_Position.Node_In_Tree);
  1261.     return Tree_Position;
  1262.   end Tree_Position;
  1263.  
  1264.  
  1265.   procedure Search_For_First_Instance (Of_Key : Keys;
  1266.                                    In_Tree : in out Position_In_Tree;
  1267.                                Giving_Item  : out Items) is
  1268.        --   This search takes a position within a tree and a value and
  1269.        -- searches for prior instances of that value, which would occur
  1270.        -- only if the value is a partial length of the Key stored in
  1271.        -- the tree. It loacates the very first instance of the Key in
  1272.        -- the tree.
  1273.      Tree_Position : Position_In_Tree renames In_Tree;
  1274.      Key_Value     : Keys renames Of_Key;
  1275.      Out_Item      : Items renames Giving_Item;
  1276.   begin
  1277.     Get_Prior (From => Tree_Position, Giving_Item => Out_Item);
  1278.  
  1279.     while Key_At (Tree_Position) (1 .. Key_Value'Length) = Key_Value loop
  1280.       Get_Prior (From => Tree_Position, Giving_Item => Out_Item);
  1281.     end loop;
  1282.  
  1283.     Get_Next (From => Tree_Position, Giving_Item => Out_Item);
  1284.       -- If the loop ends normally then the first instance of the Key
  1285.       -- will be be in the next position.
  1286.   exception
  1287.     when End_Of_Tree =>
  1288.       Out_Item := Tree_Position.Node_In_Tree.Data (1).Item.all;
  1289.         --   Sets the Out_Item when the first instance is the
  1290.         -- only instance, and the first Key in the tree.
  1291.         -- This is accomplished here due to the fact that if a
  1292.         -- procedure ends abnormally (ie. raised exceptions) any
  1293.         -- out paramaters revert to there previous states.  If the
  1294.         -- Key being searched for was the FIRST value in the tree
  1295.         -- and unique the Out_Item would never be set.  This holds
  1296.         -- true for Search_For_Last_Instance where the Key is the
  1297.         -- LAST value in the tree and unique.
  1298.   end Search_For_First_Instance;
  1299.  
  1300.  
  1301.   procedure Search_For_Last_Instance (Of_Key : Keys;
  1302.                                   In_Tree : in out Position_In_Tree;
  1303.                                     Giving_Item : out Items) is
  1304.       --   This search takes a position within a tree and a value and
  1305.       -- searches for next instances of the value, which would occur
  1306.       -- only if the value is a partial length of the Keys stored in
  1307.       -- the tree. It loacates the very last instance of the Key
  1308.       -- in the tree.
  1309.     Tree_Position : Position_In_Tree renames In_Tree;
  1310.     Key_Value     : Keys renames Of_Key;
  1311.     Out_Item      : Items renames Giving_Item;
  1312.   begin
  1313.     Get_Next (From => Tree_Position,Giving_Item => Out_Item);
  1314.  
  1315.     while Key_At (Tree_Position) (1 .. Key_Value'Length) = Key_Value loop
  1316.       Get_Next (From => Tree_Position, Giving_Item => Out_Item);
  1317.     end loop;
  1318.  
  1319.     Get_Prior (From => Tree_Position, Giving_Item => Out_Item);
  1320.       -- If the loop ends normally then the Last instance of the Key
  1321.       -- will be be in the prior position.
  1322.   exception
  1323.     when End_Of_Tree =>
  1324.       Out_Item := Tree_Position.Node_In_Tree.Data
  1325.              (Tree_Position.Position_In_Node).Item.all;
  1326.         -- See documentation at end of Search_For_First_Instance.
  1327.   end Search_For_Last_Instance;
  1328.  
  1329.  
  1330.   function Item_Where (Key_Is : Keys;In_Tree : Trees) return items is
  1331.     Out_Item           : Items;
  1332.     Temp_Tree_Position : Position_In_Tree;
  1333.     Key_Value          : Keys renames Key_Is;
  1334.     Max_Key_Length     : Integer := In_Tree.Data (No_Of_Keys).Key'Length;
  1335.   begin
  1336.     if Key_Value'Length > Max_Key_Length then
  1337.       raise Key_Length_Error;
  1338.     end if;
  1339.       -- Checks to see if the Key given is larger than the Key
  1340.       -- length that the tree is built on.
  1341.  
  1342.     Temp_Tree_Position := Tree_Position (In_Tree => In_Tree.Pointer (1),
  1343.                                          Containing => Key_Value);
  1344.       -- Finds where the key should be located in the tree.
  1345.  
  1346.     if Key_Exists (Key_Value, At_Position => Temp_Tree_Position) then
  1347.       -- If the key actually EXIST in the tree a serch for the first
  1348.       -- instance of that key is made in case it is a partial value
  1349.       -- being searched for.
  1350.  
  1351.       Search_For_First_Instance (Of_Key => Key_Value,
  1352.                                  In_Tree => Temp_Tree_Position,
  1353.                                  Giving_Item => Out_Item);
  1354.       return Out_Item;
  1355.     else
  1356.       raise Key_Not_Found;
  1357.     end if;
  1358.   end Item_Where;
  1359.  
  1360.  
  1361.   procedure Get_First (From            : Trees;
  1362.                Giving_Position : out Position_In_Tree;
  1363.                Giving_Item    : out Items) is
  1364.       --   Loops down the given nodes      1              X
  1365.       -- first pointer until it reaches   / \            / \
  1366.       -- the bottom where it returns     2   X    ie    X   1
  1367.       -- the first position and         / \ / \        / \ / \
  1368.       -- item of that node.            3  X X  X      X  X 2  X
  1369.     Current_Node : Trees := From;
  1370.   begin
  1371.     if Current_Node.Pointer (1) /= null and then
  1372.        Current_Node.Pointer (1).Data (1) = null then
  1373.       raise End_Of_Tree;
  1374.     end if;
  1375.       -- Checks to see if the tree contains any Data.
  1376.  
  1377.     while Current_Node.Pointer (1) /= null loop
  1378.       Current_Node := Current_Node.Pointer (1);
  1379.     end loop;
  1380.  
  1381.     Giving_Position := (Current_Node, 1);
  1382.     Giving_Item := Item_At (Position => (Current_Node, 1));
  1383.   end Get_First;
  1384.  
  1385.  
  1386.   procedure Get_Last (From            : Trees;
  1387.               Giving_Position : out Position_In_Tree;
  1388.               Giving_Item        : out Items) is
  1389.       --   Starting at the given node Get_Last loops through the node
  1390.       -- backwards (since each node will at least half full or
  1391.       -- greater it is quicker to go backwards)       1          X
  1392.       -- until a pointer to a lower node is          / \        / \
  1393.       -- met. The loop continues down the           X   2  ie  1   X
  1394.       -- tree until a data is met with no          / \ / \    / \ / \
  1395.       -- pointer to a lower node.                 X  X X  3  X  2 X  X
  1396.     Current_Node     : Trees := From;
  1397.     Current_Position : Integer;
  1398.     Temp_Node        : Trees;
  1399.   begin
  1400.     if Current_Node.Pointer (1) /= null and then
  1401.        Current_Node.Pointer (1).Data (1) = null then
  1402.       raise End_Of_Tree;
  1403.         -- Checks to see if the tree contains any Data.
  1404.     elsif Current_Node.Mother = null then
  1405.       Current_Node := Current_Node.Pointer (1);
  1406.         -- Puts the caller at the root_node level.
  1407.         -- When a Get_Last is done in the tree itself the
  1408.         -- input trees type is already at the proper place.
  1409.     end if;
  1410.  
  1411.     while Current_Node /= null loop
  1412.       Current_Position := No_Of_Keys;
  1413.  
  1414.       while Current_Node.Pointer (Current_Position + 1) = null and then
  1415.         Current_Node.Data (Current_Position) = null loop
  1416.     Current_Position := Current_Position - 1;
  1417.       end loop;
  1418.  
  1419.       Temp_Node := Current_Node;
  1420.       Current_Node := Current_Node.Pointer (Current_Position + 1);
  1421.     end loop;
  1422.  
  1423.     Giving_Position := (Temp_Node, Current_Position);
  1424.     Giving_Item := Item_At (Position => (Temp_Node, Current_Position));
  1425.   end Get_Last;
  1426.  
  1427.  
  1428.   procedure Get_First_Key (From            : Trees;
  1429.                With_Value      : Keys;
  1430.                Giving_Position : out Position_In_Tree;
  1431.                Giving_Item     : out Items) is
  1432.     Temp_Tree_Position : Position_In_Tree;
  1433.     Key_Value          : Keys renames With_Value;
  1434.       -- Performs the same operations as Item_Where.
  1435.   begin
  1436.     Temp_Tree_Position := Tree_Position (In_Tree => From,
  1437.                                          Containing => Key_Value);
  1438.  
  1439.     if Key_Exists (Key_Value, At_Position => Temp_Tree_Position) then
  1440.       Search_For_First_Instance (Of_Key => Key_Value,
  1441.                                  In_Tree => Temp_Tree_Position,
  1442.                                  Giving_Item => Giving_Item);
  1443.       Giving_Position := Temp_Tree_Position;
  1444.     else
  1445.       raise Key_Not_Found;
  1446.     end if;
  1447.   end Get_First_Key;
  1448.  
  1449.  
  1450.   procedure Get_Last_Key (From            : Trees;
  1451.               With_Value      : Keys;
  1452.               Giving_Position : out Position_In_Tree;
  1453.               Giving_Item        : out Items) is
  1454.     Temp_Tree_Position : Position_In_Tree;
  1455.     Key_Value          : Keys renames With_Value;
  1456.       -- Performs the same operations at Get_First_Key except
  1457.       -- it gets the LAST instance of the Key.
  1458.   begin
  1459.     Temp_Tree_Position := Tree_Position (In_Tree => From,
  1460.                                          Containing => Key_Value);
  1461.  
  1462.     if Key_Exists (Key_Value, At_Position => Temp_Tree_Position) then
  1463.       Search_For_Last_Instance (Of_Key => Key_Value,
  1464.                                 In_Tree => Temp_Tree_Position,
  1465.                                 Giving_Item => Giving_Item);
  1466.       Giving_Position := Temp_Tree_Position;
  1467.     else
  1468.       raise Key_Not_Found;
  1469.     end if;
  1470.   end Get_Last_Key;
  1471.  
  1472.  
  1473.   procedure Get_Next (From        : in out Position_In_Tree;
  1474.               Giving_Item : out Items) is
  1475.     Temp_Position : Integer;
  1476.     Nodes         : Trees renames From.Node_In_Tree;
  1477.     Position      : Integer Renames From.Position_In_Node;
  1478.  
  1479.     procedure Climb_Tree (Upper, Lower : Trees) is
  1480.       -- Used to traverse up a tree to the next logical
  1481.       -- position.  Also checks to see if the next position
  1482.       -- would be outside the tree limits.
  1483.     begin
  1484.  
  1485.       if Upper.Data (1) = null then
  1486.     Position := Temp_Position;
  1487.     raise End_Of_Tree;
  1488.       -- Raises End_Of_Tree when Climb_Tree traverses up the
  1489.       -- tree to the Seed_Node.  This resets the position back
  1490.       -- to the last Data in the tree.
  1491.       end if;
  1492.  
  1493.       Position := 1;
  1494.  
  1495.       while Upper.Pointer (Position) /= Lower loop
  1496.     Position := Position + 1;
  1497.       end loop;
  1498.         -- Find the position of the pointer in the
  1499.         -- mother_node to the lower node.
  1500.  
  1501.       if Position = No_Of_Keys + 1 or else
  1502.      Upper.Data (Position) = null then
  1503.     Climb_Tree (Upper => Upper.Mother, Lower => Upper);
  1504.         -- Recursively called by going up multiple levels
  1505.             -- in the tree
  1506.       else
  1507.     Nodes := Upper;
  1508.     Giving_Item := Item_At (Position => From);
  1509.       end if;
  1510.     end Climb_Tree;
  1511.   begin
  1512.     if Nodes.Pointer (Position + 1) = null then
  1513.       if Position + 1 > No_Of_Keys or else
  1514.      Nodes.Data (Position + 1) = null then
  1515.     Temp_Position := Position;
  1516.     Climb_Tree (Upper => Nodes.Mother, Lower => Nodes);
  1517.       else
  1518.     Position := Position + 1;
  1519.     Giving_Item := Item_At (Position => From);
  1520.       end if;
  1521.     else
  1522. @@
  1523.       Nodes := Nodes.Pointer (Position + 1);
  1524.       Get_First (From => Nodes, Giving_Position => From,
  1525.                  Giving_Item => Giving_Item);
  1526.     end if;
  1527.   end Get_Next;
  1528.  
  1529.  
  1530.   procedure Get_Prior (From        : in out Position_In_Tree;
  1531.                Giving_Item : out Items) is
  1532.     Nodes    : Trees renames From.Node_In_Tree;
  1533.     Position : Integer renames From.Position_In_Node;
  1534.  
  1535.     procedure Climb_Tree (Upper : Trees; Lower : Trees) is
  1536.         -- Used to traverse up a tree to the next logical
  1537.         -- position.  Also checks to see if the prior position
  1538.         -- would be outside the tree limits.
  1539.     begin
  1540.  
  1541.       if Upper.Data (1) = null then
  1542.     raise End_Of_Tree;
  1543.          -- Raises End_Of_Tree when Climb_Tree traverses up to the
  1544.          -- tree to the Seed_Node.
  1545.       end if;
  1546.  
  1547.       Position := No_Of_Keys + 1;
  1548.  
  1549.       while Upper.Pointer (Position) /= Lower loop
  1550.     Position := Position - 1;
  1551.       end loop;
  1552.            -- Find the position of the pointer in the
  1553.            -- mother_node to the lower node.
  1554.  
  1555.       if Position = 1 then
  1556.     Climb_Tree (Upper => Upper.Mother, Lower => Upper);
  1557.           -- Recursively called by going up multiple levels in the tree
  1558.       else
  1559.     From := (Upper, Position - 1);
  1560.     Giving_Item := Item_At (Position => From);
  1561.       end if;
  1562.     end Climb_Tree;
  1563.   begin
  1564.     if Nodes.Pointer (Position) /= null then
  1565.       Nodes := Nodes.Pointer (Position);
  1566.       Get_Last (From => Nodes, Giving_Position => From,
  1567.                 Giving_Item => Giving_Item);
  1568.  
  1569.     elsif Position = 1 then
  1570.       Climb_Tree (Upper => Nodes.Mother,
  1571.                   Lower => Nodes);
  1572.     else
  1573.       Position := Position - 1;
  1574.       Giving_Item := Item_At (Position => From);
  1575.     end if;
  1576.   end Get_Prior;
  1577.  
  1578.  
  1579.   procedure Change_Item (For_Key : Keys;
  1580.              In_Tree : Trees;
  1581.              To      : Items) is
  1582.  
  1583.  
  1584.     Key_Value     : Keys renames For_Key;
  1585.     Node_Position : Position_In_Tree;
  1586.     Hold_Item     : Items;
  1587.     Max_Key_Length : Integer := In_Tree.Data (No_Of_Keys).Key'Length;
  1588.       -- Changes the item of the FIRST instance of Key_Value.
  1589.   begin
  1590.     if Key_Value'Length /= Max_Key_Length then
  1591.       raise Key_Length_Error;
  1592.     end if;
  1593.       -- Checks to see if Key_Value is exactly the same length
  1594.       -- as the Key length the tree was built on.
  1595.     Get_First_Key (From => In_Tree, With_Value => Key_Value,
  1596.                    Giving_Position => Node_Position,
  1597.                    Giving_Item => Hold_Item);
  1598.     Free_Item (Node_Position.Node_In_Tree.Data
  1599.                      (Node_Position.Position_In_Node).Item);
  1600.     Node_Position.Node_In_Tree.Data
  1601.          (Node_Position.Position_In_Node).Item := new Items'(To);
  1602.   end Change_Item;
  1603.  
  1604.  
  1605.   procedure Change_Item (At_Position : Position_In_Tree;To : Items) is
  1606.   begin
  1607.     Free_Item (At_Position.Node_In_Tree.Data
  1608.                      (At_Position.Position_In_Node).Item);
  1609.     At_Position.Node_In_Tree.Data (At_Position.Position_In_Node).item
  1610.          := new Items'(To);
  1611.   end Change_Item;
  1612.  
  1613.  
  1614.   function Inclusive_Subtree (Of_Tree  : Trees;
  1615.                   From_Key : Keys;
  1616.                   To_Key   : Keys) return Trees is
  1617.     Hold_Item      : Items;
  1618.     Sub_Tree       : Trees := Of_Tree;
  1619.     Position       : Position_In_Tree;
  1620.     Max_Key_Length : Integer := Of_Tree.Data (No_Of_Keys).Key'Length;
  1621.   begin
  1622.     if From_Key'Length > Max_Key_Length or else
  1623.        To_Key'Length > Max_Key_Length then
  1624.       raise Key_Length_Error;
  1625.     end if;
  1626.  
  1627.     Sub_Tree := New_Tree (With_Key_Length => Max_Key_Length);
  1628.  
  1629.     Position := Tree_Position (In_Tree => Of_Tree,
  1630.                                Containing => From_Key);
  1631.  
  1632.     if Position.Position_In_Node > No_Of_Keys or else
  1633.        Position.Node_In_Tree.Data (Position.Position_In_Node) = null then
  1634.       Position.Position_In_Node := Position.Position_In_Node - 1;
  1635.       Get_Next (From => Position, Giving_Item => Hold_Item);
  1636.     end if;
  1637.       --   Checks the position passed back by Tree_Position
  1638.       -- to make sure it is valid.  It's possible for the
  1639.       -- position to be pointing to a null value or a position
  1640.       -- outside the node limits.  If true, the position is moved
  1641.       -- back one.
  1642.     Search_For_First_Instance (Of_Key => From_Key, In_Tree => Position,
  1643.                                Giving_Item => Hold_Item);
  1644.  
  1645.     while Key_At (Position) (1 .. To_Key'Length) <= To_Key loop
  1646.       Insert (Key_Value => Key_At (Position),
  1647.           And_Item => Hold_Item, Into => Sub_Tree);
  1648.       Get_Next (From => Position, Giving_Item => Hold_Item);
  1649.     end loop;
  1650.  
  1651.     return Sub_Tree;
  1652.   exception
  1653.     when End_Of_Tree =>
  1654.       return Sub_Tree;
  1655.         --   A Subtree can be built even if no Data is inserted
  1656.         -- into it.  In this case a Seed_Node is still passed back,
  1657.         -- but the tree contains no entries.
  1658.   end Inclusive_Subtree;
  1659.  
  1660.  
  1661.   function Exclusive_Subtree (Of_Tree  : Trees;
  1662.                   From_Key : Keys;
  1663.                   To_Key   : Keys) return Trees is
  1664.     Hold_Item      : Items;
  1665.     Sub_Tree       : Trees := Of_Tree;
  1666.     Position       : Position_In_Tree;
  1667.     Max_Key_Length : Integer := Of_Tree.Data (No_Of_Keys).Key'Length;
  1668.   begin
  1669.     if From_Key'Length > Max_Key_Length or else
  1670.        To_Key'Length > Max_Key_Length then
  1671.       raise Key_Length_Error;
  1672.     end if;
  1673.  
  1674.     Sub_Tree := New_Tree (With_Key_Length => Max_Key_Length);
  1675.  
  1676.     Position := Tree_Position (In_Tree => Of_Tree,
  1677.                                Containing => From_Key);
  1678.  
  1679.     if Position.Position_In_Node > No_Of_Keys or else
  1680.        Position.Node_In_Tree.Data (Position.Position_In_Node) = null then
  1681.       Position.Position_In_Node := Position.Position_In_Node - 1;
  1682.       Get_Next (From => Position, Giving_Item => Hold_Item);
  1683.     end if;
  1684.       --   Checks the position passed back by Tree_Position
  1685.       -- to make sure it is valid.  It's possible for the
  1686.       -- position to be pointing to a null value or a position
  1687.       -- outside the node limits.  If true, the position is moved
  1688.       -- back one.
  1689.     Search_For_Last_Instance (Of_Key => From_Key, In_Tree => Position,
  1690.                               Giving_Item => Hold_Item);
  1691.  
  1692.     if Key_At (Position) (1 .. From_Key'Length) = From_Key then
  1693.       Get_Next (From => Position, Giving_Item => Hold_Item);
  1694.         -- Since it is an exclusive tree the first position to
  1695.         -- right of the Key is the starting point and the first
  1696.         -- position to the left of To_Key is the ending point.
  1697.     end if;
  1698.  
  1699.     while Key_At (Position) (1 .. To_Key'Length) < To_Key loop
  1700.       Insert (Key_Value => Key_At (Position),
  1701.           And_Item  => Hold_Item, Into => Sub_Tree);
  1702.       Get_Next (From => Position, Giving_Item => Hold_Item);
  1703.     end loop;
  1704.  
  1705.     return Sub_Tree;
  1706.   exception
  1707.     when End_Of_Tree =>
  1708.       return Sub_Tree;
  1709.         --   A Subtree can be built even if no Data is inserted
  1710.         -- into it.  In this case a Seed_Node is still Passed backe, but
  1711.         -- the tree contains no entries.
  1712.   end Exclusive_Subtree;
  1713.  
  1714.  
  1715.   procedure Delete_Tree (Tree : in out Trees) is
  1716.       --   This procedure deallocates the space used by a B_tree.
  1717.       -- It starts at the lower right hand corner of a tree and
  1718.       -- deallocates each node in a right to left, bottom to top
  1719.       -- motion for each branch of the Root_Node.  The order of
  1720.       -- deallocation (1..7) =>    7
  1721.       --                          / \
  1722.       --                         6   3
  1723.       --                        / \ / \
  1724.       --                       5  4 2  1
  1725.       -- After deallocation of the tree the Seed_Node pointing to
  1726.       -- that tree is deallocated and passed back as null.
  1727.     Position     : Integer := 2;
  1728.     Upper_Node   : Trees := Tree;
  1729.     Lower_Node   : Trees;
  1730.     Max_Position : Positive := No_Of_Keys + 1;
  1731.   begin
  1732.     if Tree /= null then
  1733.  
  1734.       Lower_Node := Tree.Pointer (1);
  1735.       while Lower_Node.Pointer (1) /= null loop
  1736.         Position := 1;
  1737.  
  1738.         while Position <= Max_Position and then
  1739.               Lower_Node.Pointer (Position) /= null loop
  1740.       Position := Position + 1;
  1741.         end loop;
  1742.  
  1743.         Upper_Node := Lower_Node;
  1744.         Lower_Node := Upper_Node.Pointer (Position- 1);
  1745.       end loop;
  1746.       -- Loops to the right most bottom node.
  1747.  
  1748.       Upper_Node.Pointer (Position - 1) := null;
  1749.       Position := 1;
  1750.  
  1751.       while Position < Max_Position and then
  1752.             Lower_Node.Data (Position) /= null loop
  1753.         Free_Key (Lower_Node.Data (Position).Key);
  1754.         Free_Item (Lower_Node.Data (Position).Item);
  1755.         Free_Data (Lower_Node.Data (Position));
  1756.         Position := Position + 1;
  1757.       end loop;
  1758.       -- Deallocates the nodes data.
  1759.  
  1760.       Lower_Node.Mother := null;
  1761.       Free_Ptr (Lower_Node);
  1762.       -- Deallocates the Node.
  1763.  
  1764.       if Upper_Node = Tree then
  1765.         Free_Key (Upper_Node.Data (No_Of_Keys).Key);
  1766.         Free_Data (Upper_Node.Data (No_Of_Keys));
  1767.         Free_Ptr (Upper_Node);
  1768.         -- Deallocates the seed node.
  1769.       else
  1770.         Delete_Tree (Tree);
  1771.       end if;
  1772.     end if;
  1773.   end Delete_Tree;
  1774.  
  1775.  
  1776.   procedure Insert (Key_Value : Keys; And_Item : Items; Into : Trees) is
  1777.  
  1778.       --   To insert data with a key value of X you search
  1779.       -- to locate where the data should belong (node_B).  If there
  1780.       -- is fewer than N data in the tree (where N is the number
  1781.       -- of data allowed per node) the current data in node_B is
  1782.       -- shifted right starting at the point where the data being
  1783.       -- inserted should be and that data is inserted into the
  1784.       -- cleared position.
  1785.       --    ie.
  1786.       --      node_B  before insertion of key 'B'  => |A,C,_|
  1787.       --      node_B  after  insertion of key 'B'  => |A,B,C|
  1788.       --
  1789.       --   If node_B contains N data it is required to split node_B
  1790.       -- into two separate nodes to make room for the new data.
  1791.       -- When split, data out of node_B or the data being inserted
  1792.       -- must be raised and inserted into its mother node.  This is
  1793.       -- done so a pointer can be pointed from the mother node to
  1794.       -- the newly created node.  The new node is created (node_C)
  1795.       -- and data is moved and raised by the following algorithm.
  1796.       --
  1797.       --    M       = (No_Of_Keys + 1) / 2   -- Middle of node
  1798.       --    x       = The Key being inserted.
  1799.       --    |A,D,F| = Node_B
  1800.       --
  1801.       --    When =>        x < Node_B.Key(M)
  1802.       --       Everything less than Node_B.Key(M) moves to node_C
  1803.       --       including x.  Node_B.Key(M) gets raised and inserted
  1804.       --       into the parent node.
  1805.       --                              node_C =>  |A,x,_|
  1806.       --                              node_B =>  |F,_,_|
  1807.       --                              Raised =>   D
  1808.       --
  1809.       --    When =>        x > Node_B.Key(M+1)
  1810.       --       Everything less than Node_B.Key(M+1) moves to node_C
  1811.       --       and x is inserted into Node_B.  Node_B.Key(M+1)
  1812.       --       gets raised and inserted into the parent node.
  1813.       --                              node_C =>  |A,D,_|
  1814.       --                              node_B =>  |x,_,_|
  1815.       --                              Raised =>   F
  1816.       --
  1817.       --    When =>      Node_B.Key(M) < x < Node_B.Key(M+1)
  1818.       --       Everything less than Node_B.Key(M+1) moves to node_C
  1819.       --       and the starting key x is raised and inserted
  1820.       --       into the parent node.
  1821.       --                               node_C => |A,D,_|
  1822.       --                               node_B => |F,_,_|
  1823.       --                               Raised =>  x
  1824.       --
  1825.       --   ie.
  1826.       --     Inserting Key 'B' into Node_B in a two level tree
  1827.       --            Before               After
  1828.       --              |                   |
  1829.       --             seed                seed
  1830.       --              |                   |
  1831.       --            |K,R,_|             |D,K,R|
  1832.       --           /  |  \             /  |  \ \
  1833.       --      |A,D,F| n   n     |A,B,_||F,_,_|n n
  1834.       --         |                 |      |
  1835.       --       node_B            node_C node_B
  1836.  
  1837.     Seed                     : Trees renames Into;
  1838.     Stored_Data              : Acc_Data;
  1839.     Temp_Node, Current_Node  : Trees;
  1840.     Left, Right              : Trees;
  1841.     Temp_Position            : Integer;
  1842.     Max_Key_Length           : Integer := Seed.Data(No_Of_Keys).Key'Length;
  1843.  
  1844.     procedure Insert ( This_Data : Acc_Data; Into : Trees) is
  1845.         --   Does the actual insertion of the Data into a node.
  1846.         -- It locates the position where the Data should go,
  1847.         -- makes room for the Data and inserts it.
  1848.       Receiving_Node : Trees renames Into;
  1849.       M, First       : Integer;
  1850.       Last           : Integer := No_Of_Keys;
  1851.     begin
  1852.       First := Position (Of_Key      => This_Data.Key.all,
  1853.              Within_Node => Receiving_Node);
  1854.  
  1855.       while First < Last loop
  1856.     Receiving_Node.Data (Last) := Receiving_Node.Data (Last - 1);
  1857.     Receiving_Node.Pointer (Last + 1) := Receiving_Node.Pointer (Last);
  1858.     Last := Last - 1;
  1859.       end loop;
  1860.  
  1861.       Receiving_Node.Data (First) := This_Data;
  1862.  
  1863.       if Left /= null then
  1864.     Receiving_Node.Pointer (First) := Left;
  1865.     Receiving_Node.Pointer (First + 1) := Right;
  1866.       end if;
  1867.     end Insert;
  1868.  
  1869.  
  1870.     procedure Create_New_Root_Node is
  1871.         -- Creates a new Root node when spliting of the present
  1872.         -- root node is required due to insertion of data.
  1873.       Root_Node : Trees := new Node;
  1874.     begin
  1875.       Root_Node.Mother := Right.Mother;
  1876.       Root_Node.Mother.Pointer (1) := Root_Node;
  1877.       Right.Mother := Root_Node;
  1878.       Left.Mother := Root_Node;
  1879.       Insert (Stored_Data, Into => Root_Node);
  1880.      -- If by spliting you raise all the way to the root_node
  1881.      -- this creates a new root_node where the old one was
  1882.      -- split in two.  The insert of the data is done here
  1883.          -- because at the time this is the only place Root_Node
  1884.          -- is visable.
  1885.     end Create_New_Root_Node;
  1886.  
  1887.  
  1888.     procedure Split (Nodes : Trees; Using : Acc_Data) is
  1889.          --   Split does the actual checking to see where
  1890.          -- a node needs to be split and what value needs to be raised
  1891.          -- and inserted into the parent node of the one being split.
  1892.        The_Data   : Acc_Data renames Using;
  1893.       procedure Compose (This_Node                  : Trees;
  1894.              From_Position, To_Position : Integer) is
  1895.           -- Compose takes a range from a node thats being split
  1896.       -- and places it (left_justified) in the new node, or the node
  1897.       -- that was split. Also nulls out the remainder of the node
  1898.       -- being split after it left_justifies the data.
  1899.     Current_Position : Integer := 1;
  1900.       begin
  1901.     for A in From_Position .. To_Position loop
  1902.       This_Node.Pointer (Current_Position) := Nodes.Pointer (A);
  1903.       This_Node.Data (Current_Position) := Nodes.Data (A);
  1904.       Current_Position := Current_Position + 1;
  1905.     end loop;
  1906.  
  1907.     This_Node.Pointer (Current_Position) :=
  1908.                        Nodes.Pointer (To_Position + 1);
  1909.  
  1910.     if This_Node = Nodes then
  1911.       for A in Current_Position .. No_Of_Keys loop
  1912.         Nodes.Data (A) := null;
  1913.         Nodes.Pointer (A + 1) := null;
  1914.       end loop;
  1915.     end if;
  1916.       end Compose;
  1917.     begin
  1918.       Temp_Node := new Node;
  1919.  
  1920.       if The_Data.Key.all < Nodes.Data (Middle_Of_Node).Key.all then
  1921.     Stored_Data := Nodes.Data (Middle_Of_Node);
  1922.     Compose (Temp_Node, From_Position => 1,
  1923.                  To_position => Middle_Of_Node - 1);
  1924.     Compose (Nodes, From_Position => Middle_Of_Node + 1,
  1925.                  To_Position => No_Of_Keys);
  1926.     Insert (The_Data, Into => Temp_Node);
  1927.  
  1928.       elsif Nodes.Data (Middle_Of_Node + 1).Key.all < The_Data.Key.all then
  1929.     Stored_Data := Nodes.Data (Middle_Of_Node + 1);
  1930.     Compose (Temp_Node, From_Position => 1,
  1931.                  To_Position => Middle_Of_Node);
  1932.     Compose (Nodes, From_Position => Middle_Of_Node + 2,
  1933.                  To_Position => No_Of_Keys);
  1934.     Insert (The_Data, Into => Nodes);
  1935.  
  1936.     if Left /= null then
  1937.       Left.Mother := Right.Mother;
  1938.     end if;
  1939.       else
  1940.     Stored_Data := The_Data;
  1941.     Compose (Temp_Node, From_Position => 1,
  1942.                  To_Position => Middle_Of_Node);
  1943.     Compose (Nodes, From_Position => Middle_Of_Node + 1,
  1944.                  To_Position => No_Of_Keys);
  1945.  
  1946.     if Left /= null then
  1947.       Temp_Node.Pointer (Middle_Of_Node + 1) := Left;
  1948.     end if;
  1949.       end if;
  1950.       Left := Temp_Node;
  1951.       Right := Nodes;
  1952.  
  1953.       for A in 1 .. No_Of_Keys + 1 loop
  1954.     exit when Left.Pointer (A) = null;
  1955.     Left.Pointer (A).Mother := Left;
  1956.       -- Changes the mother pointer in the lower node to point
  1957.       -- back at the new node.
  1958.       end loop;
  1959.  
  1960.       if Nodes.Mother = Seed then
  1961.     Create_New_Root_Node;
  1962.  
  1963.       elsif Node_Is_Full (Nodes.Mother) then
  1964.     Split (Nodes.Mother, Using => Stored_Data);
  1965. 0V
  1966.       -- Recalls the spliting process for the raised data.
  1967.       else
  1968.     Insert (Stored_Data, Into => Nodes.Mother);
  1969.     Left.Mother := Right.Mother;
  1970.       -- Inserts the raised data into the mother node and
  1971.       -- sets the new nodes mother pointer equal its
  1972.       -- counterparts mother.
  1973.       end if;
  1974.     end Split;
  1975.   begin
  1976. -------------INSERT----------------
  1977.     if Key_Value'Length /= Max_Key_Length then
  1978.       raise Key_Length_Error;
  1979.     end if;
  1980.       -- Checks to see if the inserting keys length is exactly
  1981.       -- equal to the key length the that the tree is built on.
  1982.  
  1983.     Current_Node := Search_For_Node (Seed.Pointer (1), Key_Value);
  1984.     Temp_Position := Position (Of_Key      => Key_Value,
  1985.                    Within_Node => Current_Node);
  1986.        -- Finds where the key should belong in the tree.
  1987.  
  1988.     if Key_Exists (Key_Value, (Current_Node, Temp_Position)) then
  1989.       raise Key_Already_Exists;
  1990.     end if;
  1991.  
  1992.     Stored_Data := new Datas;
  1993.     Stored_Data.Key := new Keys'(Key_Value);
  1994.     Stored_Data.Item := new Items'(And_Item);
  1995.  
  1996.     if Node_Is_Full (Current_Node) then
  1997.       Split (Current_Node, Using => Stored_Data);
  1998.     else
  1999.       Insert (Stored_Data, Into => Current_Node);
  2000.     end if;
  2001.   end Insert;
  2002.  
  2003.  
  2004. procedure Delete_Key (With_Value : Keys; From : Trees) is
  2005.     --   This procedure doesn't contain much imagination and its main
  2006.     -- purpose was just to work at all.  It is probably not the
  2007.     -- most efficient way of deleting keys and keeping the tree
  2008.     -- balanced, but it does work.  The problems lies in never having
  2009.     -- a node less than half full or one branch of a tree containing
  2010.     -- more/less levels than the other brances of the tree.
  2011.     --
  2012.     --   This procedure's main problem is the fact that it doesn't work
  2013.     -- on trees with nodes of less than 4.  This is due to the fact
  2014.     -- that a node could contain only 1 data and be half full.  When
  2015.     -- this data is deleted it leaves the node with nothing in it which
  2016.     -- Delete_Key can't handle.  If deletion of this key causes a
  2017.     -- rebalancing to take place, that breaks off the unbalanced branch
  2018.     -- of the tree and reinserts it, then a CONSTRAINT_ERROR will be
  2019.     -- raised.  This is caused by procedure Get_Next going into the
  2020.     -- empty node and trying to return a Item.all of a null Items access
  2021.     -- type.
  2022.  
  2023.   Nodes          : Trees;
  2024.   Seed_Node      : Trees := From;
  2025.   Hold_Item      : Items;
  2026.   Node_Position  : Position_In_Tree;
  2027.   Temp_Node      : Position_In_Tree;
  2028.   Position       : Integer;
  2029.   Ptr_To_Node    : Integer := 1;
  2030.   Half_Of_Node   : Integer := No_Of_Keys / 2;
  2031.   Max_Key_Length : Integer := From.Data (No_Of_Keys).Key'Length;
  2032.  
  2033.  
  2034.   procedure Left_Justify (Tree : Position_In_Tree) is
  2035.       -- Left justifies the data in the node starting at
  2036.       -- the given position.
  2037.     Place : Integer := Tree.Position_In_Node;
  2038.     Left  : Trees := Tree.Node_In_Tree;
  2039.   begin
  2040.     while Place < No_Of_Keys loop
  2041.       Left.Pointer (Place) := Left.Pointer (Place + 1);
  2042.       Left.Data (Place) := Left.Data (Place + 1);
  2043.       Place := Place + 1;
  2044.     end loop;
  2045.  
  2046.     Left.Pointer (Place) := Left.Pointer (Place + 1);
  2047.     Left.Data (Place) := null;
  2048.     Left.Pointer (Place + 1) := null;
  2049.   end Left_Justify;
  2050.  
  2051.  
  2052.   function Half_Full (Tree : Trees; X : Integer := 0) return Boolean is
  2053.       -- Checks the given node to see if it is "X" less than half full.
  2054.     Num : Integer := X;
  2055.   begin
  2056.     for A in 1 .. No_Of_Keys loop
  2057.       if Tree.Data (A) /= null then
  2058.     Num := Num + 1;
  2059.       end if;
  2060.     end loop;
  2061.  
  2062.     return Num <= No_Of_Keys / 2;
  2063.   end Half_Full;
  2064.  
  2065.  
  2066.   procedure Reorg_Tree (Tree : Trees) is
  2067.       -- When the bottom nodes have become empty enough so that no
  2068.       -- combining or switching keys is possible then that part of the
  2069.       -- tree is broken off and reinserted back into the Tree.
  2070.  
  2071.     Sub_Tree    : Trees := new Node;
  2072.     Mother_Node : Trees := Tree;
  2073.  
  2074.  
  2075.     procedure Balance_Tree (Tree : Trees) is
  2076.         -- Loops through the Sub_Tree and inserts each data back into
  2077.         -- the original Tree.
  2078.       Position : Position_In_Tree;
  2079.     begin
  2080.       Get_First (From            => Sub_Tree,
  2081.          Giving_Position => Position,
  2082.          Giving_Item     => Hold_Item);
  2083.  
  2084.       loop
  2085.     Insert (Key_Value => Position.Node_In_Tree.Data
  2086.                             (Position.Position_In_Node).Key.all,
  2087.         And_Item  => Hold_Item,
  2088.         Into      => Seed_Node);
  2089.     Get_Next (From => Position, Giving_Item => Hold_Item);
  2090.       end loop;
  2091.     exception
  2092.       when End_Of_Tree =>
  2093.     Delete_Tree (Sub_Tree);
  2094.           -- Deallocates the Sub_Tree.
  2095.     end Balance_Tree;
  2096.  
  2097.  
  2098.     procedure Make_A_Sub_Tree (Data_Position, Ptr_Position : Integer) is
  2099.       -- Breaks a branch off the Tree, creating a Sub_Tree.
  2100.     begin
  2101.       Sub_Tree.Data (No_Of_Keys) := new Datas;
  2102.       Sub_Tree.Data (No_Of_Keys).Key :=
  2103.     new Keys'(Mother_Node.Data (Data_Position).Key.all);
  2104.       Sub_Tree.Pointer (1) := Mother_Node.Pointer (Ptr_Position);
  2105.  
  2106.       if Sub_Tree.Pointer (1).Data (Half_Of_Node) = null then
  2107.     Sub_Tree.Pointer (1).Data (Half_Of_Node) :=
  2108.       Mother_Node.Data (Data_Position);
  2109.       else
  2110.     Sub_Tree.Pointer (1).Data (Half_Of_Node + 1) :=
  2111.       Mother_Node.Data (Data_Position);
  2112.       end if;
  2113.  
  2114.       Sub_Tree.Pointer (1).Mother := Sub_Tree;
  2115.     end Make_A_Sub_Tree;
  2116.  
  2117.   begin
  2118.     while Half_Full (Mother_Node) and then Mother_Node.Mother /= Seed_Node loop
  2119.       Temp_Node.Node_In_Tree := Mother_Node;
  2120.       Mother_Node := Mother_Node.Mother;
  2121.         -- Loops up the Tree searching for a place to
  2122.         -- break off a branch from the tree.
  2123.     end loop;
  2124.  
  2125.     Ptr_To_Node := 1;
  2126.  
  2127.     while Mother_Node.Pointer (Ptr_To_Node) /= Temp_Node.Node_In_Tree loop
  2128.       Ptr_To_Node := Ptr_To_Node + 1;
  2129.     end loop;
  2130.  
  2131.     if Mother_Node.Data (2) = null then
  2132.       if Ptr_To_Node = 1 then
  2133.     Make_A_Sub_Tree (Ptr_To_Node, Ptr_To_Node);
  2134.     Mother_Node.Pointer (2).Mother := Seed_Node;
  2135.     Seed_Node.Pointer (1) := Mother_Node.Pointer (2);
  2136.       else
  2137.     Make_A_Sub_Tree (Ptr_To_Node - 1, Ptr_To_Node);
  2138.     Mother_Node.Pointer (1).Mother := Seed_Node;
  2139.     Seed_Node.Pointer (1) := Mother_Node.Pointer (1);
  2140.       end if;
  2141.         -- This happens when the reorg takes place at the Root_Node
  2142.         -- where it contains only 1 data record.
  2143.  
  2144.     elsif Ptr_To_Node = 1 then
  2145.       Make_A_Sub_Tree (Ptr_To_Node, Ptr_To_Node);
  2146.       Left_Justify ((Mother_Node, Ptr_To_Node));
  2147.     else
  2148.       Make_A_Sub_Tree (Ptr_To_Node - 1, Ptr_To_Node);
  2149.       Mother_Node.Pointer (Ptr_To_Node) :=
  2150.     Mother_Node.Pointer (Ptr_To_Node - 1);
  2151.       Left_Justify ((Mother_Node, Ptr_To_Node - 1));
  2152.     end if;
  2153.  
  2154.     Balance_Tree (Sub_Tree);
  2155.   end Reorg_Tree;
  2156.  
  2157.  
  2158.   procedure Try_To_Combine_Nodes is
  2159.       -- This contains the Logic of What to do after a Key has been
  2160.       -- deleted which leaves a bottom node less than half full.
  2161.  
  2162.     procedure Combine_Two_Nodes (Com_Tree         : Trees;
  2163.                  Center, Position : Integer) is
  2164.         -- Combines two bottom level nodes together along with one
  2165.         -- record from their parent node.
  2166.       Mid : Integer := Center;
  2167.     begin
  2168.       if Com_Tree.Data (Mid) /= null then
  2169.     Mid := Mid + 1;
  2170.       end if;
  2171.  
  2172.       Com_Tree.Data (Mid) := Nodes.Data (Position);
  2173.  
  2174.       for A in Mid + 1 .. No_Of_Keys loop
  2175.     Com_Tree.Data (A) := Temp_Node.Node_In_Tree.Data (A - (Mid));
  2176.     Temp_Node.Node_In_Tree.Data (A - (Mid)) := null;
  2177.           -- Moves the Data from the old node into the new one.
  2178.       end loop;
  2179.  
  2180.       Free_Ptr (Temp_Node.Node_In_Tree);
  2181.         -- Deallocates the old node
  2182.       Left_Justify ((Nodes, Position));
  2183.     end Combine_Two_Nodes;
  2184.  
  2185.  
  2186.     procedure Switch_Keys_In_Nodes (Where : String := "LFT") is
  2187.         -- Yoyos data between three connected nodes.
  2188.       Yoyo_Tree : Position_In_Tree;
  2189.  
  2190.       procedure Yoyo (To, From : Integer) is
  2191.       begin
  2192.     Temp_Node.Node_In_Tree.Data (To) := Nodes.Data (From);
  2193.     Nodes.Data (From) := Yoyo_Tree.Node_In_Tree.Data
  2194.                 (Yoyo_Tree.Position_In_Node);
  2195.     Yoyo_Tree.Node_In_Tree.Data (Yoyo_Tree.Position_In_Node) := null;
  2196.       end Yoyo;
  2197.     begin
  2198.       if Where = "RGT" then
  2199.     for A in 1 .. Position - 1 loop
  2200.       Temp_Node.Node_In_Tree.Data (A + 1) :=
  2201.         Temp_Node.Node_In_Tree.Data (A);
  2202.     end loop;
  2203.       -- When going to the right, room must be made in the first
  2204.       -- position of the node to accept a key.  This loop
  2205.       -- right justifies temp_node starting at the position of
  2206.       -- the deleted key.
  2207.     Get_Last (From            => Nodes.Pointer (Ptr_To_Node - 1),
  2208.           Giving_Position => Yoyo_Tree,
  2209.           Giving_Item     => Hold_Item);
  2210.       -- Gets the last key in the node on temp_nodes' left
  2211.     Yoyo (1, Ptr_To_Node - 1);
  2212.       else
  2213.     Yoyo_Tree := (Nodes.Pointer (Ptr_To_Node + 1), 1);
  2214.       -- sets yoyo_tree at position 1 of the node to temp_nodes
  2215.       -- right.
  2216.     Yoyo (Half_Of_Node, Ptr_To_Node);
  2217.     Left_Justify (Yoyo_Tree);
  2218.       end if;
  2219.     end Switch_Keys_In_Nodes;
  2220.  
  2221.  
  2222.     procedure Combine_Right_Node is
  2223.       -- Checks to see if the Node on the left can be combined
  2224.       -- into itself.
  2225.     begin
  2226.       if Half_Full (Nodes.Pointer (Ptr_To_Node - 1), (-1)) then
  2227.     Left_Justify (Temp_Node);
  2228.     Nodes.Pointer (Ptr_To_Node) := Nodes.Pointer (Ptr_To_Node - 1);
  2229.     Combine_Two_Nodes (Nodes.Pointer (Ptr_To_Node - 1), Half_Of_Node + 1,
  2230.                Ptr_To_Node - 1);
  2231.       else
  2232.     Switch_Keys_In_Nodes ("RGT");
  2233.       end if;
  2234.     end Combine_Right_Node;
  2235.  
  2236.  
  2237.     procedure Combine_Left_Node is
  2238.       -- Checks to see if the Node on the right can be combined
  2239.       -- into itself.
  2240.     begin
  2241.       Left_Justify (Temp_Node);
  2242.  
  2243.       if Half_Full (Nodes.Pointer (Ptr_To_Node + 1), (-1)) then
  2244.     Temp_Node.Node_In_Tree := Nodes.Pointer (Ptr_To_Node + 1);
  2245.     Nodes.Pointer (Ptr_To_Node + 1) := Nodes.Pointer (Ptr_To_Node);
  2246.     Combine_Two_Nodes (Nodes.Pointer (Ptr_To_Node), Half_Of_Node,
  2247.                Ptr_To_Node);
  2248.       else
  2249.     Switch_Keys_In_Nodes;
  2250.       end if;
  2251.     end Combine_Left_Node;
  2252.   begin
  2253.     Nodes := Nodes.Mother;
  2254.  
  2255.     while Nodes.Pointer (Ptr_To_Node) /= Temp_Node.Node_In_Tree loop
  2256.       Ptr_To_Node := Ptr_To_Node + 1;
  2257.     end loop;
  2258.  
  2259.     if Half_Full (Nodes) then
  2260.       if Ptr_To_Node /= 1 and then
  2261.      not Half_Full (Nodes.Pointer (Ptr_To_Node - 1)) then
  2262.     Switch_Keys_In_Nodes ("RGT");
  2263.       -- going from left to right
  2264.  
  2265.       elsif Nodes.Pointer (Ptr_To_Node + 1) /= null and then
  2266.         not Half_Full (Nodes.Pointer (Ptr_To_Node + 1)) then
  2267.     Left_Justify (Temp_Node);
  2268.     Switch_Keys_In_Nodes;
  2269.       -- going from right to left
  2270.  
  2271.       elsif Nodes /= Seed_Node then
  2272.     Left_Justify (Temp_Node);
  2273.     Reorg_Tree (Nodes);
  2274.       else
  2275.     Left_Justify (Temp_Node);
  2276.       -- Where Temp_Node is the Root_Node
  2277.       -- The Root_Node is the only node left in the tree.
  2278.       end if;
  2279.  
  2280.     elsif Ptr_To_Node = 1 then
  2281.       Combine_Left_Node;
  2282.  
  2283.     elsif Ptr_To_Node = No_Of_Keys + 1 or else
  2284.       Nodes.Data (Ptr_To_Node) = null then
  2285.       Combine_Right_Node;
  2286.  
  2287.     elsif Half_Full (Nodes.Pointer (Ptr_To_Node + 1)) then
  2288.       Combine_Left_Node;
  2289.     else
  2290.       Combine_Right_Node;
  2291.     end if;
  2292.   end Try_To_Combine_Nodes;
  2293.  
  2294. begin
  2295.   if With_Value'Length /= Max_Key_Length then
  2296.     raise Key_Length_Error;
  2297.   end if;
  2298.     -- Check to make sure the Key being deleted is the same length as
  2299.     -- keys the tree was built on.
  2300.  
  2301.   Node_Position := Tree_Position
  2302.               (In_Tree => Seed_Node, Containing => With_Value);
  2303.     -- Finds where the key should be in the tree.
  2304.  
  2305.   Temp_Node := Node_Position;
  2306.   Nodes := Node_Position.Node_In_Tree;
  2307.   Position := Node_Position.Position_In_Node;
  2308.  
  2309.   if Key_Exists (With_Value, At_Position => Node_Position) then
  2310.     Free_Key (Nodes.Data (Position).Key);
  2311.     Free_Item (Nodes.Data (Position).Item);
  2312.     Free_Data (Nodes.Data (Position));
  2313.  
  2314.     if Nodes.Pointer (1) /= null then
  2315.       Get_First (From            => Nodes.Pointer (Position + 1),
  2316.          Giving_Position => Temp_Node,
  2317.          Giving_Item     => Hold_Item);
  2318.       Nodes.Data (Position) :=
  2319.     Temp_Node.Node_In_Tree.Data (Temp_Node.Position_In_Node);
  2320.       Nodes := Temp_Node.Node_In_Tree;
  2321.       Position := Temp_Node.Position_In_Node;
  2322.       Nodes.Data (Position) := null;
  2323.         -- If the keys position in the tree is other than the bottom
  2324.         -- level then a bottom level node is pulled up into the vacant
  2325.         -- position left by the deleted data.
  2326.     end if;
  2327.  
  2328.     if Half_Full (Nodes, 1) then
  2329.         -- Checking to see if the node is ONE less than half full.
  2330.       Try_To_Combine_Nodes;
  2331.     else
  2332.       Left_Justify ((Nodes, Position));
  2333.     end if;
  2334.   else
  2335.     raise Key_Not_Found;
  2336.   end if;
  2337.  
  2338. end Delete_Key;
  2339.  
  2340. begin
  2341.   if No_Of_Keys < 4 then
  2342.     raise Program_Error;
  2343.   end if;
  2344. end Balanced_Trees;
  2345.  $
  2346.