indexing
	description: "Trees as active structures that may be traversed using a cursor"
	names: cursor_tree, tree
	access: cursor, membership
	contents: generic

deferred class interface
	CURSOR_TREE [G]

feature -- Access

	child_item (i: INTEGER): G
			-- Item in i-th child
		require
			argument_within_bounds: i >= 1 and then i <= arity;
			not_off: not off

	cursor: CURSOR
			-- Current cursor position
			-- (from CURSOR_STRUCTURE)

	has (v: G): BOOLEAN
			-- Does structure include an occurrence of v?
			-- (Reference or object equality,
			-- based on object_comparison.)
		ensure -- from CONTAINER
			not_found_in_empty: Result implies not empty

	index_of (v: like item; i: INTEGER): INTEGER
			-- Index of i-th occurrence of v.
			-- 0 if none.
			-- (Reference or object equality,
			-- based on object_comparison.)
			-- (from LINEAR)
		require -- from LINEAR
			positive_occurrences: i > 0
		ensure -- from LINEAR
			non_negative_result: Result >= 0

	item: G
			-- Item at current position
			-- (from TRAVERSABLE)
		require -- from ACTIVE
			readable: readable
		require -- from TRAVERSABLE
			not_off: not off
		require -- from TRAVERSABLE
			not_off: not off

	occurrences (v: G): INTEGER
		ensure -- from BAG
			non_negative_occurrences: Result >= 0

	parent_item: G
			-- Item in parent.
		require
			not_on_root: not is_root

	search (v: like item)
			-- Move to first position (at or after current
			-- position) where item and v are equal.
			-- (Reference or object equality,
			-- based on object_comparison.)
			-- If no such position ensure that exhausted will be true.
			-- (from LINEAR)
		ensure -- from LINEAR
			object_found: (not exhausted and object_comparison) implies equal (v, item);
			item_found: (not exhausted and not object_comparison) implies v = item

	arity: INTEGER
			-- Number of successors of current element
			-- (from HIERARCHICAL)
		require -- from HIERARCHICAL
			not_off: not off
	
feature -- Measurement

	breadth: INTEGER
			-- Breadth of current level

	depth: INTEGER
			-- Depth of the tree

	level: INTEGER
			-- Level of current node in tree
			-- (Root is on level 1)
	
feature -- Status report

	above: BOOLEAN
			-- Is there no valid cursor position above cursor?

	after: BOOLEAN
			-- Is there no valid cursor position to the right of cursor?

	before: BOOLEAN
			-- Is there no valid cursor position to the left of cursor?

	below: BOOLEAN
			-- Is there no valid cursor position below cursor?

	changeable_comparison_criterion: BOOLEAN
			-- May object_comparison be changed?
			-- (Answer: yes by default.)
			-- (from CONTAINER)

	empty: BOOLEAN
			-- Is there no element?
			-- (from CONTAINER)

	exhausted: BOOLEAN
			-- Has structure been completely explored?
			-- (from LINEAR)
		ensure -- from LINEAR
			exhausted_when_off: off implies Result

	extendible: BOOLEAN
			-- May new items be added?

	is_leaf: BOOLEAN
			-- Is cursor on a leaf?

	is_root: BOOLEAN
			-- Is cursor on root?

	isfirst: BOOLEAN
			-- Is cursor on first sibling?

	islast: BOOLEAN
			-- Is cursor on last sibling?

	object_comparison: BOOLEAN
			-- Must search operations use equal rather than =
			-- for comparing references? (Default: no, use =.)
			-- (from CONTAINER)

	off: BOOLEAN
			-- Is there no current item?
			-- (True if empty)

	prunable: BOOLEAN
			-- May items be removed?
			-- (from COLLECTION)

	readable: BOOLEAN
			-- Is there a current item that may be read?

	valid_cursor (p: CURSOR): BOOLEAN
			-- Can the cursor be moved to position p?
			-- (from CURSOR_STRUCTURE)

	valid_cursor_index (i: INTEGER): BOOLEAN
			-- Can cursor be moved to i-th child?
			-- 0 is before and arity + 1 is after.

	writable: BOOLEAN
			-- Is there a current item that may be modified?
	
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
		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
		ensure -- from CONTAINER
			reference_comparison: not object_comparison
	
feature -- Cursor movement

	back
			-- Move cursor one position backward.

	breadth_forth
			-- Move cursor to next position in breadth-first order.
			-- If the active node is the last in
			-- breadth-first order, the cursor ends up off.
		require
			not_off: not off

	down (i: INTEGER)
			-- Move cursor one level downward:
			-- to i-th child if there is one,
			-- or after if i = arity + 1,
			-- or before if i = 0.
		require -- from HIERARCHICAL
			not_off: not off;
			argument_within_bounds: i >= 1 and i <= arity
		require else
			not_before: not before;
			not_after: not after;
			not_below: not below;
			valid_cursor_index: (above and i = 0) or else valid_cursor_index (i)
		ensure then
			gone_before: (i = 0) implies before

	forth
			-- Move cursor one position forward.

	go_last_child
			-- Go to the last child of current parent.
			-- No effect if below
		require -- LINEAR
			precursor: True
		require else
			not_above: not above

	go_to (p: CURSOR)
			-- Move cursor to position p.
			-- (from CURSOR_STRUCTURE)
		require -- from CURSOR_STRUCTURE
			cursor_position_valid: valid_cursor (p)

	level_back
			-- Move cursor to previous position of current level.

	level_forth
			-- Move cursor to next position of current level.

	postorder_forth
			-- Move cursor to next position in postorder.
			-- If the active node is the last in
			-- postorder, the cursor ends up off.
		require
			not_off: not off

	postorder_start
			-- Move cursor to first position in postorder.
			-- Leave cursor off if tree is empty.

	preorder_forth
			-- Move cursor to next position in preorder.
			-- If the active node is the last in
			-- preorder, the cursor ends up off.
		require -- from LINEAR
			not_after: not after

	start
			-- Move cursor to root.
			-- Leave cursor off if empty.
		ensure then
			on_root_unless_empty: not empty implies is_root

	start_on_level (l: INTEGER)
			-- Move the cursor to the first position
			-- of the l-th level counting from root.
		require
			argument_within_bounds: l >= 1 and then depth >= l
		ensure
			level_expected: level = l;
			is_first: isfirst

	up
			-- Move cursor one level upward to parent,
			-- or above if is_root holds.
		require -- from HIERARCHICAL
			not_off: not off
		require else
			not_above: not above
		ensure then
			not_before: not before;
			not_after: not after;
			not_below: not below;
			coherency: (not old off and above) = (old is_root)
	
