Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 2.87 KB | None | 0 0
  1. --
  2. --  Copyright (c) 2010, Edward O'Callaghan
  3. --
  4. --  Permission to use, copy, modify, and/or distribute this software for any
  5. --  purpose with or without fee is hereby granted, provided that the above
  6. --  copyright notice and this permission notice appear in all copies.
  7. --
  8. --  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. --  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. --  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. --  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. --  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. --  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. --  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. --
  16.  
  17. --
  18. --  SPARK Verified Ada2005 Red-Back Tree in-memory implementation.
  19.  
  20. --
  21. --  Red-Black Tree Properties:
  22. --
  23. --  1.) A node is either Red or Black, Root node is always Black.
  24. --  2.) All leave nodes are Black.
  25. --  3.) Both children of every Red node are black.
  26. --  4.) Every simple* path from a given node to any of its descendant
  27. --       leaves contains the same number of Black nodes.
  28. --  *A path with no repeated vertices is called a simple path.
  29.  
  30. generic
  31.    type Element_Type is private;
  32.  
  33.    --  package ADT.RB_Tree is
  34. package RB_Tree is
  35.  
  36.    type Tree_Type is limited private;
  37.    type Node_Type is limited private;
  38.  
  39.    --
  40.    --  Red-Black Tree Constructor
  41.    procedure Initialize (Tree : in out Tree_Type; Element : in Element_Type);
  42.    --# derives Tree from *, Element;
  43.  
  44.    --
  45.    --  Red-Black Tree Destructor
  46.    procedure Destroy (Tree : in out Tree_Type);
  47.    --# derives Tree from *;
  48.  
  49.    --
  50.    --  Red-Black Tree Insertion
  51.    --  Time Complexity:
  52.    --  Average Case: O( Log[N] )
  53.    --  Worst Case: O( Log[N] )
  54.    procedure Insert (Tree : in out Tree_Type; Element : in Element_Type);
  55.    --# derives Tree from *, Element;
  56.  
  57.    --
  58.    --  Red-Black Tree Deletion
  59.    --  Time Complexity:
  60.    --  Average Case: O( Log[N] )
  61.    --  Worst Case: O( Log[N] )
  62.    procedure Delete (Tree : in out Tree_Type; Node : in Node_Type);
  63.    --# derives Tree from *, Node;
  64.  
  65.    --
  66.    --  Red-Black Tree Search
  67.    --  Time Complexity:
  68.    --  Average Case: O( Log[N] )
  69.    --  Worst Case: O( Log[N] )
  70.    procedure Search
  71.      (Tree    : in Tree_Type;
  72.       Element : in Element_Type;
  73.       Found   : out Boolean;
  74.       Node    : out Node_Type);
  75.    --# derives Found, Node from Tree, Element;
  76.    --# post Found -> Tree (Node) = Element;
  77.    --  If Found is False then Node is undefined.
  78.  
  79. private
  80.    type Node_Colour is (Red, Black);
  81.    type Tree_Type is new Node_Type;
  82.  
  83.    type Node_Type is record
  84.       Colour      : Node_Colour      := Black;
  85.       Parent      : access Node_Type := null;
  86.       Right, Left : access Node_Type := null;
  87.    end record;
  88. end RB_Tree;
  89. --  end ADT.RB_Tree;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement