(* This program is free software; you can redistribute it and/or      *)
(* modify it under the terms of the GNU Lesser General Public License *)
(* as published by the Free Software Foundation; either version 2.1   *)
(* of the License, or (at your option) any later version.             *)
(*                                                                    *)
(* This program is distributed in the hope that it will be useful,    *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of     *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *)
(* GNU General Public License for more details.                       *)
(*                                                                    *)
(* You should have received a copy of the GNU Lesser General Public   *)
(* License along with this program; if not, write to the Free         *)
(* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *)
(* 02110-1301 USA                                                     *)
(* Contribution to the Coq Library   V6.3 (July 1999)                    *)
(*     A development written by Pierre Castéran for Coq V6.1  Coq : A product of inria : "http://pauillac.inria.fr/coq/coq-eng.html"  Pierre Castéran : A product of LaBRI :   LaBRI, Universite Bordeaux-I      |    12 place Puy Paulin 351 Cours de la Liberation        |    33000 Bordeaux F-33405 TALENCE Cedex             |    France France                            |    (+ 33) 5 56 81 15 80 tel : (+ 33) 5 56 84 69 31 fax : (+ 33) 5 56 84 66 69 email: casteran@labri.u-bordeaux.fr www: http://dept-info.labri.u-bordeaux.fr/~casteran  "Les rêves sont aussi beaux que la réalité, mais ils ne sont pas mieux". ( J.L.Borges )  *)
(* VI    Deletion. ****************** *******************   Deleting an element from a binary search tree is a little more complex than inserting or searching. The difficult case is the deletion of the root of a tree; we have to reconstruct a search tree. To solve this problem, we define an auxiliary operation: deleting the greatest element of a non-empty binary search tree. *)
(* VI.2  Deletion in general ****************************  We are now ready to study the remove operation in it's generality: (RM n t t') if t' is a search tree obtained by removing n from t *)
Inductive RM (n : nat) (t t' : nat_tree) : Prop :=
rm_intro :
~ occ t' n ->
(forall p : nat, occ t' p -> occ t p) ->
(forall p : nat, occ t p -> occ t' p \/ n = p) ->
search t' -> RM n t t'.
Hint Resolve rm_intro: searchtrees.
(* base cases *)
Remark RM_0 : forall n : nat, RM n NIL NIL.
(*********************************)
Hint Resolve rm_2: searchtrees.
Remark rm_3 : ~ occ (bin q t0 t') n.
(**********************************)
Hint Resolve rm_3: searchtrees.
Remark rm_4 :
forall p0 : nat,
occ (bin q t0 t') p0 -> occ (bin n (bin p t1 t2) t') p0.
(***************************************************************)
Hint Resolve rm_4: searchtrees.
Remark rm_5 :
forall p0 : nat,
occ (bin n (bin p t1 t2) t') p0 -> occ (bin q t0 t') p0 \/ n = p0.
(********************************************)
Hint Resolve rm_5: searchtrees.
Remark rm_6 : search (bin q t0 t').
(**********************************)
Hint Resolve rm_6: searchtrees.
(********************************************************************)
End rm_root.
(* The final algorithm *)

Theorem rm :
forall (n : nat) (t : nat_tree), search t -> {t' : nat_tree | RM n t t'}.