feature -- Element change

	extend (v: G)
			-- Add v after last child.
			-- Make v the first_child if below and place
			-- cursor before.
		require -- from COLLECTION
			extendible: extendible
		ensure -- from COLLECTION
			item_inserted: has (v)
		ensure then -- from BAG
			one_more_occurrence: occurrences (v) = old (occurrences (v)) + 1

	fill (other: CURSOR_TREE [G])
			-- Fill with as many items of other
			-- as possible.
			-- The representations of other and current structure
			-- need not be the same.
		require
			is_empty: empty

	container_fill (other: CONTAINER [G])
			-- Fill with as many items of other as possible.
			-- The representations of other and current structure
			-- need not be the same.
			-- (from COLLECTION)
		require -- from COLLECTION
			other_not_void: other /= void;
			extendible

	fill_from_active (other: CURSOR_TREE [G])
			-- Copy subtree of other's active node
			-- onto active node of current tree.
		require
			cursor_on_leaf: is_leaf

	merge_left (other: CURSOR_TREE [G])
			-- Merge the items of other into current structure to
			-- the left of cursor position.
		require
			other_exists: other /= void;
			not_before: not before;
			not_above: not above;
			only_one_root: (level = 1) implies empty

	merge_right (other: CURSOR_TREE [G])
			-- Merge the items of other into current structure to
			-- the right of cursor position.
		require
			other_exists: other /= void;
			not_after: not after;
			not_above: not above;
			only_one_root: (level = 1) implies empty

	put (v: G)
			-- Put v at cursor position.
			-- (Synonym for replace)
		require -- from COLLECTION
			extendible: extendible
		ensure -- from COLLECTION
			item_inserted: has (v)

	put_left (v: G)
			-- Add v to the left of cursor position.
		require
			not_before: not before;
			not_above: not above;
			only_one_root: (level = 1) implies empty

	put_right (v: G)
			-- Add v to the right of cursor position.
		require
			not_after: not after;
			not_above: not above;
			only_one_root: (level = 1) implies empty

	replace (v: G)
			-- Replace current item by v.
			-- (from ACTIVE)
		require -- from ACTIVE
			writable: writable
		ensure -- from ACTIVE
			item_replaced: item = v
	
feature -- Removal

	prune_all (v: G)
			-- Remove all occurrences of v.
			-- (Reference or object equality,
			-- based on object_comparison.)
			-- (from COLLECTION)
		require -- from COLLECTION
			prunable
		ensure -- from COLLECTION
			no_more_occurrences: not has (v)

	remove
			-- Remove current item.
			-- (from ACTIVE)
		require -- from ACTIVE
			prunable: prunable;
			writable: writable

	wipe_out
			-- Remove all items.
			-- (from COLLECTION)
		require -- from COLLECTION
			prunable
		ensure -- from COLLECTION
			wiped_out: empty
	
feature -- Conversion

	linear_representation: LINEAR [G]
			-- Representation as a linear structure
			-- (from LINEAR)
	
feature -- Duplication

	child_tree (i: INTEGER): like Current
			-- Subtree rooted at i-th child
		require
			argument_within_bounds: i >= 1 and then i <= arity;
			not_off: not off

	parent_tree: like Current
			-- Subtree rooted at parent
		require
			not_on_root: not is_root;
			not_off: not off

	subtree: like Current
			-- Subtree rooted at current node
		require
			not_off: not off
	
invariant

		-- from GENERAL
	reflexive_equality: standard_is_equal (Current);
	reflexive_conformance: conforms_to (Current);
	non_negative_depth: depth >= 0;
	non_negative_breadth: breadth >= 0;
	is_leaf_definition: not off implies is_leaf = (arity = 0);
	above_property: above implies (arity <= 1);
	on_tree: (isfirst or islast or is_leaf or is_root) implies not off;
	off_definition: off = after or before or above or below;
	below_constraint: below implies ((after or before) and not above);
	above_constraint: above implies not (before or after or below);
	after_constraint: after implies not (before or above);
	before_constaint: before implies not (after or above);
	empty_below_constraint: (empty and (after or before)) implies below;
		-- from HIERARCHICAL
	non_negative_successor_count: arity >= 0;
		-- from TRAVERSABLE
	empty_constraint: empty implies off;
		-- from ACTIVE
	writable_constraint: writable implies readable;
	empty_constraint: empty implies (not readable) and (not writable);
		-- from LINEAR
	after_constraint: after implies off;

end -- class CURSOR_TREE