123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151# 1 "Camomile/internal/avlTree.ml"(** AVL tree *)(* Copyright (C) 2003, 2010 Yamagata Yoriyuki. *)(* This library 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 of *)(* the License, or (at your option) any later version. *)(* As a special exception to the GNU Library General Public License, you *)(* may link, statically or dynamically, a "work that uses this library" *)(* with a publicly distributed version of this library to produce an *)(* executable file containing portions of this library, and distribute *)(* that executable file under terms of your choice, without any of the *)(* additional requirements listed in clause 6 of the GNU Library General *)(* Public License. By "a publicly distributed version of this library", *)(* we mean either the unmodified Library as distributed by the authors, *)(* or a modified version of this library that is distributed under the *)(* conditions defined in clause 3 of the GNU Library General Public *)(* License. This exception does not however invalidate any other reasons *)(* why the executable file might be covered by the GNU Library General *)(* Public License . *)(* This library 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 *)(* Lesser General Public License for more details. *)(* You should have received a copy of the GNU Lesser General Public *)(* License along with this library; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *)(* USA *)(* You can contact the authour by sending email to *)(* yoriyuki.y@gmail.com *)type'atree=Empty|Nodeof'atree*'a*'atree*intletempty=Emptyletis_empty=functionEmpty->true|_->falseletsingleton_treex=Node(Empty,x,Empty,1)letleft_branch=functionEmpty->raiseNot_found|Node(l,_,_,_)->lletright_branch=functionEmpty->raiseNot_found|Node(_,_,r,_)->rletroot=functionEmpty->raiseNot_found|Node(_,v,_,_)->vletheight=functionEmpty->0|Node(_,_,_,h)->hletcreatelvr=leth'=1+max(heightl)(heightr)inassert(abs(heightl-heightr)<2);Node(l,v,r,h')(* Assume |hl - hr| < 3 *)letballvr=lethl=heightlinlethr=heightrinifhl>=hr+2thenmatchlwithEmpty->assertfalse|Node(ll,lv,lr,_)->ifheightll>=heightlrthencreatelllv(createlrvr)elsematchlrwithEmpty->assertfalse|Node(lrl,lrv,lrr,_)->create(createlllvlrl)lrv(createlrrvr)elseifhr>=hl+2thenmatchrwithEmpty->assertfalse|Node(rl,rv,rr,_)->ifheightrr>=heightrlthencreate(createlvrl)rvrrelsematchrlwithEmpty->assertfalse|Node(rll,rlv,rlr,_)->create(createlvrll)rlv(createrlrrvrr)elsecreatelvrletrecadd_leftv=functionEmpty->Node(Empty,v,Empty,1)|Node(l,v',r,_)->bal(add_leftvl)v'rletrecadd_rightv=functionEmpty->Node(Empty,v,Empty,1)|Node(l,v',r,_)->ballv'(add_rightvr)(* No assumption of height of l and r. *)letrecmake_treelvr=matchl,rwithEmpty,_->add_leftvr|_,Empty->add_rightvl|Node(ll,lv,lr,lh),Node(rl,rv,rr,rh)->iflh>rh+1thenballllv(make_treelrvr)elseifrh>lh+1thenbal(make_treelvrl)rvrrelsecreatelvr(* Utilities *)letrecsplit_leftmost=functionEmpty->raiseNot_found|Node(Empty,v,r,_)->(v,r)|Node(l,v,r,_)->letv0,l'=split_leftmostlin(v0,make_treel'vr)letrecsplit_rightmost=functionEmpty->raiseNot_found|Node(l,v,Empty,_)->(v,l)|Node(l,v,r,_)->letv0,r'=split_rightmostrin(v0,make_treelvr')letrecconcatt1t2=matcht1,t2withEmpty,_->t2|_,Empty->t1|Node(l1,v1,r1,h1),Node(l2,v2,r2,h2)->ifh1<h2thenmake_tree(concatt1l2)v2r2elsemake_treel1v1(concatr1t2)letreciterproc=functionEmpty->()|Node(l,v,r,_)->iterprocl;procv;iterprocrletrecfoldftinit=matchtwithEmpty->init|Node(l,v,r,_)->letx=foldflinitinletx=fvxinfoldfrx