note
	description: "Trees where the children of each node are kept in an array"
	library: "Free implementation of ELKS library"
	legal: "See notice at end of class."
	status: "See notice at end of class."
	names: tree
	representation: recursive, array
	access: cursor, membership
	contents: generic
	date: "$Date: 2018-12-15 18:01:30 +0000 (Sat, 15 Dec 2018) $"
	revision: "$Revision: 102607 $"

class 
	ARRAYED_TREE [G]

create 
	make

feature {NONE} -- Initialization

	default_create
			-- Process instances of classes with no creation clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end

	make (n: INTEGER_32; v: G)
			-- Create node with item v.
			-- Allocate space for n children.
		require
			valid_number_of_children: n >= 0
		do
			create arrayed_list.make (n)
			replace (v)
		ensure
			node_item: item = v
		end
	
feature -- Access

	child_item: like item
			-- Item in current child node
			-- (from TREE)
		require -- from TREE
			readable: child_readable
		do
			check
					attached child as c
			then
				Result := c.item
			end
		end

	generating_type: TYPE [detachable ARRAYED_TREE [G]]
			-- Type of current object
			-- (type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generating_type_not_void: Result /= Void
		end

	generator: STRING_8
			-- Name of current object's generating class
			-- (base class of the type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generator_not_void: Result /= Void
			generator_not_empty: not Result.is_empty
		end

	item: G
			-- Content of cell.
			-- (from CELL)

	left_sibling: like parent
			-- Left neighbor if any
		require -- from TREE
			is_not_root: not is_root
		local
			p: like parent
		do
			p := parent
			if p /= Void and then position_in_parent > 1 then
				Result := p.array_item (position_in_parent - 1)
			end
		ensure -- from TREE
			is_sibling: Result /= Void implies is_sibling (Result)
			right_is_current: (Result /= Void) implies (Result.right_sibling = Current)
		end

	parent: detachable like Current
			-- Parent of current node

	right_sibling: like parent
			-- Right neighbor if any
		require -- from TREE
			is_not_root: not is_root
		local
			p: like parent
		do
			p := parent
			if p /= Void and then position_in_parent < p.arity then
				Result := p.array_item (position_in_parent + 1)
			end
		ensure -- from TREE
			is_sibling: Result /= Void implies is_sibling (Result)
			left_is_current: (Result /= Void) implies (Result.left_sibling = Current)
		end
	
feature -- Measurement

	child_capacity: INTEGER_32
			-- Maximal number of children
			-- (from TREE)
		do
			Result := arity
		end

	count: INTEGER_32
			-- Number of items
			-- (from TREE)
		do
			Result := subtree_count + 1
		end
	
feature {NONE} -- Measurement

	estimated_count_of (other: ITERABLE [G]): INTEGER_32
			-- Estimated number of elements in other.
			-- (from CONTAINER)
		do
			if attached {FINITE [G]} other as f then
				Result := f.count
			elseif attached {READABLE_INDEXABLE [G]} other as r then
				Result := r.upper - r.lower + 1
			end
		ensure -- from CONTAINER
			instance_free: class
			non_negative_result: Result >= 0
		end
	
feature -- Comparison

	frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void
			-- or attached to isomorphic object structures?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_deep_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			shallow_implies_deep: standard_equal (a, b) implies Result
			both_or_none_void: (a = Void) implies (Result = (b = Void))
			same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b))
			symmetric: Result implies deep_equal (b, a)
		end

	frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached
			-- to objects considered equal?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b))
		end

	frozen is_deep_equal alias "≡≡≡" (other: ARRAYED_TREE [G]): BOOLEAN
			-- Are Current and other attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			shallow_implies_deep: standard_is_equal (other) implies Result
			same_type: Result implies same_type (other)
			symmetric: Result implies other.is_deep_equal (Current)
		end

	is_equal (other: ARRAYED_TREE [G]): BOOLEAN
			-- Does other contain the same elements?
			-- (Reference or object equality,
			-- based on object_comparison.)
			-- (from TREE)
		require -- from ANY
			other_not_void: other /= Void
		do
			if Current = other then
				Result := True
			else
				Result := (is_empty = other.is_empty) and (object_comparison = other.object_comparison) and (child_capacity = other.child_capacity)
				if Result and not is_empty then
					Result := tree_is_equal (Current, other)
				end
			end
		ensure -- from ANY
			symmetric: Result implies other ~ Current
			consistent: standard_is_equal (other) implies Result
		end

	node_is_equal (other: ARRAYED_TREE [G]): BOOLEAN
			-- Is other equal to Current?
			-- (from TREE)
		require -- from TREE
			other_not_void: other /= Void
		do
			if object_comparison then
				Result := item ~ other.item
			else
				Result := item = other.item
			end
		end

	frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached to
			-- field-by-field identical objects of the same type?
			-- Always uses default object comparison criterion.
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.standard_is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b))
		end

	frozen standard_is_equal alias "" (other: ARRAYED_TREE [G]): BOOLEAN
			-- Is other attached to an object of the same type
			-- as current object, and field-by-field identical to it?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)
		end
	
