home *** CD-ROM | disk | FTP | other *** search
-
- -------------------------------------------------------------------------------
- -- --
- -- Library Unit: AVL -- Generic AVL tree package --
- -- --
- -- Author: Bradley L. Richards --
- -- --
- -- Version Date Notes . . . --
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --
- -- 1.0 12 Mar 86 Initial Version (delete & update not done) --
- -- 1.1 19 Aug 86 Added update and release procedures --
- -- 1.2 7 Sep 86 Added delete procedure, cleaned up code --
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --
- -- --
- -- Library units used: unchecked_deallocation (text_io when debugging) --
- -- --
- -- Description: This package provides generic functions for creating, --
- -- modifying, and accessing AVL trees. AVL trees are binary trees --
- -- which never have more than one level of imbalance between any --
- -- two subtrees. Balance is maintained automatically when the tree --
- -- is being built. --
- -- The data to be maintained in the tree is never actually passed --
- -- to this package. Rather, pointers to the data are passed in, via --
- -- type "node_ptr." Also, comparison functions on the key fields of --
- -- the data must be provided. The package requires a less-than and an --
- -- equality test. --
- -- --
- -------------------------------------------------------------------------------
-
- with unchecked_deallocation;
- --with text_io; use text_io; -- debug
- generic
- type node is limited private;
- type node_ptr is access node; -- points to the data for a node
-
- with function equal (a, b : node_ptr) return boolean;
- with function less_than (a, b : node_ptr) return boolean;
- --with procedure put_data (a : node_ptr); -- debug
-
- package avl is
-
- type tree_ptr is private;
-
- function init_tree return tree_ptr;
- function copy_tree( original : tree_ptr ) return tree_ptr;
-
- procedure add_node( tree : in out tree_ptr; data : in node_ptr;
- duplicate : out boolean );
- procedure delete_node( tree : in out tree_ptr; data : in node_ptr;
- not_found : out boolean );
- function fetch_node( tree : tree_ptr; data : node_ptr) return node_ptr;
- --procedure print_tree( tree : tree_ptr ); -- debug
- procedure release( tree : in out tree_ptr );
- procedure update_node( tree : in tree_ptr; data : in node_ptr;
- not_found : out boolean );
-
- avl_error : exception;
-
- private
-
- type subtree_status is (tall_left, left, same, right, tall_right);
- --package bal_io is new enumeration_io(subtree_status); use bal_io; -- debug
-
- --
- -- tree_node is declared first here, since the compiler otherwise
- -- thinks I'm trying to overload type tree. Another bug?
- --
- type tree_node;
- type tree_ptr is access tree_node;
- type tree_node is
- record
- balance : subtree_status := same;
- left_child, right_child : tree_ptr := null;
- parent : tree_ptr := null;
- data : node_ptr := null;
- end record;
-
- procedure free_AVL is new unchecked_deallocation(tree_node, tree_ptr);
-
- function fetch_node( tree : tree_ptr; data : node_ptr) return tree_ptr;
- function needs_single_rotation(p1, p2, p3, p4 : in tree_ptr) return boolean;
- procedure rotate_singly(p1, p2, p3 : in out tree_ptr);
- procedure rotate_doubly(p1, p2, p3, p4 : in out tree_ptr);
-
- end avl;
-