indexing
description: "Trees where each node has a fixed number of children (The number of children is arbitrary but cannot be changed once the node has been created"
names: fixed_tree, tree, fixed_list
representation: recursive, array
access: cursor, membership
contents: generic
class interface
FIXED_TREE [G]
create
make (n: INTEGER; v: G)
-- Create node with n void children and item v.
require
valid_number_of_children: n >= 0
ensure
node_item: item = v;
node_arity: arity = n
feature -- Initialization
make (n: INTEGER; v: G)
-- Create node with n void children and item v.
require
valid_number_of_children: n >= 0
ensure
node_item: item = v;
node_arity: arity = n
setup (other: like Current)
-- Perform actions on a freshly created object so that
-- the contents of other can be safely copied onto it.
-- (from ARRAY)
ensure -- from GENERAL
consistent (other)
feature -- Access
child_item: like item
-- Item of active child
require -- from TREE
readable: child_readable
arity: INTEGER
-- (from FIXED_LIST)
child_cursor: CURSOR
-- Current cursor position
-- (from FIXED_LIST)
first_child: like parent
-- Leftmost child
require -- from CHAIN
not_empty: not is_leaf
require -- from TREE
is_not_leaf: not is_leaf
has (v: G): BOOLEAN
-- Does subtree include v?
-- (Reference or object equality,
-- based on object_comparison.)
-- (from TREE)
ensure -- from CONTAINER
not_found_in_empty: Result implies not is_leaf
child_index: INTEGER
-- Current position in the list
-- (from FIXED_LIST)
index_of (v: like child; i: INTEGER): INTEGER
-- Index of i-th occurrence of item identical to v.
-- (Reference or object equality,
-- based on object_comparison.)
-- 0 if none.
-- (from CHAIN)
require -- from LINEAR
positive_occurrences: i > 0
ensure -- from LINEAR
non_negative_result: Result >= 0
is_sibling (other: like parent): BOOLEAN
-- Are current node and other siblings?
-- (from TREE)
ensure -- from TREE
not_root: Result implies not is_root;
other_not_root: Result implies not other.is_root;
same_parent: Result = not is_root and other.parent = parent
item: G
-- Content of cell.
-- (from CELL)
child: FIXED_TREE [G]
-- Current item
-- (from FIXED_LIST)
require -- from ACTIVE
readable: fl_readable
require -- from TRAVERSABLE
not_off: not child_off
require -- from TREE
readable: readable_child
last_child: like first_child
-- Item at last position
-- (from FIXED_LIST)
require -- from CHAIN
not_empty: not is_leaf
require -- from TREE
is_not_leaf: not is_leaf
left_sibling: like parent
-- Left neighbor, if any
require -- from TREE
is_not_root: not is_root
ensure -- from TREE
is_sibling: is_sibling (Result);
right_is_current: (Result /= void) implies (Result.right_sibling = Current)
sequential_occurrences (v: FIXED_TREE [G]): INTEGER
-- Number of times v appears.
-- (Reference or object equality,
-- based on object_comparison.)
-- (from LINEAR)
ensure -- from BAG
non_negative_occurrences: Result >= 0
parent: FIXED_TREE [G]
-- Parent of current node
right_sibling: like parent
-- Right neighbor, if any
require -- from TREE
is_not_root: not is_root
ensure -- from TREE
is_sibling: is_sibling (Result);
left_is_current: (Result /= void) implies (Result.left_sibling = Current)
frozen infix "@" (i: INTEGER): FIXED_TREE [G]
-- Entry at index i, if in index interval
-- Was declared in ARRAY as synonym of item, @ and entry.
-- (from ARRAY)
require -- from TABLE
valid_key: valid_index (k)
feature {ANY} -- Access
area: SPECIAL [FIXED_TREE [G]]
-- Special data zone
-- (from TO_SPECIAL)
feature -- Measurement
capacity: INTEGER
-- Number of available indices
-- Was declared in ARRAY as synonym of count and capacity.
-- (from ARRAY)
count: INTEGER
-- Number of items
-- (from TREE)
occurrences (v: FIXED_TREE [G]): INTEGER
-- Number of times v appears in structure
-- (from ARRAY)
ensure -- from BAG
non_negative_occurrences: Result >= 0
feature -- Comparison
is_equal (other: like Current): BOOLEAN
-- Is array made of the same items as other?
-- (from ARRAY)
require -- from GENERAL
other_not_void: other /= void
ensure -- from GENERAL
symmetric: Result implies other.is_equal (Current);
consistent: standard_is_equal (other) implies Result
feature -- Status report
child_after: BOOLEAN
-- Is there no valid cursor position to the right of cursor?
-- (from LIST)
child_before: BOOLEAN
-- Is there no valid cursor position to the left of cursor?
-- (from LIST)
changeable_comparison_criterion: BOOLEAN
-- May object_comparison be changed?
-- (Answer: yes by default.)
-- (from CONTAINER)
fl_changeable_object_criterion: BOOLEAN
-- May object_comparison be changed?
-- (Answer: yes by default.)
-- (from CONTAINER)
child_contractable: BOOLEAN
-- May items be removed?
ensure
Result = not child_off
child_isfirst: BOOLEAN
-- Is cursor under first child?
-- (from TREE)
ensure -- from TREE
not_is_leaf: Result implies not is_leaf
ensure -- from CHAIN
valid_position: Result implies not is_leaf
ensure -- from TREE
not_is_leaf: Result implies not is_leaf
child_islast: BOOLEAN
-- Is cursor under last child?
-- (from TREE)
ensure -- from TREE
not_is_leaf: Result implies not is_leaf
ensure -- from CHAIN
valid_position: Result implies not is_leaf
ensure -- from TREE
not_is_leaf: Result implies not is_leaf
child_readable: BOOLEAN
-- Is there a current child_item to be read?
-- (from TREE)
child_writable: BOOLEAN
-- Is there a current child_item that may be modified?
-- (from TREE)
consistent (other: like Current): BOOLEAN
-- Is object in a consistent state so that other
-- may be copied onto it? (Default answer: yes).
-- (from ARRAY)
empty: BOOLEAN
-- Is structure empty of items?
-- (from TREE)
exhausted: BOOLEAN
-- Has structure been completely explored?
-- (from LINEAR)
ensure -- from LINEAR
exhausted_when_off: child_off implies Result
Full: BOOLEAN is true
-- Is tree full?
is_leaf: BOOLEAN
-- Are there no children?
-- (from TREE)
is_root: BOOLEAN
-- Is there no parent?
-- (from TREE)
object_comparison: BOOLEAN
-- Must search operations use equal rather than =
-- for comparing references? (Default: no, use =.)
-- (from CONTAINER)
fl_object_comparison: BOOLEAN
-- Must search operations use equal rather than =
-- for comparing references? (Default: no, use =.)
-- (from CONTAINER)
child_off: BOOLEAN
-- Is there no current item?
-- (from CHAIN)
prunable: BOOLEAN
-- May items be removed?
-- (from FIXED_LIST)
Readable: BOOLEAN is true
-- (from TREE)
readable_child: BOOLEAN
-- Is there a current child to be read?
-- (from TREE)
Resizable: BOOLEAN is false
-- May capacity be changed? (Answer: no.)
-- (from FIXED)
valid_cursor (p: CURSOR): BOOLEAN
-- Is p a valid cursor?
-- (from FIXED_LIST)
valid_cursor_index (i: INTEGER): BOOLEAN
-- Is i correctly bounded for cursor movement?
-- (from TREE)
ensure -- from TREE
valid_cursor_index_definition: Result = (i >= 0) and (i <= arity + 1)
ensure -- from CHAIN
valid_cursor_index_definition: Result = ((i >= 0) and (i <= arity + 1))
ensure -- from TREE
valid_cursor_index_definition: Result = (i >= 0) and (i <= arity + 1)
valid_index (i: INTEGER): BOOLEAN
-- Is i within the bounds of the array?
-- (from ARRAY)
ensure then -- from CHAIN
valid_index_definition: Result = ((i >= 1) and (i <= arity))
Writable: BOOLEAN is true
-- Is there a current item that may be modified?
-- (from TREE)
writable_child: BOOLEAN
-- Is there a current child that may be modified?
-- (from TREE)
feature -- Status setting
compare_objects
-- Ensure that future search operations will use equal
-- rather than = for comparing references.
-- (from CONTAINER)
require -- from CONTAINER
changeable_comparison_criterion
require -- from CONTAINER
changeable_comparison_criterion
ensure -- from CONTAINER
object_comparison
ensure -- from CONTAINER
object_comparison
compare_references
-- Ensure that future search operations will use =
-- rather than equal for comparing references.
-- (from CONTAINER)
require -- from CONTAINER
changeable_comparison_criterion
require -- from CONTAINER
changeable_comparison_criterion
ensure -- from CONTAINER
reference_comparison: not object_comparison
ensure -- from CONTAINER
reference_comparison: not object_comparison
feature -- Cursor movement
child_back
-- Move cursor to previous position, if any.
-- (from FIXED_LIST)
require -- from BILINEAR
not_before: not child_before
ensure then -- from BILINEAR
moved_back: child_index = old child_index - 1
child_finish
-- Move cursor to last position.
-- (from FIXED_LIST)
ensure then -- from CHAIN
at_last: not is_leaf implies child_islast
ensure then -- from TREE
is_last_child: not is_leaf implies child_islast
child_forth
-- Move cursor to next position, if any.
-- (from FIXED_LIST)
require -- from LINEAR
not_after: not child_after
ensure then -- from LIST
moved_forth: child_index = old child_index + 1
child_go_i_th (i: INTEGER)
-- Move cursor to i-th position.
-- (from FIXED_LIST)
require -- from CHAIN
valid_cursor_index: valid_cursor_index (i)
require else -- from TREE
valid_cursor_index: valid_cursor_index (i)
ensure -- from CHAIN
position_expected: child_index = i
ensure then -- from TREE
position: child_index = i;
is_before: (i = 0) implies child_before;
is_after: (i = arity + 1) implies child_after
child_go_to (p: CURSOR)
-- Move cursor to element remembered in p.
-- (from FIXED_LIST)
require -- from CURSOR_STRUCTURE
cursor_position_valid: valid_cursor (p)
move (i: INTEGER)
-- Move cursor i positions.
-- (from FIXED_LIST)
ensure -- from CHAIN
too_far_right: (old child_index + i > arity) implies exhausted;
too_far_left: (old child_index + i < 1) implies exhausted;
expected_index: (not exhausted) implies (child_index = old child_index + i)
search_child (v: like child)
-- Move to first position (at or after current
-- position) where item and v are equal.
-- If structure does not include v ensure that
-- exhausted will be true.
-- (Reference or object equality,
-- based on object_comparison.)
-- (from BILINEAR)
ensure -- from LINEAR
object_found: (not exhausted and object_comparison) implies equal (v, child);
item_found: (not exhausted and not object_comparison) implies v = child
child_start
-- Move cursor to first position.
-- (from FIXED_LIST)
ensure then -- from CHAIN
at_first: not is_leaf implies child_isfirst
ensure then -- from TREE
is_first_child: not is_leaf implies child_isfirst
feature -- Element change
append (s: SEQUENCE [FIXED_TREE [G]])
-- Append a copy of s.
-- (from SEQUENCE)
require -- from SEQUENCE
argument_not_void: s /= void
ensure -- from SEQUENCE
new_count: arity >= old arity
child_put (v: like item)
-- Replace current child item with v
-- Was declared in FIXED_TREE as synonym of child_put and child_replace.
require -- from TREE
child_writable: child_writable
ensure -- from TREE
item_inserted: child_item = v
child_replace (v: like item)
-- Replace current child item with v
-- Was declared in FIXED_TREE as synonym of child_put and child_replace.
require -- from TREE
child_writable: child_writable
ensure -- from TREE
item_inserted: child_item = v
extend (v: like child)
-- Add v to end.
-- Move index to the current item.
-- (from FIXED_LIST)
require -- from COLLECTION
extendible: fl_extendible
ensure -- from COLLECTION
item_inserted: has (v)
ensure then -- from BAG
one_more_occurrence: occurrences (v) = old (occurrences (v)) + 1
fill (other: TREE [G])
-- Fill with as many items of other as possible.
-- The representations of other and current node
-- need not be the same.
-- (from TREE)
put (v: like item)
-- Make v the cell's item.
-- Was declared in CELL as synonym of put and replace.
-- (from CELL)
require -- CELL
precursor: True
require -- from TREE
is_writable: writable
ensure -- from CELL
item_inserted: item = v
ensure -- from TREE
item_inserted: item = v
ensure -- from CELL
item_inserted: item = v
frozen put_i_th (v: like array_item; i: INTEGER)
-- Replace i-th entry, if in index interval, by v.
-- Was declared in ARRAY as synonym of put and enter.
-- (from ARRAY)
require -- from TABLE
valid_key: valid_index (k)
put_child (n: like parent)
-- Make n the node's child.
-- Was declared in FIXED_TREE as synonym of put_child and replace_child.
ensure then
child_replaced: n.parent = Current
put_left (v: like item)
-- Add v to the left of current node.
require
is_not_root: not is_root;
has_left_sibling: left_sibling /= void
ensure
item_put: left_sibling.item = v
put_left_sibling (other: like parent)
-- Make other the left sibling of current node.
require
is_not_root: not is_root;
has_left_sibling: left_sibling /= void
ensure
left_sibling_replaced: left_sibling = other
put_right (v: like item)
-- Add v to the right of current node.
require
is_not_root: not is_root;
has_right_sibling: right_sibling /= void
ensure
item_put: right_sibling.item = v
put_right_sibling (other: like parent)
-- Make other the right sibling of current node.
require
is_not_root: not is_root;
has_right_sibling: right_sibling /= void
ensure
right_sibling_replaced: right_sibling = other
replace (v: like item)
-- Make v the cell's item.
-- Was declared in CELL as synonym of put and replace.
-- (from CELL)
require -- CELL
precursor: True
require -- from TREE
is_writable: writable
ensure -- from CELL
item_inserted: item = v
ensure -- from TREE
item_inserted: item = v
ensure -- from CELL
item_inserted: item = v
replace_child (n: like parent)
-- Make n the node's child.
-- Was declared in FIXED_TREE as synonym of put_child and replace_child.
require -- from TREE
writable_child: writable_child;
was_root: n.is_root
ensure -- from TREE
child_replaced: child = n
ensure then
child_replaced: n.parent = Current
sprout
-- Make current node a root.
-- (from TREE)
feature -- Removal
remove_child
-- Remove active child.
ensure then
child_removed: child = void
wipe_out
-- Make array empty.
-- (from ARRAY)
require -- from COLLECTION
prunable
ensure -- from COLLECTION
wiped_out: is_leaf
feature -- Transformation
swap (i: INTEGER)
-- Exchange item at i-th position with item
-- at cursor position.
-- (from FIXED_LIST)
require -- from CHAIN
not_off: not child_off;
valid_index: valid_index (i)
feature -- Conversion
binary_representation: BINARY_TREE [G]
-- Convert to binary tree representation:
-- first child becomes left child,
-- right sibling becomes right child.
-- (from TREE)
ensure -- from TREE
result_is_root: Result.is_root;
result_has_no_right_child: not Result.has_right
linear_representation: LINEAR [G]
-- Representation as a linear structure
-- (from TREE)
feature -- Duplication
copy (other: like Current)
-- Reinitialize by copying all the items of other.
-- (This is also used by clone.)
-- (from ARRAY)
require -- ARRAY
precursor: True
require -- from GENERAL
other_not_void: other /= void;
type_identity: same_type (other)
ensure then -- from ARRAY
equal_areas: area.is_equal (other.area)
ensure -- from GENERAL
is_equal: is_equal (other)
ensure then -- from ARRAY
equal_areas: area.is_equal (other.area)
duplicate (n: INTEGER): like Current
-- Copy of sub-tree beginning at cursor position and
-- having min (n, arity - child_index + 1)
-- children.
require -- from CHAIN
not_off_unless_after: child_off implies child_after;
valid_subchain: n >= 0
require -- from TREE
not_child_off: not child_off;
valid_sublist: n >= 0
feature -- Inapplicable
prune (v: FIXED_TREE [G])
-- Remove first occurrence of v if any.
-- (Precondition is false.)
-- (from ARRAY)
require -- from COLLECTION
prunable: prunable
require -- from TREE
is_child: n.parent = Current
ensure -- from TREE
n_is_root: n.is_root
invariant
-- from GENERAL
reflexive_equality: standard_is_equal (Current);
reflexive_conformance: conforms_to (Current);
-- from TREE
leaf_definition: is_leaf = (arity = 0);
child_off_definition: child_off = child_before or child_after;
child_before_definition: child_before = (child_index = 0);
child_isfirst_definition: child_isfirst = (not is_leaf and child_index = 1);
child_islast_definition: child_islast = (not is_leaf and child_index = arity);
child_after_definition: child_after = (child_index >= arity + 1);
child_consistency: child_readable implies child.parent = Current;
-- from LIST
before_definition: child_before = (child_index = 0);
after_definition: child_after = (child_index = arity + 1);
-- from CHAIN
non_negative_index: child_index >= 0;
index_small_enough: child_index <= arity + 1;
off_definition: child_off = ((child_index = 0) or (child_index = arity + 1));
isfirst_definition: child_isfirst = ((not is_leaf) and (child_index = 1));
islast_definition: child_islast = ((not is_leaf) and (child_index = arity));
-- from BILINEAR
not_both: not (child_after and child_before);
empty_property: is_leaf implies (child_after or child_before);
before_constraint: child_before implies child_off;
-- from LINEAR
after_constraint: child_after implies child_off;
-- from TRAVERSABLE
empty_constraint: is_leaf implies child_off;
-- from FINITE
empty_definition: is_leaf = (arity = 0);
non_negative_count: arity >= 0;
-- from ARRAY
non_negative_count: arity >= 0;
-- from BOUNDED
valid_count: arity <= capacity;
-- from FIXED
not_resizable: not resizable;
end -- class FIXED_TREE