feature -- Status report

	changeable_comparison_criterion: BOOLEAN
			-- May object_comparison be changed?
			-- (Answer: yes by default.)
			-- (from CONTAINER)
		do
			Result := True
		end

	child_isfirst: BOOLEAN
			-- Is cursor under first child?
			-- (from TREE)
		do
			Result := not is_leaf and child_index = 1
		ensure -- from TREE
			not_is_leaf: Result implies not is_leaf
		end

	child_islast: BOOLEAN
			-- Is cursor under last child?
			-- (from TREE)
		do
			Result := not is_leaf and child_index = child_capacity
		ensure -- from TREE
			not_is_leaf: Result implies not is_leaf
		end

	child_readable: BOOLEAN
			-- Is there a current child_item to be read?
			-- (from TREE)
		do
			Result := not child_off and then (child /= Void)
		end

	child_writable: BOOLEAN
			-- Is there a current child_item that may be modified?
			-- (from TREE)
		do
			Result := not child_off and then (child /= Void)
		end

	conforms_to (other: ANY): BOOLEAN
			-- Does type of current object conform to type
			-- of other (as per Eiffel: The Language, chapter 13)?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		end

	al_empty: BOOLEAN
		obsolete "ELKS 2000: Use `is_empty' instead. [2017-05-31]"
			-- Is there no element?
			-- (from CONTAINER)
		do
			Result := is_empty
		end

	Extendible: BOOLEAN = True
			-- May new items be added?
			-- (from DYNAMIC_TREE)

	has (v: G): BOOLEAN
			-- Does subtree include v?
			-- (Reference or object equality,
			-- based on object_comparison.)
			-- (from TREE)
		do
			if object_comparison then
				Result := v ~ item or else subtree_has (v)
			else
				Result := v = item or else subtree_has (v)
			end
		ensure -- from CONTAINER
			not_found_in_empty: Result implies not is_empty
		end

	is_empty: BOOLEAN
			-- Is structure empty of items?
			-- (from TREE)
		do
			Result := False
		end

	is_leaf: BOOLEAN
			-- Are there no children?
			-- (from TREE)
		do
			Result := arity = 0
		end

	is_root: BOOLEAN
			-- Is there no parent?
			-- (from TREE)
		do
			Result := parent = Void
		end

	is_sibling (other: attached like parent): BOOLEAN
			-- Are current node and other siblings?
			-- (from TREE)
		require -- from TREE
			other_exists: other /= Void
		do
			Result := not is_root and other.parent = parent
		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
		end

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

	Readable: BOOLEAN = True
			-- (from TREE)

	same_type (other: ANY): BOOLEAN
			-- Is type of current object identical to type of other?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))
		end

	valid_cursor_index (i: INTEGER_32): BOOLEAN
			-- Is i correctly bounded for cursor movement?
			-- (from TREE)
		do
			Result := (i >= 0) and (i <= child_capacity + 1)
		ensure -- from TREE
			valid_cursor_index_definition: Result = (i >= 0) and (i <= child_capacity + 1)
		end

	Writable: BOOLEAN = True
			-- Is there a current item 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: changeable_comparison_criterion
		do
			object_comparison := True
		ensure -- from CONTAINER
				object_comparison
		end

	compare_references
			-- Ensure that future search operations will use =
			-- rather than equal for comparing references.
			-- (from CONTAINER)
		require -- from CONTAINER
			changeable_comparison_criterion: changeable_comparison_criterion
		do
			object_comparison := False
		ensure -- from CONTAINER
			reference_comparison: not object_comparison
		end
	
