Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 32.02 KB | None | 0 0
  1. --------------------------------------------------------------------------------------------------------------------
  2. --------------------------------------------------------------------------------------------------------------------
  3. --Program_2
  4. --------------------------------------------------------------------------------------------------------------------
  5. --------------------------------------------------------------------------------------------------------------------
  6. WITH Ada.Text_IO;
  7. WITH Ada.Integer_Text_IO;
  8. WITH Binary_Tree_String;
  9.  
  10. USE Ada.Text_IO;
  11. USE Ada.Integer_Text_IO;
  12.  
  13. PROCEDURE Program_2 IS
  14.  
  15. TempStr : Binary_Tree_String.ElementType;
  16. TempLen : Natural;
  17. StrTree : Binary_Tree_String.Bi_Tree;
  18. Cur : Binary_Tree_String.NodePtr;
  19.  
  20. MaxDepth, MinDepth, AvgDepth : Binary_Tree_String.DepthRange := -1;
  21.  
  22. In_File1, In_File2, Out_File1, Out_File2 : Ada.Text_IO.File_Type;
  23.  
  24. BEGIN
  25.  
  26. Ada.Text_IO.Open(File => In_File1,
  27.                  Mode => Ada.Text_IO.In_File,
  28.                  Name => "Input1.txt");
  29.  
  30. Ada.Text_IO.Open(File => In_File2,
  31.                  Mode => Ada.Text_IO.In_File,
  32.                  Name => "Input2.txt");
  33.  
  34. Ada.Text_IO.Open(File => Out_File1,
  35.                  Mode => Ada.Text_IO.Out_File,
  36.                  Name => "Output1.txt");
  37.  
  38. Ada.Text_IO.Open(File => Out_File2,
  39.                  Mode => Ada.Text_IO.Out_File,
  40.                  Name => "Output2.txt");
  41.  
  42. --Loading the first line of the file as the Root of the tree
  43. Ada.Text_IO.Get_Line(Item => TempStr, Last => TempLen, File => In_File1);
  44. Binary_Tree_String.Initialize(BT => StrTree, X=> TempStr, Length => TempLen);
  45.  
  46. --Loading input file 1 into the binary tree
  47. WHILE Ada.Text_IO.End_Of_File(In_File1) = False LOOP
  48. Ada.Text_IO.Get_Line(Item => TempStr, Last => TempLen, File => In_File1);
  49. Binary_Tree_String.Add_Organized(BT => StrTree, X => TempStr, Length => TempLen);
  50. END LOOP;
  51.  
  52. -----------------------------------------------------------
  53. --This Inorder procedure works, and is much more     --
  54. --efficient. I had to use my original one however  --
  55. --because it was integrated with my Max, Min      --
  56. --and Average Depth procedures.                       --
  57. --********************************** --
  58. --Put_Line("Inorder display: ");                            --
  59. --Binary_Tree_String.Display_Inorder(StrTree);    --
  60. --***********************************--
  61. ------------------------------------------------------------
  62. --Displaying the binary tree to the screen, as well as calculating the max, min, and average depths
  63. Put_Line("The tree before deletion: ");
  64. Put_Line("     Inorder: ");
  65. Binary_Tree_String.Display(BT => StrTree, MD => MaxDepth, MiD => MinDepth, AD => AvgDepth);
  66. Put_Line("     Preorder: ");
  67. Binary_Tree_String.Display_Preorder(StrTree);
  68.  
  69. --Writing the max, min, and average depths to output file 1
  70. Put(Item => "The maximum depth is : ", File => Out_File1);
  71. Put(Item => MaxDepth, Width => 4, File => Out_File1);
  72. New_Line(File => Out_File1);
  73. Put(Item => "The minimum depth is : ", File => Out_File1);
  74. Put(Item => MinDepth, Width => 4, File => Out_File1);
  75. New_Line(File => Out_File1);
  76. Put(Item => "The average depth is : ", File => Out_File1);
  77. Put(Item => AvgDepth, Width => 4, File => Out_File1);
  78. New_Line(File => Out_File1);
  79.  
  80. New_Line;
  81.  
  82. --Removing everything in input file 1 from the binary tree
  83. WHILE Ada.Text_IO.End_Of_File(In_File2) = False LOOP
  84. Ada.Text_IO.Get_Line(Item => TempStr, Last => TempLen, File => In_File2);
  85. Binary_Tree_String.Delete(BT => StrTree, X => TempStr(1..TempLen));
  86. END LOOP;
  87.  
  88. --Displaying the binary tree to the screen, as well as calculating the max, min, and average depths
  89. Put_Line("The tree after deletion: ");
  90. Put_Line("     Inorder: ");
  91. Binary_Tree_String.Display(BT => StrTree, MD => MaxDepth, MiD => MinDepth, AD => AvgDepth);
  92. Put_Line("     Preorder: ");
  93. Binary_Tree_String.Display_Preorder(StrTree);
  94.  
  95. --Writing the max, min, and average depths to output file 2
  96. Put(Item => "The maximum depth is : ", File => Out_File2);
  97. Put(Item => MaxDepth, Width => 4, File => Out_File2);
  98. New_Line(File => Out_File2);
  99. Put(Item => "The minimum depth is : ", File => Out_File2);
  100. Put(Item => MinDepth, Width => 4, File => Out_File2);
  101. New_Line(File => Out_File2);
  102. Put(Item => "The average depth is : ", File => Out_File2);
  103. Put(Item => AvgDepth, Width => 4, File => Out_File2);
  104. New_Line(File => Out_File2);
  105.  
  106. END Program_2;
  107. --------------------------------------------------------------------------------------------------------------------
  108. --------------------------------------------------------------------------------------------------------------------
  109. --Binary_Tree_String.ads
  110. --------------------------------------------------------------------------------------------------------------------
  111. --------------------------------------------------------------------------------------------------------------------
  112. WITH stacks_Generic;
  113.  
  114. PACKAGE Binary_Tree_String IS
  115.  
  116.    SUBTYPE ElementType IS String(1..32);
  117.       SUBTYPE States IS Integer RANGE 1..3;
  118.       SUBTYPE DepthRange IS Integer Range -1..Integer'Last;
  119.       --TYPE States IS (Left, Right, Both, Neither, Visited);
  120.    TYPE Node;
  121.    TYPE NodePtr IS ACCESS Node;
  122.  
  123.    TYPE Node IS RECORD
  124.       Info : ElementType;
  125.             Length : Natural;
  126.       Parent, Left, Right : NodePtr;
  127.    END RECORD;
  128.  
  129.    TYPE Bi_Tree IS RECORD
  130.       Root : NodePtr;
  131.    END RECORD;
  132.  
  133.       Does_Not_Exist : EXCEPTION;
  134.  
  135.       PACKAGE Stacks IS NEW stacks_generic(StackElement => States);
  136.  
  137.       OutOfSpace: EXCEPTION;
  138.  
  139.    PROCEDURE Initialize(BT : IN OUT Bi_Tree; X : IN ElementType; Length : IN Natural);
  140.  
  141.    PROCEDURE Add_Organized(BT : IN OUT Bi_Tree; X : ElementType; Length : IN Natural);
  142.  
  143.    PROCEDURE Display(BT : IN Bi_Tree; MD, MiD, AD : IN OUT DepthRange);
  144.  
  145.       PROCEDURE Display_Inorder(BT : IN Bi_Tree);
  146.  
  147.       PROCEDURE Display_Preorder(BT : IN Bi_Tree);
  148.  
  149.       PROCEDURE Depth_Max_Min_Average(Max, Min, Average : IN OUT DepthRange; CurDepth : IN DepthRange);
  150.  
  151.     FUNCTION Search(BT : IN Bi_Tree; X : IN String) RETURN NodePtr;
  152.  
  153.    PROCEDURE Delete(BT : IN OUT Bi_Tree; X : IN String);
  154.  
  155.    FUNCTION Find_Max(Cur : IN NodePtr) RETURN NodePtr;
  156.  
  157. END Binary_Tree_String;
  158. --------------------------------------------------------------------------------------------------------------------
  159. --------------------------------------------------------------------------------------------------------------------
  160. --Binary_Tree_String.adb
  161. --------------------------------------------------------------------------------------------------------------------
  162. --------------------------------------------------------------------------------------------------------------------
  163. WITH Unchecked_Deallocation;
  164. WITH Ada.Text_IO;
  165. WITH Ada.Integer_Text_IO;
  166. USE Ada.Text_IO;
  167. USE Ada.Integer_Text_IO;
  168.  
  169. PACKAGE BODY Binary_Tree_String IS
  170.  
  171.    PROCEDURE Dispose IS NEW Unchecked_Deallocation(Object => Node, Name => NodePtr);
  172.       ----------------------------------------------------------------------------
  173.    FUNCTION Allocate(X : ElementType; Length : Natural; Parent, Left, Right : NodePtr) RETURN NodePtr IS
  174.       Result : NodePtr;
  175.    BEGIN
  176.             BEGIN
  177.             Result := NEW Node'(Info => X, Length => Length, Parent => Parent, Left => Left, Right => Right);
  178.             RETURN Result;
  179.           EXCEPTION
  180.               WHEN Storage_Error =>
  181.                   RAISE OutOfSpace;
  182.             END;
  183.    END Allocate;
  184.       ----------------------------------------------------------------------------
  185.    PROCEDURE Deallocate(Ptr : IN OUT NodePtr) IS
  186.    BEGIN
  187.       Dispose(X => Ptr);
  188.    END Deallocate;
  189.       ----------------------------------------------------------------------------
  190.    PROCEDURE Initialize(BT : IN OUT Bi_Tree; X : IN ElementType; Length : Natural) IS
  191.    BEGIN
  192.             BT.Root := Allocate(X => X, Length => Length, Parent => NULL, Left => NULL, Right => NULL);
  193.    END Initialize;
  194.  
  195.    ----------------------------------------------------------------------------
  196.    ----------------------------------------------------------------------------
  197.    ----------------------------------------------------------------------------
  198.    ----------------------------------------------------------------------------
  199.    --Function that determines whether or not the new node goes
  200.    --to the left or to the right.
  201.    FUNCTION Add_Org_Helper(A, B : String) RETURN Boolean IS
  202.       Left_Flag : Boolean;
  203.       I : Positive := 1;
  204.       BEGIN
  205.       LOOP
  206.                   IF I > A'Last THEN
  207.                         RETURN True;
  208.                   ELSIF I > B'Last THEN
  209.                         RETURN False;
  210.                   END IF;
  211.  
  212.          IF Character'Pos(A(I)) < Character'Pos(B(I)) THEN
  213.             Left_Flag := True;
  214.             Exit;
  215.          ELSIF Character'Pos(A(I)) > Character'Pos(B(I)) THEN
  216.             Left_Flag := False;
  217.             Exit;
  218.          END IF;
  219.          I := I + 1;
  220.       END LOOP;
  221.       RETURN Left_Flag;
  222.       END Add_Org_Helper;
  223.    ----------------------------------------------------------------------------
  224.      ----------------------------------------------------------------------------
  225.       PROCEDURE Add_Org_Recurrer(Cur, Prev : NodePtr; X : IN ElementType; Left_F : IN Boolean; Length : IN Natural) IS
  226.          Current : NodePtr := Cur;
  227.          Previous : NodePtr := Prev;
  228.          Left_Flag : Boolean := Left_F;
  229.       BEGIN
  230.          IF Current = NULL THEN
  231.                Current := Allocate(X => X, Parent => Previous, Left => NULL, Right => NULL, Length => Length);
  232.                IF Previous /= NULL THEN
  233.                   IF Left_Flag = True THEN
  234.                      Previous.Left := Current;
  235.                   ELSE
  236.                      Previous.Right := Current;
  237.                   END IF;
  238.                END IF;
  239.                RETURN;
  240.          END IF;
  241.          Left_Flag := Add_Org_Helper(A => X, B => Current.Info);
  242.          IF X(1..Length) = Current.Info(1..Current.Length) THEN
  243.                RETURN;
  244.          END IF;
  245.  
  246.          IF Left_Flag = TRUE THEN
  247.             Add_Org_Recurrer(Cur => Current.Left, Prev => Current, X => X, Left_F => Left_Flag, Length => Length);
  248.          ELSE
  249.             Add_Org_Recurrer(Cur => Current.Right, Prev => Current, X => X, Left_F => Left_Flag, Length => Length);
  250.          END IF;
  251.       END Add_Org_Recurrer;
  252.      ----------------------------------------------------------------------------
  253.    ----------------------------------------------------------------------------
  254.    --Procedure that adds a string to the tree in a Binary-Tree-Friendly way
  255.       PROCEDURE Add_Organized(BT : IN OUT Bi_Tree; X : ElementType; Length : IN Natural) IS
  256.       BEGIN
  257.          Add_Org_Recurrer(Cur => BT.Root, Prev => NULL, X => X, Left_F => True, Length => Length);
  258.       END Add_Organized;
  259.       ----------------------------------------------------------------------------
  260.       ----------------------------------------------------------------------------
  261.             ----------------------------------------------------------------------------
  262.             FUNCTION Max_Num(A, B : IN DepthRange) RETURN DepthRange IS
  263.             BEGIN
  264.                IF A > B THEN
  265.                   RETURN A;
  266.                ELSE
  267.                   RETURN B;
  268.                END IF;
  269.             END Max_Num;
  270.             ----------------------------------------------------------------------------
  271.             FUNCTION Min_Num(A, B : IN DepthRange) RETURN DepthRange IS
  272.             BEGIN
  273.                IF A < B THEN
  274.                   RETURN A;
  275.                ELSE
  276.                   RETURN B;
  277.                END IF;
  278.             END Min_Num;
  279.             ----------------------------------------------------------------------------
  280.             FUNCTION Average_Num(A, B : IN DepthRange) RETURN DepthRange IS
  281.                Avg : DepthRange;
  282.             BEGIN
  283.                Avg := (A + B) / 2;
  284.                RETURN Avg;
  285.             END Average_Num;
  286.             ----------------------------------------------------------------------------
  287.                         ----------------------------------------------------------------------------
  288.             PROCEDURE Depth_Max_Min_Average(Max, Min, Average : IN OUT DepthRange; CurDepth : IN DepthRange) IS
  289.             BEGIN
  290.                Max := Max_Num(Max, CurDepth);
  291.                Min := Min_Num(Min, CurDepth);
  292.                Average := Average_Num(Average, CurDepth);
  293.             END Depth_Max_Min_Average;
  294.             ----------------------------------------------------------------------------                  
  295.                         ----------------------------------------------------------------------------
  296.                         ----------------------------------------------------------------------------
  297.             FUNCTION Find_Max(Cur : IN NodePtr) RETURN NodePtr IS   --Refers to maxinum element in tree
  298.                   Current : NodePtr := Cur;
  299.             BEGIN
  300.                   IF Current.Right /= NULL THEN
  301.                         RETURN Find_Max(Cur => Current.Right);
  302.                   ELSE
  303.                         RETURN Current;
  304.                   END IF;
  305.             END Find_Max;
  306.       ----------------------------------------------------------------------------
  307.       PROCEDURE Display_Helper(Cur : NodePtr; Add_Flag : IN Boolean; TS: IN OUT Stacks.Stack; Max : IN NodePtr; MaxD, MinD, AvgD : IN OUT DepthRange) IS--; TS, VS : IN OUT Stacks.Stack) IS
  308.          Current : NodePtr := Cur;
  309.                   TempPop : States;
  310.                   TempDepth : Natural;
  311.       BEGIN
  312.                   IF Add_Flag = True THEN
  313.                      Stacks.Pop(S => TS, V => TempPop);
  314.                      Stacks.Push(S => TS, E => (TempPop + 1));
  315.                   ELSE
  316.                      Stacks.Push(S => TS, E => 1);
  317.                   END IF;
  318.                   IF Stacks.Top(S => TS) >= 3 THEN
  319.                      IF Current.Parent = NULL THEN
  320.                         RETURN;
  321.                      END IF;
  322.                      Stacks.Pop(S => TS, V=> TempPop);
  323.                      Display_Helper(Cur => Current.Parent , Add_Flag => True, TS => TS, Max => Max, MaxD => MaxD, MinD => MinD, AvgD => AvgD);
  324.                      RETURN;--
  325.                   END IF;
  326.  
  327.                   IF Current.Left /= NULL THEN
  328.                      IF Stacks.Top(S => TS) = 2 THEN
  329.                            Put_Line(Current.Info(1..Current.Length));-------------------------------
  330.                            IF Current.Info(1..Current.Length) = Max.Info(1..Max.Length) THEN
  331.                               RETURN;
  332.                            END IF;
  333.                            IF Current.Right /= NULL THEN
  334.                                  Display_Helper(Cur => Current.Right, Add_Flag => False, TS => TS, Max => Max, MaxD => MaxD, MinD => MinD, AvgD => AvgD);
  335.                                  RETURN;--
  336.                            ELSE
  337.                                  Stacks.Pop(S => TS, V=> TempPop);
  338.                                  Display_Helper(Cur => Current.Parent, Add_Flag => True, TS => TS, Max => Max, MaxD => MaxD, MinD => MinD, AvgD => AvgD);
  339.                                  RETURN;--
  340.                            END IF;
  341.                      ELSIF Stacks.Top(S => TS) = 3 THEN
  342.                            Stacks.Pop(S => TS, V=> TempPop);
  343.                            Display_Helper(Cur => Current.Parent , Add_Flag => True, TS => TS, Max => Max, MaxD => MaxD, MinD => MinD, AvgD => AvgD);
  344.                            RETURN;--
  345.                      ELSE
  346.                            Display_Helper(Cur => Current.Left, Add_Flag => False, TS => TS, Max => Max, MaxD => MaxD, MinD => MinD, AvgD => AvgD);
  347.                            RETURN;--
  348.                      END IF;
  349.                   ELSIF Current.Right /= NULL THEN
  350.                         IF Stacks.Top(S => TS) = 1 THEN
  351.                               Put_Line(Current.Info(1..Current.Length));
  352.                               Display_Helper(Cur => Current.Right, Add_Flag => False, TS => TS, Max => Max, MaxD => MaxD, MinD => MinD, AvgD => AvgD);
  353.                               RETURN;--
  354.                         ELSIF Stacks.Top(S => TS) = 2 THEN
  355.                               Stacks.Pop(S => TS, V => TempPop);
  356.                               Display_Helper(Cur => Current.Parent, Add_Flag => True, TS => TS, Max => Max, MaxD => MaxD, MinD => MinD, AvgD => AvgD);
  357.                               RETURN;--
  358.                         END IF;
  359.                   ELSE
  360.                         Put_Line(Current.Info(1..Current.Length));---------------------------------
  361.                         TempDepth := Stacks.Count_Stack(S => TS);
  362.                         IF MaxD = -1 AND MinD = -1 AND AvgD = -1 THEN
  363.                            MaxD := TempDepth;
  364.                            MinD := TempDepth;
  365.                            AvgD := TempDepth;
  366.                         ELSE
  367.                            Depth_Max_Min_Average(Max => MaxD, Min => MinD, Average => AvgD, CurDepth => TempDepth);
  368.                         END IF;
  369.                         IF Current.Info(1..Current.Length) = Max.Info(1..Max.Length) THEN
  370.                               RETURN;
  371.                         END IF;
  372.                         Stacks.Pop(S => TS, V => TempPop);
  373.                         Display_Helper(Cur => Current.Parent, Add_Flag => True, TS => TS, Max => Max, MaxD => MaxD, MinD => MinD, AvgD => AvgD);
  374.                         RETURN;--
  375.                   END IF;
  376.       END Display_Helper;
  377.       ----------------------------------------------------------------------------
  378.       PROCEDURE Display(BT : IN Bi_Tree; MD, MiD, AD : IN OUT DepthRange) IS
  379.          TStack : Stacks.Stack;
  380.          Max : NodePtr;
  381.       BEGIN
  382.          Max := Find_Max(BT.Root);
  383.          Stacks.MakeStack(S => TStack);
  384.          Display_Helper(Cur => BT.Root, Add_Flag => False, TS => TStack, Max => Max, MaxD => MD, MinD => MiD, AvgD => AD);--, VS => VStack);
  385.       END Display;
  386.             ----------------------------------------------------------------------------
  387.             ----------------------------------------------------------------------------
  388.             PROCEDURE Display_IO_Helper(Cur : IN NodePtr) IS
  389.                   Current : NodePtr := Cur;
  390.             BEGIN
  391.                   IF Current = NULL THEN
  392.                         RETURN;
  393.                   END IF;
  394.                   Display_IO_Helper(Current.Left);
  395.                   Put_Line(Current.Info(1..Current.Length));
  396.                   Display_IO_Helper(Current.Right);
  397.             END Display_IO_Helper;
  398.             ----------------------------------------------------------------------------
  399.             PROCEDURE Display_Inorder(BT : IN Bi_Tree) IS
  400.             BEGIN
  401.                   Display_IO_Helper(BT.Root);
  402.             END Display_Inorder;
  403.       ----------------------------------------------------------------------------
  404.       ----------------------------------------------------------------------------
  405.             ----------------------------------------------------------------------------
  406.             PROCEDURE Display_PO_Helper(Cur : IN NodePtr) IS
  407.                   Current : NodePtr := Cur;
  408.             BEGIN
  409.                   IF Current = NULL THEN
  410.                         RETURN;
  411.                   END IF;
  412.                   Put_Line(Current.Info(1..Current.Length));
  413.                   Display_PO_Helper(Current.Left);
  414.                   Display_PO_Helper(Current.Right);
  415.             END Display_PO_Helper;
  416.             ----------------------------------------------------------------------------
  417.             PROCEDURE Display_Preorder(BT : IN Bi_Tree) IS
  418.             BEGIN
  419.                   Display_PO_Helper(BT.Root);
  420.             END Display_Preorder;
  421.       ----------------------------------------------------------------------------
  422.             ----------------------------------------------------------------------------
  423.       ----------------------------------------------------------------------------
  424.             FUNCTION Search(BT : IN Bi_Tree; X : IN String) RETURN NodePtr IS
  425.                Current : NodePtr;
  426.                Left_Flag : Boolean;
  427.             BEGIN
  428.                   Current := BT.Root;
  429.                   LOOP
  430.                         IF Current.Info(1..Current.Length) = X THEN
  431.                               RETURN Current;
  432.                         END IF;
  433.                         Left_Flag := Add_Org_Helper(A => X, B => Current.Info(1..Current.Length));
  434.                         IF Left_Flag = True THEN
  435.                            Current := Current.Left;
  436.                         ELSE
  437.                            Current := Current.Right;
  438.                         END IF;
  439.                         IF Current = NULL THEN
  440.                            RAISE Does_Not_Exist;
  441.                         END IF;
  442.                   END LOOP;
  443.                   RETURN NULL;
  444.             END Search;
  445.             ----------------------------------------------------------------------------
  446.                         ----------------------------------------------------------------------------
  447.                         FUNCTION Is_Left_Child(N : NodePtr) RETURN Boolean IS
  448.                         BEGIN
  449.                               IF N.Parent.Left = N THEN
  450.                                     RETURN True;
  451.                               ELSE
  452.                                     RETURN False;
  453.                               END IF;
  454.                         END Is_Left_Child;
  455.                         ----------------------------------------------------------------------------
  456.             PROCEDURE Delete(BT : IN OUT Bi_Tree; X : IN String) IS
  457.                               NtD : NodePtr;
  458.                               TempNode : NodePtr;
  459.                    BEGIN
  460.                               NtD := Search(BT => BT, X => X);
  461.                               IF BT.Root = NtD THEN
  462.                                     TempNode := Find_Max(NtD.Left);
  463.                                     NtD.Left.Parent := NULL;
  464.                                     BT.Root := NtD.Left;
  465.                                     TempNode.Right := NtD.Right;
  466.                                     NtD.Right.Parent := TempNode;
  467.                               ELSIF NtD.Right /= NULL AND Ntd.Left /= NULL THEN
  468.                                     TempNode := Find_Max(NtD.Left);
  469.                                     NtD.Left.Parent := NtD.Parent;
  470.                                     IF Is_Left_Child(NtD) = TRUE THEN
  471.                                           NtD.Parent.Left := NtD.Left;
  472.                                     ELSE
  473.                                           Ntd.Parent.Right := Ntd.Left;
  474.                                     END IF;
  475.                                     TempNode.Right := NtD.Right;
  476.                                     NtD.Right.Parent := TempNode;
  477.                               ELSIF NtD.Right = NULL AND NtD.Left /= NULL THEN   --Node to be removed has one left child
  478.                                     Ntd.Left.Parent := NtD.Parent;
  479.                                     IF Is_Left_Child(NtD) = TRUE THEN
  480.                                           NtD.Parent.Left := NtD.Left;
  481.                                     ELSE
  482.                                           Ntd.Parent.Right := Ntd.Left;
  483.                                     END IF;
  484.                               ELSIF NtD.Right /= NULL AND NtD.Left = NULL THEN   --Node to be removed has one right child
  485.                                     Ntd.Right.Parent := Ntd.Parent;
  486.                                     IF Is_Left_Child(NtD) = TRUE THEN
  487.                                           NtD.Parent.Left := NtD.Right;
  488.                                     ELSE
  489.                                           Ntd.Parent.Right := Ntd.Right;
  490.                                     END IF;
  491.                               ELSE                                                                     --Node to be removed has no children
  492.                                     IF Is_Left_Child(NtD) = TRUE THEN
  493.                                           Ntd.Parent.Left := NULL;
  494.                                     ELSE
  495.                                           Ntd.Parent.Right := NULL;
  496.                                     END IF;
  497.                               END IF;
  498.                               Deallocate(Ptr => NtD);
  499.             END Delete;
  500.             ----------------------------------------------------------------------------
  501. END Binary_Tree_String;
  502. --------------------------------------------------------------------------------------------------------------------
  503. --------------------------------------------------------------------------------------------------------------------
  504. --stacks_Generic.ads
  505. --------------------------------------------------------------------------------------------------------------------
  506. --------------------------------------------------------------------------------------------------------------------
  507. WITH lists_generic;
  508. GENERIC
  509. TYPE StackElement IS PRIVATE;
  510. PACKAGE stacks_generic IS
  511.  
  512. TYPE Stack IS LIMITED PRIVATE;
  513.  
  514. StackEmpty : Exception;
  515.  
  516. PROCEDURE MakeStack ( S : IN OUT Stack);
  517.  
  518. PROCEDURE EmptyStack (S : IN OUT Stack);
  519.  
  520. PROCEDURE Push (S : IN OUT Stack; E : IN StackElement);
  521.  
  522. PROCEDURE Pop (S : IN OUT Stack; V : OUT StackElement);
  523.  
  524. FUNCTION Count_Stack(S : IN Stack) RETURN Natural;
  525.  
  526. FUNCTION Top (S : IN Stack) RETURN StackElement;
  527.  
  528. FUNCTION IsEmpty (S : IN Stack) RETURN Boolean;
  529.  
  530. PRIVATE
  531.  
  532. PACKAGE Lists IS NEW lists_generic(ElementType => StackElement);
  533.  
  534. TYPE Stack IS RECORD
  535. Store : Lists.List;
  536. END RECORD;
  537.  
  538. END stacks_generic;
  539. --------------------------------------------------------------------------------------------------------------------
  540. --------------------------------------------------------------------------------------------------------------------
  541. --stacks_Generic.adb
  542. --------------------------------------------------------------------------------------------------------------------
  543. --------------------------------------------------------------------------------------------------------------------
  544. PACKAGE BODY Stacks_Generic IS
  545.  
  546.   PROCEDURE MakeStack (S : IN OUT Stack) IS
  547.   BEGIN
  548.      Lists.Initialize(S.Store);
  549.   END MakeStack;
  550.  
  551.   PROCEDURE EmptyStack (S : IN OUT Stack) IS
  552.   BEGIN
  553.     Lists.MakeEmpty(S.Store);
  554.   END EmptyStack;
  555.  
  556.   FUNCTION IsEmpty (S : IN Stack) RETURN Boolean IS
  557.   BEGIN
  558.     RETURN Lists.IsEmpty(S.Store);
  559.   END IsEmpty;
  560.  
  561.   PROCEDURE Push (S : IN OUT Stack; E : IN StackElement) IS
  562.   BEGIN
  563.      Lists.AddToFront(S.Store, E);
  564.   END Push;
  565.  
  566.   FUNCTION Top (S : IN Stack) RETURN StackElement IS
  567.      Ptr : Lists.Position;
  568.      Elem : StackElement;
  569.   BEGIN
  570.      Ptr := Lists.First(S.Store);
  571.      Elem := Lists.Retrieve(S.Store, Ptr);
  572.      RETURN Elem;
  573.   END Top;
  574.  
  575.   PROCEDURE Pop (S : IN OUT Stack; V : OUT StackElement) IS
  576.   BEGIN
  577.      V := Top(S);
  578.      Lists.RemoveFront(S.Store);
  579.   END Pop;
  580.  
  581.     FUNCTION Count_Stack(S : IN Stack) RETURN Natural IS
  582.        Count : Natural;
  583.     BEGIN
  584.        Count := Lists.Count_List(S.Store);
  585.        RETURN Count;
  586.     END Count_Stack;
  587.  
  588. END Stacks_Generic;
  589. --------------------------------------------------------------------------------------------------------------------
  590. --------------------------------------------------------------------------------------------------------------------
  591. --lists_Generic.ads
  592. --------------------------------------------------------------------------------------------------------------------
  593. --------------------------------------------------------------------------------------------------------------------
  594. GENERIC
  595.  
  596.    TYPE ElementType IS PRIVATE;
  597.    --WITH PROCEDURE DisplayElement(Item : IN ElementType);
  598. PACKAGE lists_generic IS
  599.  
  600.   TYPE Position IS PRIVATE;
  601.   TYPE List IS LIMITED PRIVATE;
  602.  
  603.  
  604.   OutOfSpace: EXCEPTION;
  605.   PastEnd   : EXCEPTION;
  606.   PastBegin : EXCEPTION;
  607.   EmptyList : EXCEPTION;
  608.  
  609.  
  610.  
  611.   PROCEDURE Initialize(L: IN OUT List);
  612.  
  613.   PROCEDURE AddToFront(L: IN OUT List; X: ElementType);
  614.  
  615.   PROCEDURE RemoveFront(L : IN OUT List);
  616.  
  617.   PROCEDURE MakeEmpty(L : IN OUT List);
  618.  
  619.   FUNCTION First (L: List) RETURN Position;
  620.  
  621.   FUNCTION Retrieve (L: IN List; P: IN Position) RETURN ElementType;
  622.  
  623.     FUNCTION Count_List ( L : IN List) RETURN Natural;
  624.  
  625.   FUNCTION  IsFirst   (L: List; P: Position) RETURN Boolean;
  626.   FUNCTION  IsLast    (L: List; P: Position) RETURN Boolean;
  627.   FUNCTION IsEmpty (L: List) RETURN Boolean;
  628.  
  629.  
  630. PRIVATE
  631.  
  632.   TYPE Node;
  633.   TYPE Position IS ACCESS Node;
  634.  
  635.   TYPE Node IS RECORD
  636.     Info: ElementType;
  637.     Link: Position;
  638.   END RECORD;
  639.  
  640.   TYPE List IS RECORD
  641.     Head: Position;
  642.     Tail: Position;
  643.   END RECORD;
  644.  
  645. END Lists_Generic;
  646. --------------------------------------------------------------------------------------------------------------------
  647. --------------------------------------------------------------------------------------------------------------------
  648. --lists_Generic.adb
  649. --------------------------------------------------------------------------------------------------------------------
  650. --------------------------------------------------------------------------------------------------------------------
  651. WITH Unchecked_Deallocation;
  652. PACKAGE BODY Lists_Generic IS
  653.  
  654.   PROCEDURE Dispose IS
  655.     NEW Unchecked_Deallocation(Object => Node, Name => Position);
  656.  
  657.   FUNCTION Allocate (X: ElementType; P: Position) RETURN Position IS
  658.     Result: Position;
  659.   BEGIN
  660.     Result := NEW Node'(Info => X, Link => P);
  661.     RETURN Result;
  662.   EXCEPTION
  663.     WHEN Storage_Error =>
  664.       RAISE OutOfSpace;
  665.   END Allocate;
  666.  
  667.   PROCEDURE Deallocate (P: IN OUT Position) IS
  668.   BEGIN
  669.     Dispose (X => P);
  670.   END Deallocate;
  671.  
  672.   PROCEDURE Initialize(L: IN OUT List) IS
  673.     Previous: Position;
  674.     Current : Position;
  675.   BEGIN
  676.     IF L.Head /= NULL THEN
  677.       Current := L.Head;
  678.       WHILE Current /= NULL LOOP
  679.     Previous := Current;
  680.     Current := Current.Link;
  681.     Deallocate(Previous);
  682.       END LOOP;
  683.       L := (Head => NULL, Tail => NULL);
  684.     END IF;
  685.   END Initialize;
  686. ---------------------------------------------------------
  687.   PROCEDURE AddToFront(L: IN OUT List; X: ElementType) IS
  688.   BEGIN
  689.     L.Head := Allocate(X, L.Head);
  690.     IF L.Tail = NULL THEN
  691.       L.Tail := L.Head;
  692.     END IF;
  693.   END AddToFront;
  694. ---------------------------------------------------------
  695.   PROCEDURE RemoveFront(L : IN OUT List) IS
  696.      Temp : Position;
  697.   BEGIN
  698.      IF IsEmpty(L) = false THEN
  699.      Temp := L.Head;
  700.      L.Head := L.Head.all.Link;
  701.      Deallocate(Temp);
  702.      END IF;
  703.   END RemoveFront;
  704. ---------------------------------------------------------
  705.   PROCEDURE MakeEmpty(L : IN OUT List) IS
  706.      Current : Position := L.Head;
  707.      Next : Position;
  708.   BEGIN
  709.      WHILE Current /= NULL LOOP
  710.         Next := Current.all.Link;
  711.         Deallocate(Current);
  712.         Current := Next;
  713.      END LOOP;
  714.      L := (Head => NULL, Tail => NULL);
  715.   END MakeEmpty;
  716. ---------------------------------------------------------
  717.   FUNCTION First (L: List) RETURN Position IS
  718.   BEGIN
  719.     RETURN L.Head;
  720.   END First;
  721. ---------------------------------------------------------  
  722.   FUNCTION Retrieve (L: IN List; P: IN Position) RETURN ElementType IS
  723.   BEGIN
  724.      RETURN P.all.Info;
  725.   END Retrieve;
  726. ---------------------------------------------------------
  727.   FUNCTION IsEmpty (L: List) RETURN Boolean IS
  728.   BEGIN
  729.     RETURN L.Head = NULL;
  730.   END IsEmpty;
  731. ---------------------------------------------------------
  732.   FUNCTION IsFirst (L: List; P: Position) RETURN Boolean IS
  733.   BEGIN
  734.     RETURN (L.Head /= NULL) AND (P = L.Head);
  735.   END IsFirst;
  736. ---------------------------------------------------------
  737.   FUNCTION IsLast (L: List; P: Position) RETURN Boolean IS
  738.   BEGIN
  739.     RETURN (L.Tail /= NULL) AND (P = L.Tail);
  740.   END IsLast;
  741. ---------------------------------------------------------
  742.     FUNCTION Count_List ( L : IN List) RETURN Natural IS
  743.          Current : Position := L.Head;
  744.          Count : Natural := 0;
  745.     BEGIN
  746.           LOOP
  747.              IF IsLast(L, Current) = TRUE THEN
  748.                 EXIT;
  749.              END IF;
  750.              Current := Current.Link;
  751.              Count := Count + 1;
  752.           END LOOP;
  753.           RETURN Count;
  754.     END Count_List;
  755. ---------------------------------------------------------
  756. END Lists_Generic;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement