Java中AVL平衡二叉树实现Map 仿照TreeMap和TreeSet.docx
- 文档编号:30057387
- 上传时间:2023-08-04
- 格式:DOCX
- 页数:63
- 大小:29.69KB
Java中AVL平衡二叉树实现Map 仿照TreeMap和TreeSet.docx
《Java中AVL平衡二叉树实现Map 仿照TreeMap和TreeSet.docx》由会员分享,可在线阅读,更多相关《Java中AVL平衡二叉树实现Map 仿照TreeMap和TreeSet.docx(63页珍藏版)》请在冰豆网上搜索。
Java中AVL平衡二叉树实现Map仿照TreeMap和TreeSet
Java中AVL平衡二叉树实现Map(仿照TreeMap和TreeSet)
1、下面是AVLTreeMap的实现
packagecom;
importjava.io.IOException;
importjava.util.*;
publicclassAVLTreeMap
privatestaticfinallongserialVersionUID=1731396135957583906L;
privatefinalComparator
superK>comparator;
privatetransientEntry
privatetransientintsize=0;
privatetransientintmodCount=0;
publicAVLTreeMap(){
comparator=null;
}
publicAVLTreeMap(Comparator
superK>comparator){
parator=comparator;
}
publicAVLTreeMap(Map
extendsK,?
extendsV>m){
comparator=null;
putAll(m);
}
publicAVLTreeMap(SortedMap extendsV>m){ comparator=parator(); try{ buildFromSorted(m.size(),m.entrySet().iterator(),null,null); }catch(IOExceptione){ }catch(ClassNotFoundExceptione){ } } publicintsize(){ returnsize; } publicbooleancontainsKey(Objectkey){ returngetEntry(key)! =null; } publicbooleancontainsValue(Objectvalue){ for(Entry =null;e=successor(e)){ if(valEquals(value,e.value)) returntrue; } returnfalse; } publicVget(Objectkey){ Entry returnp==null? null: p.value; } publicComparator superK>comparator(){ returncomparator; } publicKfirstKey(){ returnkey(getFirstEntry()); } publicKlastKey(){ returnkey(getLastEntry()); } @SuppressWarnings({"rawtypes","unchecked"}) publicvoidputAll(Map extendsK,? extendsV>map){ intmapSize=map.size(); if(size==0&&mapSize! =0&&mapinstanceofSortedMap){ Comparable superK>cmp=(Comparable superK>)((SortedMap)map).comparator(); if(cmp==comparator||(cmp! =null&&cmp.equals(comparator))){ ++modCount; try{ buildFromSorted(mapSize,map.entrySet().iterator(),null,null); }catch(IOExceptione){ }catch(ClassNotFoundExceptione){ } return; } } super.putAll(map); } @SuppressWarnings("unchecked") finalEntry if(comparator! =null){ returngetEntryUsingComparator(key); } if(key==null) thrownewNullPointerException(); Comparable superK>k=(Comparable superK>)key; Entry while(p! =null){ intcmp=pareTo(p.key); if(cmp<0){ p=p.left; }elseif(cmp>0){ p=p.right; }else{ returnp; } } returnnull; } @SuppressWarnings("unchecked") finalEntry Kk=(K)key; Comparator superK>cpr=comparator; if(cpr! =null){ Entry while(p! =null){ intcmp=pare(k,p.key); if(cmp<0) p=p.left; elseif(cmp>0) p=p.right; else returnp; } } returnnull; } finalEntry Entry while(p! =null){ intcmp=compare(key,p.key); if(cmp<0){ if(p.left! =null) p=p.left; else returnp; }elseif(cmp<0){ if(p.right! =null) p=p.right; else{ Entry Entry while(parent! =null&&ch==parent.right){ ch=parent; parent=parent.parent; } returnparent; } }else{ returnp; } } returnnull; } finalEntry Entry while(p! =null){ intcmp=compare(key,p.key); if(cmp>0){ if(p.right! =null) p=p.right; else returnp; }elseif(cmp<0){ if(p.left! =null) p=p.left; else{ Entry Entry while(parent! =null&&ch==parent.left){ ch=parent; parent=parent.parent; } returnparent; } }else{ returnp; } } returnnull; } finalEntry Entry while(p! =null){ intcmp=compare(key,p.key); if(cmp<0){ if(p.left! =null) p=p.left; else returnp; }else{ if(p.right! =null) p=p.right; else{ Entry Entry while(parent! =null&&ch==parent.right){ ch=parent; parent=parent.parent; } returnparent; } } } returnnull; } finalEntry Entry while(p! =null){ intcmp=compare(key,p.key); if(cmp>0){ if(p.right! =null) p=p.right; else returnp; }else{ if(p.left! =null) p=p.left; else{ Entry Entry while(parent! =null&&ch==parent.left){ ch=parent; parent=parent.parent; } returnparent; } } } returnnull; } @SuppressWarnings("unchecked") publicVput(Kkey,Vvalue){ Entry if(t==null){ root=newEntry root.height=1; size++; modCount++; returnnull; } intcmp; Entry Comparator superK>cpr=comparator; if(cpr! =null){ do{ parent=t; cmp=pare(key,t.key); if(cmp<0) t=t.left; elseif(cmp>0) t=t.right; else returnt.setValue(value); }while(t! =null); }else{ do{ parent=t; cmp=((Comparable superK>)key).compareTo(t.key); if(cmp<0) t=t.left; elseif(cmp>0) t=t.right; else returnt.setValue(value); }while(t! =null); } Entry if(cmp<0) parent.left=e; else parent.right=e; fixAfterInsertion(e); size++; modCount++; returnnull; } publicVremove(Objectkey){ Entry if(p==null){ returnnull; } VoldVal=p.value; deleteEntry(p); returnoldVal; } publicvoidclear(){ size=0; modCount++; root=null; } @SuppressWarnings("unchecked") publicObjectclone(){ AVLTreeMap try{ clone=(AVLTreeMap }catch(CloneNotSupportedExceptione){ thrownewInternalError(); } clone.root=null; clone.size=0; clone.modCount=0; clone.entrySet=null; clone.keySet=null; clone.descendingMap=null; try{ clone.buildFromSorted(size,entrySet().iterator(),null,null); }catch(IOExceptione){ }catch(ClassNotFoundExceptione){ } returnclone; } publicMap.Entry returnexportEntry(getFirstEntry()); } publicMap.Entry returnexportEntry(getLastEntry()); } publicMap.Entry AVLTreeMap.Entry Map.Entry if(p! =null) deleteEntry(p); returnresult; } publicMap.Entry AVLTreeMap.Entry Map.Entry if(e! =null) deleteEntry(e); returnresult; } publicMap.Entry returnexportEntry(getLowerEntry(key)); } publicKlowerKey(Kkey){ returnkeyOrNull(getLowerEntry(key)); } publicMap.Entry returnexportEntry(getFloorEntry(key)); } publicKfloorKey(Kkey){ returnkeyOrNull(getFloorEntry(key)); } publicMap.Entry returnexportEntry(getCeilingEntry(key)); } publicKceilingKey(Kkey){ returnkeyOrNull(getCeilingEntry(key)); } publicMap.Entry returnexportEntry(getHigherEntry(key)); } publicKhigherKey(Kkey){ returnkeyOrNull(getHigherEntry(key)); } privatetransientEntrySetentrySet=null; privatetransientKeySet privatetransientNavigableMap @SuppressWarnings("unused") privatetransientSet privatetransientCollection publicSet returnnavigableKeySet(); } @SuppressWarnings({"unchecked","rawtypes"}) publicNavigableSet KeySet return(ks! =null)? ks: (navigableKeySet=newKeySet(this)); } publicNavigableSet returndescendingMap().navigableKeySet(); } publicSet EntrySetes=entrySet; return(es! =null)? es: (entrySet=newEntrySet()); } publicCollection Collection return(vs! =null)? vs: (values=newValues()); } @SuppressWarnings({"unchecked","rawtypes"}) publicNavigableMap NavigableMap return(km! =null)? km: (descendingMap=newDescendingSubMap(this,true,null,true,true,null,true)); } @SuppressWarnings({"unchecked","rawtypes"}) publicNavigableMap returnnewAscendingSubMap(this,false,fromKey,fromInclusive,false,toKey,toInclusive); } @SuppressWarnings({"unchecked","rawtypes"}) publicNavigableMap returnnewAscendingSubMap(this,true,null,true,false,toKey,inclusive); } @SuppressWarnings({"unchecked","rawtypes"}) publicNavigableMap returnnewAscendingSubMap(this,false,fr
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java中AVL平衡二叉树实现Map 仿照TreeMap和TreeSet Java AVL 平衡 二叉 实现 Map 仿照 TreeMap TreeSet
![提示](https://static.bdocx.com/images/bang_tan.gif)