feature -- Element change

	child_extend (v: like item)
			-- Add v at end.
			-- Do not move child cursor.
		local
			n: like parent
		do
			n := new_cell (v)
			if object_comparison then
				n.compare_objects
			else
				n.compare_references
			end
			al_extend (n)
		end

	child_put (v: like item)
			-- Replace current child item with v.
			-- Was declared in ARRAYED_TREE as synonym of child_replace.
		require -- from TREE
			child_writable: child_writable
		local
			c: like child
		do
			c := child
			if c /= Void then
				if object_comparison then
					c.compare_objects
				else
					c.compare_references
				end;
				c.replace (v)
			end
		ensure -- from TREE
			item_inserted: child_item = v
		end

	child_put_left (v: like item)
			-- Add v to the left of cursor position.
			-- Do not move child cursor.
		require -- from DYNAMIC_TREE
			not_child_before: not child_before
		local
			n: like parent
		do
			n := new_cell (v)
			if object_comparison then
				n.compare_objects
			else
				n.compare_references
			end
			al_put_left (n)
		end

	child_put_right (v: like item)
			-- Add v to the right of cursor position.
			-- Do not move child cursor.
		require -- from DYNAMIC_TREE
			not_child_after: not child_after
		local
			n: like parent
		do
			n := new_cell (v)
			if object_comparison then
				n.compare_objects
			else
				n.compare_references
			end
			al_put_right (n)
		end

	child_replace (v: like item)
			-- Replace current child item with v.
			-- Was declared in ARRAYED_TREE as synonym of child_put.
		require -- from TREE
			child_writable: child_writable
		local
			c: like child
		do
			c := child
			if c /= Void then
				if object_comparison then
					c.compare_objects
				else
					c.compare_references
				end;
				c.replace (v)
			end
		ensure -- from TREE
			item_inserted: child_item = v
		end

	fill (other: TREE [G])
		obsolete "Fill the tree explicitly. [2018-11-30]"
			-- Fill with as many items of other as possible.
			-- The representations of other and current node
			-- need not be the same.
			-- (from TREE)
		do
			replace (other.item)
			fill_subtree (other)
		end

	merge_tree_after (other: like Current)
			-- Merge children of other into current structure
			-- after cursor position. Do not move cursor.
			-- Make other a leaf.
		require -- from DYNAMIC_TREE
			not_child_off: not child_off
			other_exists: other /= Void
		local
			l_list: ARRAYED_LIST [like other]
		do
			attach (other)
			create l_list.make (1);
			l_list.extend (other)
			al_merge_left (l_list)
		ensure -- from DYNAMIC_TREE
			other_is_leaf: other.is_leaf
		end

	merge_tree_before (other: like Current)
			-- Merge children of other into current structure
			-- before cursor position. Do not move cursor.
			-- Make other a leaf.
		require -- from DYNAMIC_TREE
			not_child_off: not child_off
			other_exists: other /= Void
		local
			l_list: ARRAYED_LIST [like other]
		do
			attach (other)
			create l_list.make (1);
			l_list.extend (other)
			al_merge_left (l_list)
		ensure -- from DYNAMIC_TREE
			other_is_leaf: other.is_leaf
		end

	put (v: like item)
			-- Make v the cell's item.
			-- Was declared in CELL as synonym of replace.
			-- (from CELL)
		require -- from TREE
			is_writable: Writable
		do
			item := v
		ensure -- from TREE
			item_inserted: item = v
		ensure -- from CELL
			item_inserted: item = v
		end

	put_child (n: like child)
			-- Add n to the list of children.
			-- Do not move child cursor.
		require -- from TREE
			non_void_argument: n /= Void
		do
			if object_comparison then
				n.compare_objects
			else
				n.compare_references
			end
			al_extend (n);
			n.attach_to_parent (Current)
		end

	put_child_left (n: like child)
			-- Add n to the left of cursor position.
			-- Do not move cursor.
		require -- from DYNAMIC_TREE
			not_child_before: not child_before
			non_void_argument: n /= Void
		do
			if object_comparison then
				n.compare_objects
			else
				n.compare_references
			end
			al_put_left (n);
			n.attach_to_parent (Current)
		end

	put_child_right (n: like child)
			-- Add n to the right of the cursor position.
			-- Do not move cursor.
		require -- from DYNAMIC_TREE
			not_child_after: not child_after
			non_void_argument: n /= Void
		do
			if object_comparison then
				n.compare_objects
			else
				n.compare_references
			end
			al_put_right (n);
			n.attach_to_parent (Current)
		end

	replace (v: like item)
			-- Make v the cell's item.
			-- Was declared in CELL as synonym of put.
			-- (from CELL)
		require -- from TREE
			is_writable: Writable
		do
			item := v
		ensure -- from TREE
			item_inserted: item = v
		ensure -- from CELL
			item_inserted: item = v
		end

	replace_child (n: like child)
			-- Make n the node's current child.
		require -- from TREE
			writable_child: writable_child
		do
			if object_comparison then
				n.compare_objects
			else
				n.compare_references
			end
			if child_off then
				al_extend (n)
			else
				al_replace (n)
			end;
			n.attach_to_parent (Current)
		ensure -- from TREE
			child_replaced: child = n
		ensure then
			child_replaced: n.parent = Current
		end

	sprout
			-- Make current node a root.
			-- (from TREE)
		local
			p: like parent
		do
			p := parent
			if p /= Void then
				p.prune (Current)
			end
		end
	
feature -- Removal

	forget_left
			-- Forget all left siblings.
		local
			i: INTEGER_32
			old_idx: INTEGER_32
			p: like parent
		do
			p := parent
			if p /= Void and then position_in_parent < p.arity then
				old_idx := p.child_index
				from
					i := 1
				until
					i = position_in_parent
				loop
					p.child_go_i_th (i);
					p.remove_child
					i := i + 1
				end;
				p.child_go_i_th (old_idx)
			end
		end

	forget_right
			-- Forget all right siblings.
		local
			i: INTEGER_32
			old_idx: INTEGER_32
			p: like parent
		do
			p := parent
			if p /= Void and then position_in_parent < p.arity then
				old_idx := p.child_index
				from
					i := position_in_parent + 1
				until
					i > p.arity
				loop
					p.child_go_i_th (i);
					p.remove_child
					i := i + 1
				end;
				p.child_go_i_th (old_idx)
			end
		end

	remove_child
			-- Remove child at cursor position.
			-- Move cursor to the next sibling, or after if none.
		require -- from DYNAMIC_TREE
			child_not_off: not child_off
		local
			c: like child
		do
			c := child
			if c /= Void then
				c.attach_to_parent (Void)
			end
			al_remove
		ensure -- from DYNAMIC_TREE
			new_arity: arity = old arity - 1
			new_child_index: child_index = old child_index
		end

	remove_left_child
			-- Remove item to the left of cursor position.
			-- Do not move cursor.
		require -- from DYNAMIC_TREE
			is_not_first: not child_isfirst
		do
			child_back
			remove_child
		ensure -- from DYNAMIC_TREE
			new_arity: arity = old arity - 1
			new_child_index: child_index = old child_index - 1
		end

	remove_right_child
			-- Remove item to the right of cursor position.
			-- Do not move cursor.
		require -- from DYNAMIC_TREE
			is_not_last: not child_islast
		do
			child_forth
			remove_child
			child_back
		ensure -- from DYNAMIC_TREE
			new_arity: arity = old arity - 1
			new_child_index: child_index = old child_index
		end
	
feature -- Conversion

	binary_representation: BINARY_TREE [G]
			-- Convert to binary tree representation:
			-- first child becomes left child,
			-- right sibling becomes right child.
			-- (from TREE)
		local
			current_sibling: detachable BINARY_TREE [G]
			c: like first_child
		do
			create Result.make (item)
			if not is_leaf then
				c := first_child
				if c /= Void then
					Result.put_left_child (c.binary_representation)
				end
				from
					child_start
					child_forth
					current_sibling := Result.left_child
				until
					child_after
				loop
					if current_sibling /= Void then
						c := child
						if c /= Void then
							current_sibling.put_right_child (c.binary_representation)
						end
						current_sibling := current_sibling.right_child
					end
					child_forth
				end
			end
		ensure -- from TREE
			result_is_root: Result.is_root
			result_has_no_right_child: not Result.has_right
		end

	fill_from_binary (b: BINARY_TREE [G])
			-- Fill from a binary tree representation.
			-- Left child becomes first child.
			-- Right child becomes right sibling.
			-- Any right child of b is ignored.
			-- (from DYNAMIC_TREE)
		local
			current_node: detachable BINARY_TREE [G]
			c: like child
		do
			replace (b.item)
			wipe_out
			if b.has_left then
				from
					current_node := b.left_child
				until
					current_node = Void
				loop
					child_put_right (current_node.item)
					child_forth
					c := child
					if c /= Void then
						c.fill_from_binary (current_node)
					end
					current_node := current_node.right_child
				end
			end
		end

	linear_representation: LINEAR [G]
			-- Representation as a linear structure
			-- (from TREE)
		local
			al: ARRAYED_LIST [G]
		do
			create al.make (count);
			al.start;
			al.extend (item)
			fill_list (al)
			Result := al
		end
	
feature -- Duplication

	frozen clone (other: detachable ANY): like other
		obsolete "Use `twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- equal to other
			--
			-- For non-void other, clone calls copy;
			-- to change copying/cloning semantics, redefine copy.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.twin
			end
		ensure -- from ANY
			instance_free: class
			equal: Result ~ other
		end

	copy (other: ARRAYED_TREE [G])
			-- Copy contents from other.
			-- (from TREE)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		local
			i: INTEGER_32
			old_idx: INTEGER_32
			tmp_tree: ARRAYED_TREE [G]
			c: like child
		do
			tmp_tree := clone_node (other)
			if not other.is_leaf then
				tree_copy (other, tmp_tree)
			end
			standard_copy (tmp_tree)
			old_idx := child_index
			from
				i := 1
			until
				i > child_capacity
			loop
				child_go_i_th (i)
				c := child
				if c /= Void then
					c.attach_to_parent (Current)
				end
				i := i + 1
			end
			child_go_i_th (old_idx)
		ensure -- from ANY
			is_equal: Current ~ other
		end

	frozen deep_clone (other: detachable ANY): like other
		obsolete "Use `deep_twin' instead. [2017-05-31]"
			-- Void if other is void: otherwise, new object structure
			-- recursively duplicated from the one attached to other
			-- (from ANY)
		do
			if other /= Void then
				Result := other.deep_twin
			end
		ensure -- from ANY
			instance_free: class
			deep_equal: deep_equal (other, Result)
		end

	frozen deep_copy (other: ARRAYED_TREE [G])
			-- Effect equivalent to that of:
			--		copy (other . deep_twin)
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		do
			copy (other.deep_twin)
		ensure -- from ANY
			deep_equal: deep_equal (Current, other)
		end

	frozen deep_twin: ARRAYED_TREE [G]
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			deep_twin_not_void: Result /= Void
			deep_equal: deep_equal (Current, Result)
		end

	duplicate (n: INTEGER_32): like Current
		obsolete "Create and initialize a new tree explicitly. [2018-11-30]"
			-- Copy of sub-tree beginning at cursor position and
			-- having min (n, arity - child_index + 1)
			-- children.
		require -- from TREE
			not_child_off: not child_off
			valid_sublist: n >= 0
		local
			counter: INTEGER_32
			pos: CURSOR
			c: like child
		do
			from
				Result := new_node
				pos := child_cursor;
				Result.child_start
			until
				child_after or else (counter = n)
			loop
				c := child
				if c /= Void then
					Result.replace_child (c.duplicate_all)
				end;
				Result.child_forth
				child_forth
				counter := counter + 1
			end
			child_go_to (pos)
		end

	frozen standard_clone (other: detachable ANY): like other
		obsolete "Use `standard_twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.standard_twin
			end
		ensure -- from ANY
			instance_free: class
			equal: standard_equal (Result, other)
		end

	frozen standard_copy (other: ARRAYED_TREE [G])
			-- Copy every field of other onto corresponding field
			-- of current object.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)
		end

	frozen standard_twin: ARRAYED_TREE [G]
			-- New object field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			standard_twin_not_void: Result /= Void
			equal: standard_equal (Result, Current)
		end

	frozen twin: ARRAYED_TREE [G]
			-- New object equal to Current
			-- twin calls copy; to change copying/twinning semantics, redefine copy.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			twin_not_void: Result /= Void
			is_equal: Result ~ Current
		end
	
feature -- Basic operations

	frozen as_attached: attached ARRAYED_TREE [G]
		obsolete "Remove calls to this feature. [2017-05-31]"
			-- Attached version of Current.
			-- (Can be used during transitional period to convert
			-- non-void-safe classes to void-safe ones.)
			-- (from ANY)
		do
			Result := Current
		end

	frozen default: detachable ARRAYED_TREE [G]
			-- Default value of object's type
			-- (from ANY)
		do
		end

	frozen default_pointer: POINTER
			-- Default value of type POINTER
			-- (Avoid the need to write p.default for
			-- some p of type POINTER.)
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end

	default_rescue
			-- Process exception for routines with no Rescue clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end
	
