Java中AVL平衡二叉树实现Map 仿照TreeMap和TreeSet.docx

上传人:b****8 文档编号:30057387 上传时间:2023-08-04 格式:DOCX 页数:63 大小:29.69KB
下载 相关 举报
Java中AVL平衡二叉树实现Map 仿照TreeMap和TreeSet.docx_第1页
第1页 / 共63页
Java中AVL平衡二叉树实现Map 仿照TreeMap和TreeSet.docx_第2页
第2页 / 共63页
Java中AVL平衡二叉树实现Map 仿照TreeMap和TreeSet.docx_第3页
第3页 / 共63页
Java中AVL平衡二叉树实现Map 仿照TreeMap和TreeSet.docx_第4页
第4页 / 共63页
Java中AVL平衡二叉树实现Map 仿照TreeMap和TreeSet.docx_第5页
第5页 / 共63页
点击查看更多>>
下载资源
资源描述

Java中AVL平衡二叉树实现Map 仿照TreeMap和TreeSet.docx

《Java中AVL平衡二叉树实现Map 仿照TreeMap和TreeSet.docx》由会员分享,可在线阅读,更多相关《Java中AVL平衡二叉树实现Map 仿照TreeMap和TreeSet.docx(63页珍藏版)》请在冰豆网上搜索。

Java中AVL平衡二叉树实现Map 仿照TreeMap和TreeSet.docx

Java中AVL平衡二叉树实现Map仿照TreeMap和TreeSet

Java中AVL平衡二叉树实现Map(仿照TreeMap和TreeSet)

1、下面是AVLTreeMap的实现

packagecom;

importjava.io.IOException;

importjava.util.*;

publicclassAVLTreeMapextendsAbstractMapimplementsNavigableMap,java.io.Serializable{

privatestaticfinallongserialVersionUID=1731396135957583906L;

privatefinalComparator

superK>comparator;

privatetransientEntryroot=null;

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(Entrye=getFirstEntry();e!

=null;e=successor(e)){

if(valEquals(value,e.value))

returntrue;

}

returnfalse;

}

publicVget(Objectkey){

Entryp=getEntry(key);

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")

finalEntrygetEntry(Objectkey){

if(comparator!

=null){

returngetEntryUsingComparator(key);

}

if(key==null)

thrownewNullPointerException();

Comparable

superK>k=(Comparable

superK>)key;

Entryp=root;

while(p!

=null){

intcmp=pareTo(p.key);

if(cmp<0){

p=p.left;

}elseif(cmp>0){

p=p.right;

}else{

returnp;

}

}

returnnull;

}

@SuppressWarnings("unchecked")

finalEntrygetEntryUsingComparator(Objectkey){

Kk=(K)key;

Comparator

superK>cpr=comparator;

if(cpr!

=null){

Entryp=root;

while(p!

=null){

intcmp=pare(k,p.key);

if(cmp<0)

p=p.left;

elseif(cmp>0)

p=p.right;

else

returnp;

}

}

returnnull;

}

finalEntrygetCeilingEntry(Objectkey){

Entryp=root;

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{

Entryparent=p.parent;

Entrych=p;

while(parent!

=null&&ch==parent.right){

ch=parent;

parent=parent.parent;

}

returnparent;

}

}else{

returnp;

}

}

returnnull;

}

finalEntrygetFloorEntry(Objectkey){

Entryp=root;

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{

Entryparent=p.parent;

Entrych=p;

while(parent!

=null&&ch==parent.left){

ch=parent;

parent=parent.parent;

}

returnparent;

}

}else{

returnp;

}

}

returnnull;

}

finalEntrygetHigherEntry(Objectkey){

Entryp=root;

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{

Entryparent=p.parent;

Entrych=p;

while(parent!

=null&&ch==parent.right){

ch=parent;

parent=parent.parent;

}

returnparent;

}

}

}

returnnull;

}

finalEntrygetLowerEntry(Objectkey){

Entryp=root;

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{

Entryparent=p.parent;

Entrych=p;

while(parent!

=null&&ch==parent.left){

ch=parent;

parent=parent.parent;

}

returnparent;

}

}

}

returnnull;

}

@SuppressWarnings("unchecked")

publicVput(Kkey,Vvalue){

Entryt=root;

if(t==null){

root=newEntry(key,value,null);

root.height=1;

size++;

modCount++;

returnnull;

}

intcmp;

Entryparent;

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);

}

Entrye=newEntry(key,value,parent);

if(cmp<0)

parent.left=e;

else

parent.right=e;

fixAfterInsertion(e);

size++;

modCount++;

returnnull;

}

publicVremove(Objectkey){

Entryp=getEntry(key);

if(p==null){

returnnull;

}

VoldVal=p.value;

deleteEntry(p);

returnoldVal;

}

publicvoidclear(){

size=0;

modCount++;

root=null;

}

@SuppressWarnings("unchecked")

publicObjectclone(){

AVLTreeMapclone=null;

try{

clone=(AVLTreeMap)super.clone();

}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.EntryfirstEntry(){

returnexportEntry(getFirstEntry());

}

publicMap.EntrylastEntry(){

returnexportEntry(getLastEntry());

}

publicMap.EntrypollFirstEntry(){

AVLTreeMap.Entryp=getFirstEntry();

Map.Entryresult=exportEntry(p);

if(p!

=null)

deleteEntry(p);

returnresult;

}

publicMap.EntrypollLastEntry(){

AVLTreeMap.Entrye=getLastEntry();

Map.Entryresult=exportEntry(e);

if(e!

=null)

deleteEntry(e);

returnresult;

}

publicMap.EntrylowerEntry(Kkey){

returnexportEntry(getLowerEntry(key));

}

publicKlowerKey(Kkey){

returnkeyOrNull(getLowerEntry(key));

}

publicMap.EntryfloorEntry(Kkey){

returnexportEntry(getFloorEntry(key));

}

publicKfloorKey(Kkey){

returnkeyOrNull(getFloorEntry(key));

}

publicMap.EntryceilingEntry(Kkey){

returnexportEntry(getCeilingEntry(key));

}

publicKceilingKey(Kkey){

returnkeyOrNull(getCeilingEntry(key));

}

publicMap.EntryhigherEntry(Kkey){

returnexportEntry(getHigherEntry(key));

}

publicKhigherKey(Kkey){

returnkeyOrNull(getHigherEntry(key));

}

privatetransientEntrySetentrySet=null;

privatetransientKeySetnavigableKeySet=null;

privatetransientNavigableMapdescendingMap=null;

@SuppressWarnings("unused")

privatetransientSetkeySet=null;

privatetransientCollectionvalues=null;

publicSetkeySet(){

returnnavigableKeySet();

}

@SuppressWarnings({"unchecked","rawtypes"})

publicNavigableSetnavigableKeySet(){

KeySetks=navigableKeySet;

return(ks!

=null)?

ks:

(navigableKeySet=newKeySet(this));

}

publicNavigableSetdescendingKeySet(){

returndescendingMap().navigableKeySet();

}

publicSet>entrySet(){

EntrySetes=entrySet;

return(es!

=null)?

es:

(entrySet=newEntrySet());

}

publicCollectionvalues(){

Collectionvs=values;

return(vs!

=null)?

vs:

(values=newValues());

}

@SuppressWarnings({"unchecked","rawtypes"})

publicNavigableMapdescendingMap(){

NavigableMapkm=descendingMap;

return(km!

=null)?

km:

(descendingMap=newDescendingSubMap(this,true,null,true,true,null,true));

}

@SuppressWarnings({"unchecked","rawtypes"})

publicNavigableMapsubMap(KfromKey,booleanfromInclusive,KtoKey,booleantoInclusive){

returnnewAscendingSubMap(this,false,fromKey,fromInclusive,false,toKey,toInclusive);

}

@SuppressWarnings({"unchecked","rawtypes"})

publicNavigableMapheadMap(KtoKey,booleaninclusive){

returnnewAscendingSubMap(this,true,null,true,false,toKey,inclusive);

}

@SuppressWarnings({"unchecked","rawtypes"})

publicNavigableMaptailMap(KfromKey,booleaninclusive){

returnnewAscendingSubMap(this,false,fr

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 其它模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1