Advertisement
Guest User

Untitled

a guest
Apr 4th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 7.43 KB | None | 0 0
  1. with Ada.Text_IO;
  2.  
  3. package body Red_Black_Trees is
  4.    function Create(Item: Item_Type) return Tree_Type_Access is
  5.       Node : Tree_Type_Access;
  6.    begin
  7.       Node := new Tree_Type'(Item, Black, Null, Null, Null);
  8.       return Node;
  9.    end Create;
  10.    
  11. --  --     procedure Rotate_Left(Root : in out Tree_Type_Access; X : in out Tree_Type_Access) is
  12. --  --        Y : Tree_Type_Access;
  13. --  --     begin
  14. --  --        Y := X.Right;
  15. --  --        X.Right := Y.Left;
  16. --  --        
  17. --  --        if X.Right /= Null then
  18. --  --           X.Right.Parent := X;
  19. --  --        end if;
  20. --  --        
  21. --  --        Y.Parent := X.Parent;
  22. --  --        
  23. --  --        if X.Parent = Null then
  24. --  --           Root := Y;
  25. --  --        else if X = X.Parent.Left then
  26. --  --              X.Parent.Left := Y;
  27. --  --           else
  28. --  --              X.Parent.Right := Y;
  29. --  --           end if;
  30. --  --        end if;
  31. --  --        
  32. --  --        Y.Left := X;
  33. --  --        X.Parent := Y;
  34. --  --     end Rotate_Left;
  35. --  --    
  36. --  --     procedure Rotate_Right(Root : in out Tree_Type_Access; Y : in out Tree_Type_Access) is
  37. --  --        X : Tree_Type_Access;
  38. --  --     begin
  39. --  --        X := Y.Left;
  40. --  --        Y.Left := X.Right;
  41. --  --        if X.Right /= Null then
  42. --  --           X.Right.Parent := Y;
  43. --  --        end if;
  44. --  --        
  45. --  --        X.Parent := Y.Parent;
  46. --  --        if X.Parent = Null then
  47. --  --           Root := X;
  48. --  --        else if Y = Y.Parent.Left then
  49. --  --              Y.Parent.Left := X;
  50. --  --           else
  51. --  --              Y.Parent.Right := X;
  52. --  --           end if;
  53. --  --        end if;
  54. --  --        X.Right := Y;
  55. --  --        Y.Parent := X;
  56. --  --     end Rotate_Right;
  57. --    
  58. --     procedure Swap_Color(C1: in out Color_Type; C2: in out Color_Type) is
  59. --        Tmp_Col : Color_Type := C1;
  60. --     begin
  61. --        C1 := C2;
  62. --        C2 := Tmp_Col;
  63. --     end Swap_Color;
  64. --    
  65. --     procedure Balance(Root: in out Tree_Type_Access; Z : in out Tree_Type_Access) is
  66. --        Y : Tree_Type_Access;
  67. --     begin
  68. --        Ada.Text_IO.Put_Line("In Balance");
  69. --        
  70. --        while Z /= Root and then Z.Parent.Col = Red loop
  71. --           if Z.Parent = Z.Parent.Parent.Left then
  72. --              Y := Z.Parent.Parent.Right;
  73. --           else
  74. --              Y := Z.Parent.Parent.Left;
  75. --           end if;
  76. --          
  77. --           if Y.Col = Red then
  78. --              Y.Col := Black;
  79. --              Z.Parent.Col := Black;
  80. --              Z.Parent.Parent.Col := Red;
  81. --              Z := Z.Parent.Parent;
  82. --           else
  83. --              if Z.Parent = Z.Parent.Parent.Left and then Z = Z.Parent.Left then
  84. --                 Swap_Color(Z.Parent.Col, Z.Parent.Parent.Col);
  85. --                 Rotate_Right(Root, Z.Parent.Parent);
  86. --              end if;
  87. --              
  88. --              if Z.Parent = Z.Parent.Parent.Left and then Z = Z.Parent.Right then
  89. --                 Swap_Color(Z.Col, Z.Parent.Parent.Col);
  90. --                 Rotate_Left(Root, Z.Parent);
  91. --                 Rotate_Right(Root, Z.Parent.Parent);
  92. --              end if;
  93. --                
  94. --              if Z.Parent = Z.Parent.Parent.Right and then Z = Z.Parent.Right then
  95. --                 Swap_Color(Z.Parent.Col, Z.Parent.Parent.Col);
  96. --                 Rotate_Left(Root, Z.Parent.Parent);
  97. --              end if;
  98. --              
  99. --              if Z.Parent = Z.Parent.Parent.Right and then Z = Z.Parent.Left then
  100. --                 Swap_Color(Z.Col, Z.Parent.Parent.Col);
  101. --                 Rotate_Right(Root, Z.Parent);
  102. --                 Rotate_Left(Root, Z.Parent.Parent);
  103. --              end if;
  104. --           end if;
  105. --           Root.Col := Black;  
  106. --        end loop;
  107. --        
  108. --        
  109. --     end Balance;
  110. --    
  111. --     function Uncle(Node: Tree_Type_Access) return Tree_Type_Access is
  112. --     begin
  113. --        if Node.Parent = Null or else Node.Parent.Parent = Null then
  114. --           return Null;
  115. --        end if;
  116. --        if Node.Parent = Node.Parent.Left then
  117. --           return Node.Parent.Parent.Right;
  118. --        else
  119. --           return Node.Parent.Parent.Left;
  120. --        end if;
  121. --     end Uncle;
  122. --    
  123. --     function Sibling(Node: Tree_Type_Access) return Tree_Type_Access is
  124. --     begin
  125. --        if Node.Parent = Null then
  126. --           return Null;
  127. --        end if;
  128. --        if Node.Parent = Node.Parent.Left then
  129. --           return Node.Parent.Right;
  130. --        else
  131. --           return Node.Parent.Left;
  132. --        end if;
  133. --     end Sibling;
  134. --    
  135. --     procedure Move_Down(Node: Tree_Type_Access; Node_Parent: Tree_Type_Access) is
  136. --     begin
  137. --        if Node.Parent /= Null then
  138. --           if Node.Parent = Node.Parent.Left then
  139. --              Node.Parent.Left := Node_Parent;
  140. --           else
  141. --              Node.Parent.Right := Node_Parent;
  142. --           end if;
  143. --        end if;
  144. --        Node_Parent.Parent := Node.Parent;
  145. --        Node.Parent := Node_Parent;
  146. --     end Move_Down;
  147. --    
  148. --     procedure Rotate_Left(Node: Tree_Type_Access; X: Tree_Type_Access) is
  149. --        Node_Parent : Tree_Type_Access;
  150. --     begin
  151. --        Node_Parent := X.Right;
  152. --        
  153. --        if X = Node.
  154. --     end Rotate_Left;
  155. --    
  156. --     procedure Insert(Tree: in out Tree_Type_Access; Item: Item_Type) is
  157. --     begin
  158. --        null;
  159. --     end Insert;
  160. --    
  161. --  --        Z : Tree_Type_Access;
  162. --  --        X, Y : Tree_Type_Access;
  163. --  --        
  164. --  --     begin
  165. --  --        Z := new Tree_Type; --'(Item, Black, Null, Null, Null);
  166. --  --        Z.all.Item := Item;
  167. --  --        
  168. --  --        Y := Null;
  169. --  --        X := Tree;
  170. --  --        while X /= Null loop
  171. --  --           Y := X;
  172. --  --           if Z.Item < X.Item then
  173. --  --              X := X.Left;
  174. --  --           else
  175. --  --              X := X.Right;
  176. --  --           end if;
  177. --  --        end loop;
  178. --  --        
  179. --  --        Z.Parent := Y;
  180. --  --        if not (Z.Item < Y.Item) then
  181. --  --           Y.Right := Z;
  182. --  --        else
  183. --  --           Y.Left := Z;
  184. --  --        end if;
  185. --  --        Z.Col := Red;
  186. --  --        
  187. --  --        Balance(Tree, Z);
  188. --  --        --if Z.Item <
  189. --  --  
  190. --  --     end Insert;
  191.  
  192.    
  193.    function Parent(Node: Tree_Type_Access) return Tree_Type_Access is
  194.    begin
  195.       return Node.Parent;
  196.    end Parent;
  197.    
  198.    function Grandparent(Node: Tree_Type_Access) return Tree_Type_Access is
  199.       P : Tree_Type_Access := Parent(Node);
  200.    begin
  201.       if P = Null then
  202.          return Null;
  203.       end if;
  204.       return Parent(P);
  205.    end Grandparent;
  206.    
  207.    function Sibling(Node: Tree_Type_Access) return Tree_Type_Access is
  208.       P : Tree_Type_Access := Parent(Node);
  209.    begin
  210.       if P = Null then
  211.          return Null;
  212.       end if;
  213.       if Node = P.Left then
  214.          return P.Right;
  215.       else
  216.          return P.Left;
  217.       end if;
  218.    end Sibling;
  219.    
  220.    function Uncle(Node: Tree_Type_Access) return Tree_Type_Access is
  221.       P : Tree_Type_Access := Parent(Node);
  222.       G : Tree_Type_Access := Grandparent(Node);
  223.    begin
  224.       if G = Null then
  225.          return Null;
  226.       end if;
  227.       return Sibling(P);
  228.    end Uncle;
  229.    
  230.    procedure Rotate_Left(Node: Tree_Type_Access) is
  231.       Node_New : Tree_Type_Access := Node.Right;
  232.       P : Tree_Type_Access := Parent(N);
  233.    begin
  234.       Node.Right := Node_New.Left;
  235.       Node_New.Left := Node;
  236.    end Uncle;
  237.    
  238. end Red_Black_Trees;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement