Java中AVL平衡二叉树实现Map_第1页
Java中AVL平衡二叉树实现Map_第2页
Java中AVL平衡二叉树实现Map_第3页
Java中AVL平衡二叉树实现Map_第4页
Java中AVL平衡二叉树实现Map_第5页
已阅读5页,还剩39页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Java中AVL平衡二叉树实现Map (仿照TreeMap和TreeSet)1、下面是 AVLTreeMap 的实现package com;import java.io.IOException;import java.util.*;public class AVLTreeMap extends AbstractMap implements NavigableMap, java.io.Serializable private static final long serialVersionUI= 1731396135957583906L;private final Comparator compa

2、rator;private transient Entry root = null;private transient int size = 0;private transient int modCount = 0;public AVLTreeMap() comparator = null;public AVLTreeMap(Comparator comparator) parator = comparator;public AVLTreeMap(Map m) comparator = null;putAll(m);public AVLTreeMap(SortedMap m) comparat

3、or = parator();try buildFromSorted(m.size(), m.entrySet().iterator(), null, null); catch (IOException e) catch (ClassNotFoundException e) public int size() return size;public boolean containsKey(Object key) return getEntry(key) != null;public boolean containsValue(Object value) for (Entry e = getFir

4、stEntry(); e != null; e = successor(e) if (valEquals(value, e.value) return true;return false;public V get(Object key) Entry p = getEntry(key); return p = null ? null : p.value;public Comparator comparator() return comparator;public K firstKey() return key(getFirstEntry();public K lastKey() return k

5、ey(getLastEntry();SuppressWarnings( rawtypes, unchecked ) public void putAll(Map map) int mapSize = map.size();if (size = 0 & mapSize != 0 & map instanceof SortedMap) Comparable cmp = (Comparable) (SortedMap) map).comparator();if (cmp = comparator | (cmp != null & cmp.equals(comparator) +modCount;tr

6、y buildFromSorted(mapSize, map.entrySet().iterator(), null, null); catch (IOException e) catch (ClassNotFoundException e) return; super.putAll(map);SuppressWarnings(unchecked) final Entry getEntry(Object key) if (comparator != null) return getEntryUsingComparator(key);if (key = null)throw new NullPo

7、interException();Comparable k = (Comparable) key; Entry p = root;while (p != null) int cmp = pareTo(p.key);if (cmp 0) p = p.right; else return p; return null;SuppressWarnings(unchecked)final Entry getEntryUsingComparator(Object key) K k = (K) key;Comparator cpr = comparator;if (cpr != null) Entry p

8、= root;while (p != null) int cmp = pare(k, p.key);if (cmp 0)p = p.right;else return p;return null;final Entry getCeilingEntry(Object key) Entry p = root;while (p != null) int cmp = compare(key, p.key);if (cmp 0) if (p.left != null)p = p.left;elsereturn p; else if (cmp 0) if (p.right != null) p = p.r

9、ight;else Entry parent = p.parent;Entry ch = p;while (parent != null & ch = parent.right) ch = parent;parent = parent.parent;return parent; else return p;return null;final Entry getFloorEntry(Object key) Entry p = root;while (p != null) int cmp = compare(key, p.key);if (cmp 0) if (p.right != null) p

10、 = p.right;elsereturn p; else if (cmp 0) if (p.left != null)p = p.left;else Entry parent = p.parent;Entry ch = p;while (parent != null & ch = parent.left) ch = parent;parent = parent.parent;return parent; else return p;return null;final Entry getHigherEntry(Object key) Entry p = root;while (p != nul

11、l) int cmp = compare(key, p.key);if (cmp 0) if (p.left != null)p = p.left;elsereturn p; else if (p.right != null)p = p.right;else Entry parent = p.parent;Entry ch = p;while (parent != null & ch = parent.right) ch = parent;parent = parent.parent;return parent;return null;final Entry getLowerEntry(Obj

12、ect key) Entry p = root;while (p != null) int cmp = compare(key, p.key);if (cmp 0) if (p.right != null)p = p.right;else return p; else if (p.left != null)p = p.left;else Entry parent = p.parent;Entry ch = p; while (parent != null & ch = parent.left) ch = parent;parent = parent.parent; return parent;

13、 return null;SuppressWarnings(unchecked) public V put(K key, V value) Entry t = root;if (t = null) root = new Entry(key, value, null); root.height = 1;size+; modCount+; return null;int cmp;Entry parent;Comparator cpr = comparator;if (cpr != null) do parent = t; cmp = pare(key, t.key); if (cmp 0)t =

14、t.right;elsereturn t.setValue(value); while (t != null); else do parent = t;cmp = (Comparable) key).compareTo(t.key); if (cmp 0)t = t.right;elsereturn t.setValue(value); while (t != null);Entry e = new Entry(key, value, parent); if (cmp 0)parent.left = e;else parent.right = e;fixAfterInsertion(e); s

15、ize+;modCount+; return null;public V remove(Object key) Entry p = getEntry(key);if (p = null) return null;V oldVal = p.value; deleteEntry(p); return oldVal;public void clear() size = 0; modCount+;root = null;SuppressWarnings(unchecked) public Object clone() AVLTreeMap clone = null;try clone = (AVLTr

16、eeMap) super.clone(); catch (CloneNotSupportedException e) throw new InternalError();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 (IOException e)

17、 catch (ClassNotFoundException e) return clone;public Map.Entry firstEntry() return exportEntry(getFirstEntry();public Map.Entry lastEntry() return exportEntry(getLastEntry();public Map.Entry pollFirstEntry() AVLTreeMap.Entry p = getFirstEntry(); Map.Entry result = exportEntry(p); if (p != null)dele

18、teEntry(p);return result;public Map.Entry pollLastEntry() AVLTreeMap.Entry e = getLastEntry(); Map.Entry result = exportEntry(e); if (e != null)deleteEntry(e);return result;public Map.Entry lowerEntry(K key) return exportEntry(getLowerEntry(key);public K lowerKey(K key) return keyOrNull(getLowerEntr

19、y(key);public Map.Entry floorEntry(K key) return exportEntry(getFloorEntry(key);public K floorKey(K key) return keyOrNull(getFloorEntry(key);public Map.Entry ceilingEntry(K key) return exportEntry(getCeilingEntry(key);public K ceilingKey(K key) return keyOrNull(getCeilingEntry(key);public Map.Entry

20、higherEntry(K key) return exportEntry(getHigherEntry(key);public K higherKey(K key) return keyOrNull(getHigherEntry(key);private transient EntrySet entrySet = null;private transient KeySet navigableKeySet = null;private transient NavigableMap descendingMap = null; SuppressWarnings(unused)private tra

21、nsient Set keySet = null; private transient Collection values = null;public Set keySet() return navigableKeySet();SuppressWarnings( unchecked, rawtypes ) public NavigableSet navigableKeySet() KeySet ks = navigableKeySet;return (ks != null) ? ks : (navigableKeySet = new KeySet(this);public NavigableS

22、et descendingKeySet() return descendingMap().navigableKeySet();public SetMap.Entry entrySet() EntrySet es = entrySet;return (es != null) ? es : (entrySet = new EntrySet();public Collection values() Collection vs = values;return (vs != null) ? vs : (values = new Values();SuppressWarnings( unchecked,

23、rawtypes ) public NavigableMap descendingMap() NavigableMap km = descendingMap;return (km != null) ? km : (descendingMap = new DescendingSubMap(this, true, null, true, true, null, true);SuppressWarnings( unchecked, rawtypes )public NavigableMap subMap(K fromKey, boolean fromInclusive, K toKey, boole

24、an toInclusive) return new AscendingSubMap(this, false, fromKey, fromInclusive, false, toKey, toInclusive);SuppressWarnings( unchecked, rawtypes )public NavigableMap headMap(K toKey, boolean inclusive) return new AscendingSubMap(this, true, null, true, false, toKey, inclusive);SuppressWarnings( unch

25、ecked, rawtypes )public NavigableMap tailMap(K fromKey, boolean inclusive) return new AscendingSubMap(this, false, fromKey, inclusive, true, null, true);public SortedMap subMap(K fromKey, K toKey) return subMap(fromKey, true, toKey, false);public SortedMap headMap(K toKey) return headMap(toKey, fals

26、e);public SortedMap tailMap(K fromKey) return tailMap(fromKey, true);class Values extends AbstractCollection public Iterator iterator() return new ValueIterator(getFirstEntry();public int size() return AVLTreeMap.this.size();public boolean contains(Object o) return AVLTreeMap.this.containsValue(o);p

27、ublic boolean remove(Object o) for (Entry e = getFirstEntry(); e != null; e = successor(e) if (valEquals(o, e.value) deleteEntry(e); return true;return false;public void clear() AVLTreeMap.this.clear();class EntrySet extends AbstractSetMap.Entry public IteratorMap.Entry iterator() return new EntryIt

28、erator(getFirstEntry();public int size() return AVLTreeMap.this.size();SuppressWarnings(unchecked) public boolean contains(Object o) if (!(o instanceof Map.Entry) return false;Map.Entry entry = (Map.Entry) o;V value = entry.getValue();Entry p = getEntry(entry.getKey();return p != null & valEquals(p.

29、getValue(), value);SuppressWarnings(unchecked) public boolean remove(Object o) if (!(o instanceof Map.Entry) return false;Map.Entry entry = (Map.Entry) o;V value = entry.getValue();Entry p = getEntry(entry.getKey();if (p != null & valEquals(p.getValue(), value) deleteEntry(p); return true;return fal

30、se;public void clear() AVLTreeMap.this.clear();Iterator keyIterator() return new KeyIterator(getFirstEntry();Iterator descendingKeyIterator() return new DescendingKeyIterator(getLastEntry();static final class KeySet extends AbstractSet implements NavigableSet private final NavigableMap m;KeySet(Navi

31、gableMap m) this.m = m;SuppressWarnings( unchecked, rawtypes ) public Iterator iterator() if (m instanceof AVLTreeMap)return (AVLTreeMap) m).keyIterator();elsereturn (Iterator) (AVLTreeMap.NavigableSubMap) m).keyIterator(); SuppressWarnings( unchecked, rawtypes ) public Iterator descendingIterator()

32、 if (m instanceof AVLTreeMap) return (AVLTreeMap) m).descendingKeyIterator(); elsereturn (Iterator) (AVLTreeMap.NavigableSubMap) m).descendingKeyIterator();public int size() return m.size();public boolean isEmpty() return m.isEmpty();public boolean contains(Object o) return m.containsKey(o);public v

33、oid clear() m.clear();public E lower(E e) return m.lowerKey(e);public E floor(E e) return m.floorKey(e);public E ceiling(E e) return m.ceilingKey(e);public E higher(E e) return m.higherKey(e);public E first() return m.firstKey();public E last() return m.lastKey();public Comparator comparator() retur

34、n parator();public E pollFirst() Map.Entry e = m.pollFirstEntry(); return e = null ? null : e.getKey();public E pollLast() Map.Entry e = m.pollLastEntry();return e = null ? null : e.getKey();public boolean remove(Object o) int oldSize = m.size(); m.remove(o); return oldSize != m.size();public Naviga

35、bleSet descendingSet() return new AVLTreeSet(m.descendingMap();public NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) return new AVLTreeSet(m.subMap(fromElement, fromInclusive, toElement,toInclusive);public NavigableSet headSet(E toElement, boolean inclusi

36、ve) return new AVLTreeSet(m.headMap(toElement, inclusive);public NavigableSet tailSet(E fromElement, boolean inclusive) return new AVLTreeSet(m.tailMap(fromElement, inclusive);public SortedSet subSet(E fromElement, E toElement) return subSet(fromElement, true, toElement, false);public SortedSet head

37、Set(E toElement) return headSet(toElement, false);public SortedSet tailSet(E fromElement) return tailSet(fromElement, true);abstract class PrivateEntryIterator implements Iterator Entry next;Entry lastReturned;int expectedModCount;PrivateEntryIterator(Entry first) expectedModCount = modCount; lastRe

38、turned = null; next = first;public final boolean hasNext() return next != null;final Entry nextEntry() Entry e = next; if (next = null)throw new NoSuchElementException();if (modCount != expectedModCount)throw new ConcurrentModificationException(); next = successor(e);lastReturned = e; return e;final

39、 Entry prevEntry() Entry e = next;if (next = null)throw new NoSuchElementException();if (modCount != expectedModCount)throw new ConcurrentModificationException();next = predecessor(e); lastReturned = e; return e;public void remove() if (lastReturned = null) throw new IllegalStateException();if (modC

40、ount != expectedModCount)throw new ConcurrentModificationException();if (leftOf(lastReturned) != null & rightOf(lastReturned) != null) next = lastReturned;deleteEntry(lastReturned); expectedModCount = modCount; lastReturned = null;final class EntryIterator extends PrivateEntryIteratorMap.Entry Entry

41、Iterator(Entry first) super(first);public Map.Entry next() return nextEntry();final class ValueIterator extends PrivateEntryIterator ValueIterator(Entry first) super(first);public V next() return nextEntry().value;final class KeyIterator extends PrivateEntryIterator KeyIterator(Entry first) super(fi

42、rst);public K next() return nextEntry().key;final class DescendingKeyIterator extends PrivateEntryIterator DescendingKeyIterator(Entry first) super(first);public K next() return prevEntry().key;SuppressWarnings(unchecked)final int compare(Object k1, Object k2) return comparator = null ? (Comparable)

43、 k1).compareTo(K) k2) : pare(K) k1, (K) k2);final static boolean valEquals(Object o1, Object o2) return o1 = null ? o2 = null : o1.equals(o2);SuppressWarnings( unchecked, rawtypes )static Map.Entry exportEntry(Entry e) return e = null ? null : new AbstractMap.SimpleImmutableEntry(e);static K keyOrNu

44、ll(Entry e) return e = null ? null : e.key;static K key(Entry e) if (e = null)throw new NoSuchElementException();return e.key;final int max(int height1, int height2) return (height1 height2) ? height1 : height2;static abstract class NavigableSubMap extends AbstractMap implements NavigableMap, java.i

45、o.Serializable private static final long serialVersionUI= 3330238317193227055L; final AVLTreeMapm;final K lo, hi;final boolean fromStart, toEnd; final boolean loInclusive, hiInclusive;NavigableSubMap(AVLTreeMap m, boolean fromStart, K lo, boolean loInclusive, boolean toEnd, K hi, boolean hiInclusive

46、) if (!fromStart & !toEnd) if (pare(lo, hi) 0)throw new IllegalArgumentException(fromKey toKey); else if (!fromStart)pare(lo, lo);pare(hi, hi);this.m = m;this.lo = lo;this.hi = hi; this.fromStart = fromStart; this.toEnd = toEnd; this.loInclusive = loInclusive; this.hiInclusive = hiInclusive;final bo

47、olean tooLow(Object key) if (!fromStart) int c = pare(key, lo);if (c 0 | (c = 0 & !hiInclusive) return true; return false;final boolean inRange(Object key) return !tooLow(key) & !tooHigh(key);final boolean inClosedRange(Object key) return (fromStart | pare(key, lo) = 0) & (toEnd | pare(key, hi) = 0)

48、;final boolean inRange(Object key, boolean inclusive) return inclusive ? inRange(key) : inClosedRange(key);final AVLTreeMap.Entry absLowest() AVLTreeMap.Entry e = fromStart ? m.getFirstEntry() : (loInclusive ? m.getCeilingEntry(lo) : m.getHigherEntry(lo);return (e = null | tooHigh(e.key) ? null : e;

49、final AVLTreeMap.Entry absHighest() AVLTreeMap.Entry e = toEnd ? m.getLastEntry() : (hiInclusive ? m.getFloorEntry(hi) : m.getLowerEntry(hi);return (e = null | tooLow(e.key) ? null : e);final AVLTreeMap.Entry absCeiling(K key) if (tooLow(key)return absLowest();AVLTreeMap.Entry e = m.getCeilingEntry(

50、key); return (e = null | tooHigh(e.key) ? null : e;final AVLTreeMap.Entry absHigher(K key) if (tooLow(key)return absLowest();AVLTreeMap.Entry e = m.getHigherEntry(key); return (e = null | tooHigh(e.key) ? null : e;final AVLTreeMap.Entry absFloor(K key) if (tooHigh(key)return absHighest();AVLTreeMap.

51、Entry e = m.getFloorEntry(key);return (e = null | tooLow(e.key) ? null : e;final AVLTreeMap.Entry absLower(K key) if (tooHigh(key)return absHighest();AVLTreeMap.Entry e = m.getLowerEntry(key);return (e = null | tooLow(e.key) ? null : e;final AVLTreeMap.Entry absHighFence() return toEnd ? null : (hiI

52、nclusive ? m.getHigherEntry(hi) : m.getCeilingEntry(hi); final AVLTreeMap.Entry absLowFence() return fromStart ? null : (loInclusive ? m.getLowerEntry(lo) : m.getCeilingEntry(lo);abstract AVLTreeMap.Entry subLowest();abstract AVLTreeMap.Entry subHighest();abstract AVLTreeMap.Entry subCeiling(K key);

53、abstract AVLTreeMap.Entry subHigher(K key);abstract AVLTreeMap.Entry subFloor(K key);abstract AVLTreeMap.Entry subLower(K key);abstract Iterator keyIterator();abstract Iterator descendingKeyIterator();public boolean isEmpty() return (fromStart & toEnd) ? m.isEmpty() : entrySet().isEmpty(); public in

54、t size() return (fromStart & toEnd) ? m.size() : entrySet().size(); public final boolean containsKey(Object key) return inRange(key) & m.containsKey(key); public final V put(K key, V value) if (!inRange(key)throw new IllegalArgumentException(key out of range); return m.put(key, value);public final V

55、 get(Object key) return !inRange(key) ? null : m.get(key); public final V remove(Object key) return !inRange(key) ? null : m.remove(key); public Map.Entry ceilingEntry(K key) return exportEntry(subCeiling(key); public K ceilingKey(K key) return keyOrNull(subCeiling(key); public Map.Entry higherEntry

56、(K key) return exportEntry(subHigher(key); public K higherKey(K key) return keyOrNull(subHigher(key);public Map.Entry floorEntry(K key) return exportEntry(subFloor(key);public K floorKey(K key) return keyOrNull(subFloor(key);public Map.Entry lowerEntry(K key) return exportEntry(subLower(key);public

57、K lowerKey(K key) return keyOrNull(subLower(key);public K firstKey() return key(subLowest();public K lastKey() return key(subHighest();public Map.Entry firstEntry() return exportEntry(subLowest();public Map.Entry lastEntry() return exportEntry(subHighest();public Map.Entry pollFirstEntry() AVLTreeMa

58、p.Entry e = subLowest(); Map.Entry result = exportEntry(e); if (e != null)m.deleteEntry(e);return result;public Map.Entry pollLastEntry() AVLTreeMap.Entry e = subHighest();Map.Entry result = exportEntr(ye); if (e != null) m.deleteEntry(e);return result;transient NavigableMap descendingMapView = null

59、; transient EntrySetView entrySetView = null;transient KeySet navigableKeySetView = null;SuppressWarnings( unchecked, rawtypes ) public final NavigableSet navigableKeySet() KeySet nksv = navigableKeySetView; return nksv != null ? nksv : (navigableKeySetView = new AVLTreeMap.KeySet(this);public final

60、 Set keySet() return navigableKeySet();public NavigableSet descendingKeySet() return descendingMap().navigableKeySet();public SortedMap subMap(K fromKey, K toKey) return subMap(fromKey, true, toKey, false);public SortedMap headMap(K toKey) return headMap(toKey, false);public SortedMap tailMap(K from

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论