feature -- Inapplicable

	extend (v: G)
			-- Add v as new child.
		do
		end
	
feature {RECURSIVE_CURSOR_TREE} -- Inapplicable

	set_child (n: like parent)
			-- Set child to n.
		require -- from DYNAMIC_TREE
			non_void_argument: n /= Void
		do
		end
	
feature {ARRAYED_TREE} -- Implementation

	copy_node (n: like Current)
			-- Copy content of n except tree data into Current.
		require -- from TREE
			is_root: is_root
			is_leaf: is_leaf
			not_void: n /= Void
		local
			l_arrayed_list: like arrayed_list
		do
			l_arrayed_list := arrayed_list
			standard_copy (n)
			arrayed_list := l_arrayed_list
			parent := Void
		ensure -- from TREE
			object_comparison_copied: object_comparison = n.object_comparison
			same_arity: arity = old arity
			same_item: item = old item
			result_is_root: is_root
			result_is_leaf: is_leaf
		end

	new_node: like Current
		obsolete "Create and initialize a new tree explicitly. [2018-11-30]"
			-- A newly created instance of the same type,
			-- with the same arity and node value.
			-- This feature may be redefined in descendants so as to
			-- produce an adequately allocated and initialized object.
		do
			create Result.make (arity, item)
		end
	
feature {ARRAYED_TREE}{DYNAMIC_TREE} -- Implementation

	duplicate_all: like Current
		obsolete "Create and initialize a new tree explicitly. [2018-11-30]"
			-- Copy of sub-tree including all children
		local
			pos: CURSOR
			c: like child
		do
			from
				Result := new_node
				pos := child_cursor;
				Result.child_start
				child_start
			until
				child_off
			loop
				c := child
				if c /= Void then
					Result.replace_child (c.duplicate_all)
				end;
				Result.child_forth
				child_forth
			end
			child_go_to (pos)
		end

	fill_subtree (other: TREE [G])
		obsolete "Fill subtree explicitly. [2018-11-30]"
			-- Fill children with children of other
		require -- from  TREE
			True
		local
			temp: like parent
			c: detachable TREE [G]
		do
			from
				other.child_start
				child_start
			until
				child_after
			loop
				c := other.child
				if c /= Void then
					create temp.make (other.arity, other.child_item);
					temp.fill_subtree (c)
					replace_child (temp)
				end
				child_forth;
				other.child_forth
			end
		end
	
feature {ARRAYED_TREE}{TREE} -- Implementation

	attach_to_parent (n: like parent)
			-- Make n parent of current node;
		do
			parent := n
		ensure -- from TREE
			new_parent: parent = n
		end

	clone_node (n: like Current): like Current
			-- Clone node n.
		require -- from TREE
			not_void: n /= Void
		do
			create Result.make (n.arity, n.item);
			Result.copy_node (n)
		ensure -- from TREE
			result_is_root: Result.is_root
			result_is_leaf: Result.is_leaf
		end
	
