Automatic generation produced by ISE Eiffel

Classes Clusters Cluster hierarchy Chart Relations Text Flat Contracts Flat contracts Go to:
indexing description: "[ Binary search trees; left child item is less than current item, right child item is greater ]" status: "See notice at end of class" names: binary_search_tree, tree representation: recursive, array access: cursor, membership contents: generic date: "$Date: 2001-11-16 20:32:23 +0000 (Fri, 16 Nov 2001) $" revision: "$Revision: 51435 $" class BINARY_SEARCH_TREE [G -> COMPARABLE] create make feature -- Initialization make (v: like item) is -- Create single node with item v. require item_exists: v /= void do bt_make (v) ensure node_item: item = v no_child: (left_child = void) and (right_child = void) end bt_make (v: like item) is -- Create a root node with value v. -- (from BINARY_TREE) do item := v ensure -- from BINARY_TREE is_root: is_root is_leaf: is_leaf end feature -- Access child: like parent is -- Child at cursor position -- (from BINARY_TREE) require -- from TREE readable: readable_child do inspect child_index when 1 then Result := left_child when 2 then Result := right_child end end child_cursor: CURSOR is -- Current cursor position -- (from BINARY_TREE) do create {ARRAYED_LIST_CURSOR} Result.make (child_index) end child_index: INTEGER -- Index of cursor position -- (from BINARY_TREE) child_item: like item is -- Item in current child node -- (from TREE) require -- from TREE readable: child_readable do Result := child.item end first_child: like parent is -- Left child -- (from BINARY_TREE) require -- from TREE is_not_leaf: not is_leaf do Result := left_child end has (v: like item): BOOLEAN is -- Does tree contain a node whose item -- is equal to v (object comparison)? require -- from CONTAINER True require else argument_not_void: v /= void do if items_equal (item, v) then Result := True elseif v < item then if left_child /= void then set_comparison_mode (left_child); Result := left_child.has (v) end else if right_child /= void then set_comparison_mode (right_child) Result := right_child.has (v) end end ensure -- from CONTAINER not_found_in_empty: Result implies not is_empty end item: G -- Content of cell. -- (from CELL) last_child: like parent is -- Right child -- (from BINARY_TREE) require -- from TREE is_not_leaf: not is_leaf do Result := right_child end left_child: like parent -- Left child, if any -- (from BINARY_TREE) left_item: like item is -- Value of left child -- (from BINARY_TREE) require -- from BINARY_TREE has_left: left_child /= void do Result := left_child.item end left_sibling: like parent is -- Left neighbor, if any -- (from BINARY_TREE) require -- from TREE is_not_root: not is_root do if parent.right_child = Current then Result := parent.left_child end ensure -- from TREE is_sibling: Result /= void implies is_sibling (Result) right_is_current: (Result /= void) implies (Result.right_sibling = Current) end parent: BINARY_SEARCH_TREE [G] -- Parent of current node right_child: like parent -- Right child, if any -- (from BINARY_TREE) right_item: like item is -- Value of right child -- (from BINARY_TREE) require -- from BINARY_TREE has_right: right_child /= void do Result := right_child.item end right_sibling: like parent is -- Right neighbor, if any -- (from BINARY_TREE) require -- from TREE is_not_root: not is_root do if parent.left_child = Current then Result := parent.right_child 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 arity: INTEGER is -- Number of children -- (from BINARY_TREE) do if has_left then Result := Result + 1 end if has_right then Result := Result + 1 end ensure then -- from BINARY_TREE valid_arity: Result <= 2 end count: INTEGER is -- Number of items -- (from TREE) do Result := subtree_count + 1 end max: like item is -- Maximum item in tree do if has_right then Result := right_child.max else Result := item end ensure maximum_present: has (Result) end min: like item is -- Minimum item in tree do if has_left then Result := left_child.min else Result := item end ensure minimum_present: has (Result) end feature -- Comparison is_equal (other: like Current): BOOLEAN is -- 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.is_equal (Current) consistent: standard_is_equal (other) implies Result end feature -- Status report changeable_comparison_criterion: BOOLEAN is -- May object_comparison be changed? -- (Answer: yes by default.) -- (from CONTAINER) do Result := True end child_after: BOOLEAN is -- Is there no valid child position to the right of cursor? -- (from BINARY_TREE) do Result := child_index >= child_capacity + 1 end child_before: BOOLEAN is -- Is there no valid child position to the left of cursor? -- (from TREE) do Result := child_index = 0 end child_isfirst: BOOLEAN is -- 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 -- 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_off: BOOLEAN is -- Is there no current child? -- (from TREE) do Result := child_before or child_after end child_readable: BOOLEAN is -- 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 -- Is there a current child_item that may be modified? -- (from TREE) do Result := not child_off and then (child /= void) end empty: BOOLEAN is obsolete "ELKS 2000: Use `is_empty' instead" -- Is there no element? -- (from CONTAINER) do Result := is_empty end has_both: BOOLEAN is -- Has current node two children? -- (from BINARY_TREE) do Result := left_child /= void and right_child /= void ensure -- from BINARY_TREE Result = (has_left and has_right) end has_left: BOOLEAN is -- Has current node a left child? -- (from BINARY_TREE) do Result := left_child /= void ensure -- from BINARY_TREE Result = (left_child /= void) end has_none: BOOLEAN is -- Are there no children? -- Was declared in BINARY_TREE as synonym of is_leaf. -- (from BINARY_TREE) do Result := left_child = void and right_child = void end has_right: BOOLEAN is -- Has current node a right child? -- (from BINARY_TREE) do Result := right_child /= void ensure -- from BINARY_TREE Result = (right_child /= void) end is_empty: BOOLEAN is -- Is structure empty of items? -- (from TREE) do Result := False end is_leaf: BOOLEAN is -- Are there no children? -- Was declared in BINARY_TREE as synonym of has_none. -- (from BINARY_TREE) do Result := left_child = void and right_child = void end is_root: BOOLEAN is -- Is there no parent? -- (from TREE) do Result := parent = void end is_sibling (other: like parent): BOOLEAN is -- 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 is True -- (from TREE) readable_child: BOOLEAN is -- Is there a current child to be read? -- (from TREE) do Result := not child_off end sorted: BOOLEAN is -- Is tree sorted? do Result := True if (has_left and then left_item > item) or (has_right and then right_item < item) then Result := False else if has_left then Result := left_child.sorted_and_less (item) end if has_right and Result then Result := right_child.sorted end end end sorted_and_less (i: like item): BOOLEAN is -- Is tree sorted and all its elements less then i do Result := True if (has_left and then left_item > item) or (has_right and then right_item < item) then Result := False else if has_left then Result := left_child.sorted_and_less (item) end if has_right and Result then Result := right_child.sorted_and_less (i) end end end valid_cursor_index (i: INTEGER): BOOLEAN is -- 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 is True -- Is there a current item that may be modified? -- (from TREE) writable_child: BOOLEAN is -- Is there a current child that may be modified? -- (from TREE) do Result := not child_off end feature -- Status setting compare_objects is -- Ensure that future search operations will use equal -- rather than = for comparing references. -- (from CONTAINER) require -- from CONTAINER changeable_comparison_criterion do object_comparison := True ensure -- from CONTAINER object_comparison end compare_references is -- Ensure that future search operations will use = -- rather than equal for comparing references. -- (from CONTAINER) require -- from CONTAINER changeable_comparison_criterion do object_comparison := False ensure -- from CONTAINER reference_comparison: not object_comparison end feature -- Cursor movement child_back is -- Move cursor to previous child. -- (from BINARY_TREE) do child_index := child_index - 1 end child_finish is -- Move cursor to last child. -- (from BINARY_TREE) do child_index := 2 ensure then -- from TREE is_last_child: not is_leaf implies child_islast end child_forth is -- Move cursor to next child. -- (from BINARY_TREE) do child_index := child_index + 1 end child_go_i_th (i: INTEGER) is -- Move cursor to i-th child. -- (from BINARY_TREE) require else -- from TREE valid_cursor_index: valid_cursor_index (i) do child_index := i ensure then -- from TREE position: child_index = i end child_go_to (p: ARRAYED_LIST_CURSOR) is -- Move cursor to child remembered by p. -- (from BINARY_TREE) do child_index := p.index end child_start is -- Move to first child. -- (from BINARY_TREE) do child_index := 1 ensure then -- from TREE is_first_child: not is_leaf implies child_isfirst end i_infix is -- Apply node_action to every node's item -- in tree, using infix order. do if left_child /= void then left_child.i_infix end node_action (item) if right_child /= void then right_child.i_infix end end node_action (v: like item) is -- Operation on node item, -- to be defined by descendant classes. -- Here it is defined as an empty operation. -- Redefine this procedure in descendant classes if useful -- operations are to be performed during traversals. do end postorder is -- Apply node_action to every node's item -- in tree, using post-order. do if left_child /= void then left_child.postorder end if right_child /= void then right_child.postorder end node_action (item) end preorder is -- Apply node_action to every node's item -- in tree, using pre-order. do node_action (item) if left_child /= void then left_child.preorder end if right_child /= void then right_child.preorder end end feature -- Element change child_put (v: like item) is -- Put v at current child position. -- Was declared in BINARY_TREE as synonym of child_replace. -- (from BINARY_TREE) require -- from TREE child_writable: child_writable local node: like Current do if child /= void then if object_comparison then child.compare_objects else child.compare_references end child.bt_put (v) else create node.bt_make (v) if object_comparison then node.compare_objects end put_child (node) end ensure -- from TREE item_inserted: child_item = v end child_replace (v: like item) is -- Put v at current child position. -- Was declared in BINARY_TREE as synonym of child_put. -- (from BINARY_TREE) require -- from TREE child_writable: child_writable local node: like Current do if child /= void then if object_comparison then child.compare_objects else child.compare_references end child.bt_put (v) else create node.bt_make (v) if object_comparison then node.compare_objects end put_child (node) end ensure -- from TREE item_inserted: child_item = v end extend (v: like item) is -- Put v at proper position in tree -- (unless v exists already). -- (Reference or object equality, -- based on object_comparison.) -- Was declared in BINARY_SEARCH_TREE as synonym of put. require new_item_exists: v /= void do if not items_equal (v, item) then if v < item then if left_child = void then put_left_child (new_tree) left_child.replace (v) else left_child.put (v) end else if right_child = void then put_right_child (new_tree) right_child.replace (v) else right_child.put (v) end end end ensure item_inserted: has (v) end fill (other: TREE [G]) is -- 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 put (v: like item) is -- Put v at proper position in tree -- (unless v exists already). -- (Reference or object equality, -- based on object_comparison.) -- Was declared in BINARY_SEARCH_TREE as synonym of extend. require new_item_exists: v /= void do if not items_equal (v, item) then if v < item then if left_child = void then put_left_child (new_tree) left_child.replace (v) else left_child.put (v) end else if right_child = void then put_right_child (new_tree) right_child.replace (v) else right_child.put (v) end end end ensure item_inserted: has (v) end bt_put (v: like item) is -- 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 parent) is -- Put n at current child position. -- Was declared in BINARY_TREE as synonym of replace_child. -- (from BINARY_TREE) do if object_comparison then n.compare_objects else n.compare_references end n.attach_to_parent (Current) inspect child_index when 1 then left_child := n when 2 then right_child := n end end replace (v: like item) is -- 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 parent) is -- Put n at current child position. -- Was declared in BINARY_TREE as synonym of put_child. -- (from BINARY_TREE) require -- from TREE writable_child: writable_child was_root: n.is_root do if object_comparison then n.compare_objects else n.compare_references end n.attach_to_parent (Current) inspect child_index when 1 then left_child := n when 2 then right_child := n end ensure -- from TREE child_replaced: child = n end sprout is -- Make current node a root. -- (from TREE) do if parent /= void then parent.prune (Current) end end feature {BINARY_SEARCH_TREE} -- Element change put_left_child (n: like parent) is -- Set left_child to n. -- (from BINARY_TREE) require -- from BINARY_TREE no_parent: n = void or else n.is_root do if n /= void then if object_comparison then n.compare_objects else n.compare_references end end if left_child /= void then left_child.attach_to_parent (void) end if n /= void then n.attach_to_parent (Current) end left_child := n end put_right_child (n: like parent) is -- Set right_child to n. -- (from BINARY_TREE) require -- from BINARY_TREE no_parent: n = void or else n.is_root do if n /= void then if object_comparison then n.compare_objects else n.compare_references end end if right_child /= void then right_child.attach_to_parent (void) end if n /= void then n.attach_to_parent (Current) end right_child := n end feature {BINARY_SEARCH_TREE} -- Removal remove_left_child is -- Remove left child. -- (from BINARY_TREE) do if left_child /= void then left_child.attach_to_parent (void) end left_child := void ensure -- from BINARY_TREE not has_left end remove_right_child is -- Remove right child. -- (from BINARY_TREE) do if right_child /= void then right_child.attach_to_parent (void) end right_child := void ensure -- from BINARY_TREE not has_right end feature -- Removal child_remove is -- Remove current child. -- (from BINARY_TREE) do inspect child_index when 1 then left_child.attach_to_parent (void); left_child := void when 2 then right_child.attach_to_parent (void); right_child := void end end prune (n: like parent) is -- Prune n from child nodes. -- (from BINARY_TREE) require -- from TREE is_child: n.parent = Current do if left_child = n then remove_left_child elseif right_child = n then remove_right_child end ensure -- from TREE n_is_root: n.is_root end feature -- Transformation sort is -- Sort tree. local seq: LINEAR [G] temp: ARRAY [G] heap: HEAP_PRIORITY_QUEUE [G] i: INTEGER do seq := linear_representation i := count remove_left_child remove_right_child from seq.start create heap.make (i) until seq.off loop heap.put (seq.item) seq.forth end from create temp.make (1, heap.count) i := 1 until heap.is_empty loop temp.put (heap.item, i) heap.remove i := i + 1 end replace (temp.item ((temp.count) // 2 + 1)) fill_from_sorted_special (temp.area, 0, temp.count - 1) ensure is_sorted: sorted end feature -- Conversion binary_representation: BINARY_TREE [G] is -- Convert to binary tree representation: -- first child becomes left child, -- right sibling becomes right child. -- (from TREE) local current_sibling: BINARY_TREE [G] do create Result.make (item) if not is_leaf then Result.put_left_child (first_child.binary_representation) from child_start child_forth current_sibling := Result.left_child until child_after loop current_sibling.put_right_child (child.binary_representation) current_sibling := current_sibling.right_child child_forth end end ensure -- from TREE result_is_root: Result.is_root result_has_no_right_child: not Result.has_right end linear_representation: LINEAR [G] is -- 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 copy (other: like Current) is -- Copy contents from other. -- (from BINARY_TREE) require -- from ANY other_not_void: other /= void type_identity: same_type (other) local tmp_tree: like Current do create tmp_tree.bt_make (other.item) if not other.is_leaf then tree_copy (other, tmp_tree) end ensure -- from ANY is_equal: is_equal (other) end duplicate (n: INTEGER): like Current is -- Copy of sub-tree beginning at cursor position and -- having min (n, arity - child_index + 1) -- children. -- (from BINARY_TREE) require -- from TREE not_child_off: not child_off valid_sublist: n >= 0 do Result := new_tree if child_index <= 1 and child_index + n >= 1 and has_left then Result.put_left_child (left_child.duplicate_all) end if child_index <= 2 and child_index + n >= 2 and has_right then Result.put_right_child (right_child.duplicate_all) end end duplicate_all: like Current is -- (from BINARY_TREE) do Result := new_tree if has_left then Result.put_left_child (left_child.duplicate_all) end if has_right then Result.put_right_child (right_child.duplicate_all) end end feature {BINARY_SEARCH_TREE, BINARY_SEARCH_TREE_SET} -- Implementation intersect (other: BINARY_SEARCH_TREE [G]) is -- Remove all items not in other. do if right_child /= void then right_child.intersect (other) end if left_child /= void then left_child.intersect (other) end if not other.has (item) then remove_node end end is_subset (other: like Current): BOOLEAN is -- Is Current a subset of other do Result := other.has (item) if Result and left_child /= void then Result := left_child.is_subset (other) end if Result and right_child /= void then Result := right_child.is_subset (other) end end max_node: like Current is -- Node containing max do if has_right then Result := right_child.min_node else Result := Current end end merge (other: like Current) is -- Add all items of other. do if other.right_child /= void then merge (other.right_child) end if other.left_child /= void then merge (other.left_child) end extend (other.item) end min_node: like Current is -- Node containing min do if has_left then Result := left_child.min_node else Result := Current end end pruned (v: like item; par: like Current): like Current is -- Prune v. -- (par is the parent node of the current node, needed to update -- parent correctly.) local m: like Current do if items_equal (item, v) then if has_none then elseif not has_right then left_child.attach_to_parent (par); Result := left_child elseif not has_left then right_child.attach_to_parent (par); Result := right_child else m := right_child.min_node m.remove_node item := m.item Result := Current end else Result := Current if v < item then if left_child /= void then left_child := left_child.pruned (v, Current) end else if right_child /= void then right_child := right_child.pruned (v, Current) end end end end remove_node is -- Remove current node from the tree. require is_not_root: not is_root local is_left_child: BOOLEAN m: like Current do is_left_child := Current = parent.left_child if not has_right then if left_child /= void then left_child.attach_to_parent (void) end if is_left_child then parent.put_left_child (left_child) else parent.put_right_child (left_child) end parent := void elseif not has_left then if right_child /= void then right_child.attach_to_parent (void) end; if is_left_child then parent.put_left_child (right_child) else parent.put_right_child (right_child) end; parent := void else m := right_child.min_node m.remove_node item := m.item end end subtract (other: BINARY_SEARCH_TREE [G]) is -- Remove all items also in other. require set_exists: other /= void do if right_child /= void then right_child.subtract (other) end if left_child /= void then left_child.subtract (other) end if other.has (item) then remove_node end end feature {NONE} -- Implementation Child_capacity: INTEGER is 2 -- (from BINARY_TREE) fill_from_sorted_special (t: SPECIAL [G]; s, e: INTEGER) is -- Put values from t into tree in such an order that -- the tree will be balanced if t is sorted. local m: INTEGER do m := (s + e) // 2 put (t.item (m)) if m - 1 >= s then fill_from_sorted_special (t, s, m - 1) end if m + 1 <= e then fill_from_sorted_special (t, m + 1, e) end end fill_subtree (other: BINARY_TREE [G]) is -- Copy other to subtee. -- (from BINARY_TREE) do if not other.is_leaf then put_left_child (other.left_child.duplicate_all) end if other.arity >= 2 then put_right_child (other.right_child.duplicate_all) end end items_equal (src, dest: like item): BOOLEAN is -- Are src and dest equal? -- (depending on object_comparison) do if object_comparison then Result := src /= void and then src.is_equal (dest) else Result := (src = dest) end end new_tree: like Current is -- New tree node -- (from BINARY_TREE) do create Result.bt_make (item) if object_comparison then Result.compare_objects end end remove is -- Remove current item -- (from TREE) do end set_comparison_mode (t: like Current) is -- Set comparison mode of t to the same mode as Current. require not_void: t /= void do if object_comparison then t.compare_objects else t.compare_references end ensure mode_set: object_comparison = t.object_comparison end subtree_count: INTEGER is -- Number of items in subtree -- (from BINARY_TREE) do if left_child /= void then Result := left_child.count end if right_child /= void then Result := Result + right_child.count end end subtree_has (v: G): BOOLEAN is -- Does subtree contain v? -- (from BINARY_TREE) do if left_child /= void then Result := left_child.has (v) end if right_child /= void and not Result then Result := right_child.has (v) end end tree_copy (other, tmp_tree: like Current) is -- 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 p1, p2: like Current other_stack, tmp_stack: LINKED_STACK [like Current] idx_stack, orgidx_stack: LINKED_STACK [INTEGER] 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.child = void end p2.replace_child (clone (p1.child)) if other_stack.is_empty then p2.child.attach_to_parent (Current) end check comparison_mode_ok: p2.child.object_comparison = p1.child.object_comparison p1_consistent: p1.child.parent = p1 p2_consistent: p2.child.parent = p2 end if not p1.child.is_leaf then other_stack.put (p1) tmp_stack.put (p2) idx_stack.put (i + 1) p1 := p1.child p2 := p2.child orgidx_stack.put (p1.child_index) i := 0 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) 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 check tree_stacks_empty: other_stack.is_empty and tmp_stack.is_empty at_root: p1 = other and p2 = tmp_tree end standard_copy (tmp_tree) child_go_i_th (orgidx_stack.item) orgidx_stack.remove check index_stack_empty: orgidx_stack.is_empty end end tree_is_equal (t1, t2: like Current): BOOLEAN is -- 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 i: INTEGER p1, p2: like Current t1_stack, t2_stack: LINKED_STACK [like Current] idx_stack, orgidx1_stack, orgidx2_stack: LINKED_STACK [INTEGER] do if t1.is_leaf and t2.is_leaf then if t1.object_comparison then Result := equal (t1.item, t2.item) else Result := (t1.item = t2.item) end elseif t1.is_leaf xor t2.is_leaf then Result := False else create t1_stack.make create t2_stack.make create idx_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 i := 1 p1 := t1 p2 := t2 invariant same_count: t1_stack.count = t2_stack.count and t2_stack.count = idx_stack.count until not Result or else (i > p1.child_capacity and t1_stack.is_empty) loop check p1_not_void: p1 /= void p2_not_void: p2 /= void end p1.child_go_i_th (i) p2.child_go_i_th (i) if p1.child_readable and p2.child_readable and p1.child_capacity = p2.child_capacity then check p1_consistent: p1.child.parent = p1 p2_consistent: p2.child.parent = p2 end if t1.object_comparison then Result := equal (p1.item, p2.item) else Result := (p1.item = p2.item) end if not (p1.child.is_leaf or p2.child.is_leaf) then t1_stack.put (p1) t2_stack.put (p2) idx_stack.put (i + 1) p1 := p1.child p2 := p2.child orgidx1_stack.put (p1.child_index) orgidx2_stack.put (p2.child_index) i := 0 elseif p1.child.is_leaf xor p2.child.is_leaf then Result := False end elseif p1.child_capacity /= p2.child_capacity or else (p1.child_readable xor p2.child_readable) then Result := False end if i <= p1.child_capacity then i := i + 1 else from invariant same_count: t1_stack.count = t2_stack.count and t2_stack.count = idx_stack.count until t1_stack.is_empty or else i <= p1.child_capacity 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 i := idx_stack.item t1_stack.remove t2_stack.remove idx_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 end feature {BINARY_TREE} -- Implementation fill_list (al: ARRAYED_LIST [G]) is -- Fill al with all the children's items. -- (from BINARY_TREE) do if left_child /= void then al.extend (left_child.item) left_child.fill_list (al) end if right_child /= void then al.extend (right_child.item) right_child.fill_list (al) end end feature {TREE} -- Implementation attach_to_parent (n: like parent) is -- Make n parent of current node. -- (from TREE) do parent := n ensure -- from TREE new_parent: parent = n end invariant -- from ANY reflexive_equality: standard_is_equal (Current) reflexive_conformance: conforms_to (Current) -- from BINARY_TREE tree_is_binary: child_capacity = 2 -- 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 = child_capacity) child_after_definition: child_after = (child_index >= child_capacity + 1) child_consistency: child_readable implies child.parent = Current indexing library: "[ EiffelBase: Library of reusable components for Eiffel. ]" status: "[ Copyright 1986-2001 Interactive Software Engineering (ISE). For ISE customers the original versions are an ISE product covered by the ISE Eiffel license and support agreements. ]" license: "[ EiffelBase may now be used by anyone as FREE SOFTWARE to develop any product, public-domain or commercial, without payment to ISE, under the terms of the ISE Free Eiffel Library License (IFELL) at http://eiffel.com/products/base/license.html. ]" source: "[ Interactive Software Engineering Inc. ISE Building 360 Storke Road, Goleta, CA 93117 USA Telephone 805-685-1006, Fax 805-685-6869 Electronic mail <info@eiffel.com> Customer support http://support.eiffel.com ]" info: "[ For latest info see award-winning pages: http://eiffel.com ]" end -- class BINARY_SEARCH_TREE
Classes Clusters Cluster hierarchy Chart Relations Text Flat Contracts Flat contracts Go to:

-- Generated by ISE Eiffel --
For more details: www.eiffel.com