用JUnit或本身編寫驅動類對本身實現的功能進行測試,提交測試代碼運行截圖,要全屏,包含本身的學號信息html
課下把代碼推送到代碼託管平臺java
//Rotate Left private void rotateLeft(Entry<K,V> p) { if (p != null) { Entry<K,V> r = p.right; p.right = r.left; if (r.left != null) r.left.parent = p; r.parent = p.parent; if (p.parent == null) root = r; else if (p.parent.left == p) p.parent.left = r; else p.parent.right = r; r.left = p; p.parent = r; } }
//Rotate Right private void rotateRight(Entry<K,V> p) { if (p != null) { Entry<K,V> l = p.left; p.left = l.right; if (l.right != null) l.right.parent = p; l.parent = p.parent; if (p.parent == null) root = l; else if (p.parent.right == p) p.parent.right = l; else p.parent.left = l; l.right = p; p.parent = l; } }
//getEntry()方法 final Entry<K,V> getEntry(Object key) { ...... if (key == null)//不容許key值爲null throw new NullPointerException(); Comparable<? super K> k = (Comparable<? super K>) key;//使用元素的天然順序 Entry<K,V> p = root; while (p != null) { int cmp = k.compareTo(p.key); if (cmp < 0)//向左找 p = p.left; else if (cmp > 0)//向右找 p = p.right; else return p; } return null; }
public V put(K key, V value) { ...... int cmp; Entry<K,V> parent; if (key == null) throw new NullPointerException(); Comparable<? super K> k = (Comparable<? super K>) key;//使用元素的天然順序 do { parent = t; cmp = k.compareTo(t.key); if (cmp < 0) t = t.left;//向左找 else if (cmp > 0) t = t.right;//向右找 else return t.setValue(value); } while (t != null); Entry<K,V> e = new Entry<>(key, value, parent);//建立並插入新的entry if (cmp < 0) parent.left = e; else parent.right = e; fixAfterInsertion(e);//調整 size++; return null; }