feature {NONE} -- Implementation

	arrayed_list: ARRAYED_LIST [like Current]
			-- arrayed list of arrayed_tree.

	attach (other: like Current)
			-- Attach all children of other to current node.
			-- Put other in mode off.
		local
			c: like child
		do
			from
				other.child_start
			until
				other.child_off
			loop
				c := other.child;
				other.child_forth
				if c /= Void then
					c.attach_to_parent (Current)
				end
			end
		end

	child_remove
			-- Remove item of current child
			-- (from TREE)
		do
		end

	do_all_internal (an_agent: PROCEDURE [G]; a_tree_node: like Current)
			-- Apply action to every child.
		require
			non_void_agent: an_agent /= Void
		do
			an_agent (a_tree_node.item)
			from
				a_tree_node.child_start
			until
				a_tree_node.child_off
			loop
				do_all_internal (an_agent, a_tree_node.child);
				a_tree_node.child_forth
			end
		end

	new_cell (v: like item): like Current
			-- New node with value v and no children.
		do
			create Result.make (0, v);
			Result.attach_to_parent (Current)
		end

	position_in_parent: INTEGER_32
			-- Position of current node in parent
		local
			p: like parent
		do
			p := parent
			if p /= Void then
				Result := p.index_of (Current, 1)
			end
		end

	remove
			-- Remove current item
			-- (from TREE)
		do
		end

	tree_copy (other, tmp_tree: ARRAYED_TREE [G])
			-- Generic implementation of copy. other is copied onto
			-- Current. tmp_tree is used as temporary storage during
			-- copying. Since it cannot be created locally because of the
			-- generic implementation, it has to be passed in.
			-- (from TREE)
		require -- from TREE
			other_not_empty: other /= Void and then not other.is_empty
			other_not_leaf: not other.is_leaf
			tmp_tree_exists: tmp_tree /= Void
			same_rule: object_comparison = other.object_comparison
		local
			i: INTEGER_32
			p1, p2, node: ARRAYED_TREE [G]
			c1: like child
			other_stack, tmp_stack: LINKED_STACK [ARRAYED_TREE [G]]
			idx_stack, orgidx_stack: LINKED_STACK [INTEGER_32]
		do
			create other_stack.make
			create tmp_stack.make
			create idx_stack.make
			create orgidx_stack.make
			if other.object_comparison then
				tmp_tree.compare_objects
			end;
			orgidx_stack.put (other.child_index)
			from
				i := 1
				p1 := other
				p2 := tmp_tree
			invariant
				same_count: other_stack.count = tmp_stack.count and tmp_stack.count = idx_stack.count
			until
				i > p1.child_capacity and other_stack.is_empty
			loop
				p1.child_go_i_th (i);
				p2.child_go_i_th (i)
				if p1.child_readable then
					check
						source_tree_not_void: p1 /= Void
						target_tree_not_void: p2 /= Void
						source_child_not_void: p1.child /= Void
						target_child_void: p2.readable_child implies p2.child = Void
					end
					c1 := p1.child
					if c1 = Void then
						check
							source_child_not_void: p1.child /= Void
						end
					else
						node := clone_node (c1)
						check
							not_the_same: node /= p1.child
						end;
						p2.put_child (node)
						check
							node_is_child: node = p2.child
							comparison_mode_ok: node.object_comparison = c1.object_comparison
							p1_consistent: c1.parent = p1
							p2_consistent: node.parent = p2
						end
						if not c1.is_leaf then
							other_stack.put (p1);
							tmp_stack.put (p2);
							idx_stack.put (i + 1)
							p1 := c1
							p2 := node;
							orgidx_stack.put (p1.child_index)
							i := 0
						end
					end
				end
				if i <= p1.child_capacity then
					i := i + 1
				else
					from
					invariant
						same_count: other_stack.count = tmp_stack.count and tmp_stack.count = idx_stack.count
					until
						other_stack.is_empty or else i <= p1.child_capacity
					loop
						p1.child_go_i_th (orgidx_stack.item);
						p2.child_go_i_th (orgidx_stack.item)
						check
							child_indices_equal: p1.child_index = p2.child_index
						end
						p1 := other_stack.item
						p2 := tmp_stack.item
						check
							p1_not_void: p1 /= Void
							p2_not_void: p2 /= Void
						end
						i := idx_stack.item;
						other_stack.remove;
						tmp_stack.remove;
						idx_stack.remove;
						orgidx_stack.remove
					end
				end
			end;
			other.child_go_i_th (orgidx_stack.item);
			tmp_tree.child_go_i_th (orgidx_stack.item);
			orgidx_stack.remove
			check
				tree_stacks_empty: other_stack.is_empty and tmp_stack.is_empty
				at_root: p1 = other and p2 = tmp_tree
				copy_correct: other ~ tmp_tree
				index_stack_empty: orgidx_stack.is_empty
			end
		end

	tree_is_equal (t1, t2: ARRAYED_TREE [G]): BOOLEAN
			-- Are t1 and t2 recursively equal?
			-- (from TREE)
		require -- from TREE
			trees_exist: t1 /= Void and t2 /= Void
			trees_not_empty: not t1.is_empty and not t2.is_empty
			same_rule: t1.object_comparison = t2.object_comparison
		local
			p1, p2: ARRAYED_TREE [G]
			c1, c2: like child
			t1_stack, t2_stack: LINKED_STACK [ARRAYED_TREE [G]]
			orgidx1_stack, orgidx2_stack: LINKED_STACK [INTEGER_32]
			l_current_cursor, l_other_cursor: like child_cursor
		do
			l_current_cursor := t1.child_cursor
			l_other_cursor := t2.child_cursor
			if t1.is_leaf and t2.is_leaf then
				Result := t1.item ~ t2.item
			elseif t1.is_leaf xor t2.is_leaf then
				Result := False
			else
				create t1_stack.make
				create t2_stack.make
				create orgidx1_stack.make
				create orgidx2_stack.make;
				orgidx1_stack.put (t1.child_index);
				orgidx2_stack.put (t2.child_index)
				from
					Result := True
					p1 := t1
					p2 := t2;
					p1.child_start;
					p2.child_start
				invariant
					same_count: t1_stack.count = t2_stack.count
				until
					not Result or else p1.child_after and t1_stack.is_empty
				loop
					check
						p1_not_void: p1 /= Void
						p2_not_void: p2 /= Void
					end
					if p1.child_readable and p2.child_readable and p1.child_capacity = p2.child_capacity then
						Result := p1.node_is_equal (p2)
						c1 := p1.child
						c2 := p2.child
						if c1 = Void or else c2 = Void then
							check
									False
							end
						else
							if not (c1.is_leaf or c2.is_leaf) then
								t1_stack.put (p1);
								t2_stack.put (p2)
								p1 := c1
								p2 := c2
								Result := p1.node_is_equal (p2);
								orgidx1_stack.put (p1.child_index);
								orgidx2_stack.put (p2.child_index);
								p1.child_start;
								p2.child_start
							elseif c1.is_leaf xor c2.is_leaf then
								Result := False
							else
								Result := c1.node_is_equal (c2)
							end
						end
					elseif p1.child_capacity /= p2.child_capacity or else (p1.child_readable xor p2.child_readable) then
						Result := False
					end
					if not p1.child_after then
						p1.child_forth;
						p2.child_forth
					else
						from
						invariant
							same_count: t1_stack.count = t2_stack.count
						until
							t1_stack.is_empty or else not p1.child_after
						loop
							p1 := t1_stack.item
							p2 := t2_stack.item;
							p1.child_forth;
							p2.child_forth;
							t1_stack.remove;
							t2_stack.remove;
							orgidx1_stack.remove;
							orgidx2_stack.remove
						end
					end
				end
				if not Result then
					from
					invariant
						same_count: t1_stack.count = t2_stack.count and orgidx1_stack.count = orgidx2_stack.count
					until
						orgidx1_stack.count = 1
					loop
						p1.child_go_i_th (orgidx1_stack.item);
						p2.child_go_i_th (orgidx2_stack.item)
						p1 := t1_stack.item
						p2 := t2_stack.item
						check
							p1_not_void: p1 /= Void
							p2_not_void: p2 /= Void
						end;
						t1_stack.remove;
						t2_stack.remove;
						orgidx1_stack.remove;
						orgidx2_stack.remove
					end
					check
						tree_stacks_empty: t1_stack.is_empty and t2_stack.is_empty
						at_root: p1 = t1 and p2 = t2
						p1_not_void: p1 /= Void
						p2_not_void: p2 /= Void
					end;
					p1.child_go_i_th (orgidx1_stack.item);
					p2.child_go_i_th (orgidx2_stack.item);
					orgidx1_stack.remove;
					orgidx2_stack.remove
					check
						index_stacks_empty: orgidx1_stack.is_empty and orgidx2_stack.is_empty
					end
				end
			end;
			t1.child_go_to (l_current_cursor);
			t2.child_go_to (l_other_cursor)
		end
	
feature {DYNAMIC_TREE} -- Implementation

	new_tree: like Current
		obsolete "Create and initialize a new tree explicitly. [2018-11-30]"
			-- A newly created instance of the same type.
		do
			create Result.make (0, item)
		ensure -- from DYNAMIC_TREE
			result_exists: Result /= Void
			result_item: Result.item = item
		end
	
feature {TREE} -- Implementation

	fill_list (al: ARRAYED_LIST [G])
			-- Fill al with all the children's items.
			-- (from TREE)
		local
			c: like child
		do
			from
				child_start
			until
				child_off
			loop
				c := child
				if c /= Void then
					al.extend (child_item);
					c.fill_list (al)
				end
				child_forth
			end
		end

	subtree_count: INTEGER_32
			-- Number of items in children
			-- (from TREE)
		local
			pos: CURSOR
			c: like child
		do
			Result := arity
			from
				pos := child_cursor
				child_start
			until
				child_off
			loop
				c := child
				if c /= Void then
					Result := Result + c.subtree_count
				end
				child_forth
			end
			child_go_to (pos)
		end

	subtree_has (v: G): BOOLEAN
			-- Do children include v?
			-- (Reference or object equality,
			-- based on object_comparison.)
			-- (from TREE)
		local
			cursor: CURSOR
			c: like child
		do
			cursor := child_cursor
			from
				child_start
			until
				child_off or else Result
			loop
				if child /= Void then
					if object_comparison then
						Result := v ~ child_item
					else
						Result := v = child_item
					end
				end
				child_forth
			end
			from
				child_start
			until
				child_off or else Result
			loop
				c := child
				if c /= Void then
					Result := c.subtree_has (v)
				end
				child_forth
			end
			child_go_to (cursor)
		end
	
feature -- Access: children

	arity: INTEGER_32
			-- Number of children
		do
			Result := arrayed_list.count
		end

	array_item (n: INTEGER_32): like Current
		do
			Result := arrayed_list.i_th (n)
		end

	child: attached like parent
			-- Current child node
		require -- from TREE
			readable: readable_child
		do
			Result := arrayed_list.item
		end

	child_after: BOOLEAN
			-- Is there no valid child position to the right of cursor?
		do
			Result := arrayed_list.after
		end

	child_back
			-- Move cursor to previous child.
		do
			arrayed_list.back
		end

	child_before: BOOLEAN
			-- Is there no valid child position to the left of cursor?
		do
			Result := arrayed_list.before
		end

	child_cursor: CURSOR
			-- Current cursor position
		do
			Result := arrayed_list.cursor
		end

	child_finish
			-- Move cursor to last child.
		do
			arrayed_list.finish
		end

	child_forth
			-- Move cursor to next child.
		do
			arrayed_list.forth
		end

	child_go_i_th (i: INTEGER_32)
			-- Move cursor to i-th child.
		do
			arrayed_list.go_i_th (i)
		ensure then -- from TREE
			position: child_index = i
		end

	child_go_to (p: CURSOR)
			-- Move cursor to position p.
		do
			arrayed_list.go_to (p)
		end

	child_index: INTEGER_32
			-- Index of current child
		do
			Result := arrayed_list.index
		ensure -- from TREE
			valid_index: Result >= 0 and Result <= arity + 1
		end

	child_move (i: INTEGER_32)
		do
			arrayed_list.move (i)
		end

	child_off: BOOLEAN
			-- Is there no current child?
		do
			Result := arrayed_list.off
		end

	child_start
			-- Move cursor to first child.
		do
			arrayed_list.start
		end

	do_all (an_agent: PROCEDURE [G])
			-- Apply an_agent to every child nodes in the tree.
		do
			do_all_internal (an_agent, Current)
		end

	first_child: like parent
			-- Leftmost child
		require -- from TREE
			is_not_leaf: not is_leaf
		do
			Result := arrayed_list.first
		end

	index_of (v: like Current; i: INTEGER_32): INTEGER_32
		do
			Result := arrayed_list.index_of (v, i)
		end

	last_child: like first_child
			-- Right most child
		require -- from TREE
			is_not_leaf: not is_leaf
		do
			Result := arrayed_list.last
		end

	prune (n: like child)
			-- Remove n from the children.
		require -- from TREE
			is_child: n.parent = Current
		do
			arrayed_list.prune (n)
		ensure -- from TREE
			n_is_root: n.is_root
		end

	readable_child: BOOLEAN
			-- Is there a current child to be read?
		do
			Result := arrayed_list.readable
		end

	search_child (v: like Current)
		do
			arrayed_list.search (v)
		end

	wipe_out
			-- Remove all children.
		do
			arrayed_list.wipe_out
		ensure -- from TREE
			is_leaf: is_leaf
		end

	writable_child: BOOLEAN
			-- Is there a current child that may be modified?
		do
			Result := arrayed_list.writable
		end
	
feature -- Iteration

	new_cursor: TREE_ITERATION_CURSOR [G]
			-- Fresh cursor associated with current structure
			-- (from TREE)
		do
			create Result.make (Current)
		ensure -- from ITERABLE
			result_attached: Result /= Void
		end
	
feature -- Output

	Io: STD_FILES
			-- Handle to standard file setup
			-- (from ANY)
		once
			create Result;
			Result.set_output_default
		ensure -- from ANY
			instance_free: class
			io_not_void: Result /= Void
		end

	out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		do
			Result := tagged_out
		ensure -- from ANY
			out_not_void: Result /= Void
		end

	print (o: detachable ANY)
			-- Write terse external representation of o
			-- on standard output.
			-- (from ANY)
		local
			s: READABLE_STRING_8
		do
			if attached o then
				s := o.out
				if attached {READABLE_STRING_32} s as s32 then
					Io.put_string_32 (s32)
				elseif attached {READABLE_STRING_8} s as s8 then
					Io.put_string (s8)
				else
					Io.put_string_32 (s.as_string_32)
				end
			end
		ensure -- from ANY
			instance_free: class
		end

	frozen tagged_out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			tagged_out_not_void: Result /= Void
		end
	
feature -- Platform

	Operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
		once
			create Result
		ensure -- from ANY
			instance_free: class
			operating_environment_not_void: Result /= Void
		end
	
feature {NONE} -- Retrieval

	frozen internal_correct_mismatch
			-- Called from runtime to perform a proper dynamic dispatch on correct_mismatch
			-- from MISMATCH_CORRECTOR.
			-- (from ANY)
		local
			l_msg: STRING_32
			l_exc: EXCEPTIONS
		do
			if attached {MISMATCH_CORRECTOR} Current as l_corrector then
				l_corrector.correct_mismatch
			else
				create l_msg.make_from_string ("Mismatch: ".as_string_32)
				create l_exc;
				l_msg.append (generating_type.name_32);
				l_exc.raise_retrieval_exception (l_msg)
			end
		end
	
feature {NONE} -- private access arrayed_list

	al_duplicate (n: INTEGER_32): ARRAYED_LIST [like Current]
		obsolete "[
			Create a new container explicitly using `make_from_iterable` if available.
			Otherwise, replace a call to the feature with code that creates and initializes container.
			[2018-11-30]
		]"
		do
			Result := arrayed_list.duplicate (n)
		end

	al_extend (v: like Current)
		do
			arrayed_list.extend (v)
		end

	al_extendible: BOOLEAN
		do
			Result := arrayed_list.extendible
		end

	al_fill (other: CONTAINER [like Current])
		do
			arrayed_list.fill (other)
		end

	al_full: BOOLEAN
		do
			Result := arrayed_list.full
		end

	al_has (v: like Current): BOOLEAN
		do
			Result := arrayed_list.has (v)
		end

	al_lin_rep: LINEAR [like Current]
		do
			Result := arrayed_list.linear_representation
		end

	al_merge_left (v: ARRAYED_LIST [like Current])
		do
			arrayed_list.merge_left (v)
		end

	al_merge_right (v: ARRAYED_LIST [like Current])
		do
			arrayed_list.merge_right (v)
		end

	al_object_comparison: BOOLEAN
		do
			Result := arrayed_list.object_comparison
		end

	al_put (v: like Current)
		do
			arrayed_list.put (v)
		end

	al_put_left (v: like Current)
		do
			arrayed_list.put_left (v)
		end

	al_put_right (v: like Current)
		do
			arrayed_list.put_right (v)
		end

	al_remove
		do
			arrayed_list.remove
		end

	al_remove_left
		do
			arrayed_list.remove_left
		end

	al_remove_right
		do
			arrayed_list.remove_right
		end

	al_replace (v: like Current)
		do
			arrayed_list.replace (v)
		end
	
invariant
		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

		-- from DYNAMIC_TREE
	extendible_definition: Extendible
	child_after_definition: child_after = (child_index = arity + 1)

		-- from TREE
	tree_consistency: child_readable implies (attached child as c and then c.parent = Current)
	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 = child_capacity)
	child_after_definition: child_after = (child_index >= child_capacity + 1)

note
	ca_ignore: "CA033", "CA033: very large class"
	copyright: "Copyright (c) 1984-2018, Eiffel Software and others"
	license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
	source: "[
		Eiffel Software
		5949 Hollister Ave., Goleta, CA 93117 USA
		Telephone 805-685-1006, Fax 805-685-6869
		Website http://www.eiffel.com
		Customer support http://support.eiffel.com
	]"

end -- class ARRAYED_TREE

Generated by ISE EiffelStudio