实验内容过多,故参考做业:php
getRight
,contains
,toString
,preorder
,postorder
,这些分别是获得右孩子,是否包含,输出,前序遍历,后续遍历,关键代码以下:public LinkedBinaryTree1<T> getRight() { if(root == null) { throw new EmptyCollectionException("BinaryTree"); } LinkedBinaryTree1<T> result = new LinkedBinaryTree1<>(); result.root = root.getRight(); return result; public boolean contains(T targetElement) { BinaryTreeNode node = root; BinaryTreeNode temp = root; boolean result = false; if (node == null){ result = false; } if (node.getElement().equals(targetElement)){ result = true; } while (node.right != null){ if (node.right.getElement().equals(targetElement)){ result = true; break; } else { node = node.right; } } while (temp.left.getElement().equals(targetElement)){ if (temp.left.getElement().equals(targetElement)){ result = true; break; } else { temp = temp.left; } } return result; } public String toString() { UnorderedListADT<BinaryTreeNode<String>> nodes = new ArrayUnorderedList<BinaryTreeNode<String>>(); UnorderedListADT<Integer> levelList = new ArrayUnorderedList<Integer>(); BinaryTreeNode<String> current; String result = ""; int printDepth = this.getHeight(); int possibleNodes = (int)Math.pow(2, printDepth + 1); int countNodes = 0; nodes.addToRear((BinaryTreeNode<String>) root); Integer currentLevel = 0; Integer previousLevel = -1; levelList.addToRear(currentLevel); while (countNodes < possibleNodes) { countNodes = countNodes + 1; current = nodes.removeFirst(); currentLevel = levelList.removeFirst(); if (currentLevel > previousLevel) { result = result + "\n\n"; previousLevel = currentLevel; for (int j = 0; j < ((Math.pow(2, (printDepth - currentLevel))) - 1); j++) { result = result + " "; } } else { for (int i = 0; i < ((Math.pow(2, (printDepth - currentLevel + 1)) - 1)) ; i++) { result = result + " "; } } if (current != null) { result = result + (current.getElement()).toString(); nodes.addToRear(current.getLeft()); levelList.addToRear(currentLevel + 1); nodes.addToRear(current.getRight()); levelList.addToRear(currentLevel + 1); } else { nodes.addToRear(null); levelList.addToRear(currentLevel + 1); nodes.addToRear(null); levelList.addToRear(currentLevel + 1); result = result + " "; } } return result; } protected void preOrder(BinaryTreeNode<T> node, ArrayUnorderedList<T> tempList) { if (node != null){ tempList.addToRear(node.getElement()); preOrder(node.getLeft(),tempList); preOrder(node.getRight(),tempList); } } protected void postOrder(BinaryTreeNode<T> node, ArrayUnorderedList<T> tempList) { if (node != null){ postOrder(node.getLeft(),tempList); postOrder(node.getRight(),tempList); tempList.addToRear(node.getElement()); } } }
public BinaryTreeNode initTree(String[] preOrder, int start1, int end1, String[] inOrder, int start2, int end2) { if (start1 > end1 || start2 > end2) { return null; } String rootData = preOrder[start1]; BinaryTreeNode head = new BinaryTreeNode(rootData); //找到根节点所在位置 int rootIndex = findIndexInArray(inOrder, rootData, start2, end2); //构建左子树 BinaryTreeNode left = initTree(preOrder, start1 + 1, start1 + rootIndex - start2, inOrder, start2, rootIndex - 1); //构建右子树 BinaryTreeNode right = initTree(preOrder, start1 + rootIndex - start2 + 1, end1, inOrder, rootIndex + 1, end2); head.left = left; head.right = right; return head; } }
第三:对于书上的背部疼痛诊断器简单修改,无需放上。html
第四:本次实验惟一的难点,关键代码:java
public static String toSuffix(String infix) { String result = ""; String[] array = infix.split("\\s+"); Stack<LinkedBinaryTree> num = new Stack(); Stack<LinkedBinaryTree> op = new Stack(); for (int a = 0; a < array.length; a++) { if (array[a].equals("+") || array[a].equals("-") || array[a].equals("*") || array[a].equals("/")) { if (op.empty()) { op.push(new LinkedBinaryTree<>(array[a])); } else { if ((op.peek().root.element).equals("+") || (op.peek().root.element).equals("-") && array[a].equals("*") || array[a].equals("/")) { op.push(new LinkedBinaryTree(array[a])); } else { LinkedBinaryTree right = num.pop(); LinkedBinaryTree left = num.pop(); LinkedBinaryTree temp = new LinkedBinaryTree(op.pop().root.element, left, right); num.push(temp); op.push(new LinkedBinaryTree(array[a])); } } } else { num.push(new LinkedBinaryTree<>(array[a])); } } while (!op.empty()) { LinkedBinaryTree right = num.pop(); LinkedBinaryTree left = num.pop(); LinkedBinaryTree temp = new LinkedBinaryTree(op.pop().root.element, left, right); num.push(temp); } Iterator itr=num.pop().iteratorPostOrder(); while (itr.hasNext()){ result+=itr.next()+" "; } return result; }
static final class Entry<K,V> implements Map.Entry<K,V> { K key; // 键 V value; // 值 Entry<K,V> left = null; // 左孩子 Entry<K,V> right = null; // 右孩子 Entry<K,V> parent; // 双亲节点 boolean color = BLACK; // 当前节点颜色 // 构造函数 Entry(K key, V value, Entry<K,V> parent) { this.key = key; this.value = value; this.parent = parent; } }
TreeMap
的构造方法进行分析,TreeMap
一共四个构造方法。1.无参数构造方法node
public TreeMap() { comparator = null; }
2.带有比较器的构造方法web
public TreeMap(Comparator<? super K> comparator) { this.comparator = comparator; }
3.带Map的构造方法数据结构
public TreeMap(Map<? extends K, ? extends V> m) { comparator = null; putAll(m); }
4.带有SortedMap的构造方法app
public TreeMap(SortedMap<K, ? extends V> m) { comparator = m.comparator(); try { buildFromSorted(m.size(), m.entrySet().iterator(), null, null); } catch (java.io.IOException cannotHappen) { } catch (ClassNotFoundException cannotHappen) { } }
// 将map中的所有节点添加到TreeMap中 public void putAll(Map<? extends K, ? extends V> map) { // 获取map的大小 int mapSize = map.size(); // 若是TreeMap的大小是0,且map的大小不是0,且map是已排序的“key-value对” if (size==0 && mapSize!=0 && map instanceof SortedMap) { Comparator c = ((SortedMap)map).comparator(); // 若是TreeMap和map的比较器相等; // 则将map的元素所有拷贝到TreeMap中,而后返回! if (c == comparator || (c != null && c.equals(comparator))) { ++modCount; try { buildFromSorted(mapSize, map.entrySet().iterator(), null, null); } catch (java.io.IOException cannotHappen) { } catch (ClassNotFoundException cannotHappen) { } return; } } // 调用AbstractMap中的putAll(); // AbstractMap中的putAll()又会调用到TreeMap的put() super.putAll(map); }
显然,若是Map里的元素是排好序的,就调用buildFromSorted方法来拷贝Map中的元素,这在下一个构造方法中会重点说起,而若是Map中的元素不是排好序的,就调用AbstractMap的putAll(map)方法,该方法源码以下:函数
public void putAll(Map<? extends K, ? extends V> m) { for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) put(e.getKey(), e.getValue()); }
put
方法,一样的,利用备注的形式在代码中标出。public V put(K key, V value) { Entry<K,V> t = root; // 若红黑树为空,则插入根节点 if (t == null) { // throw NullPointerException // compare(key, key); // type check root = new Entry<K,V>(key, value, null); size = 1; modCount++; return null; } int cmp; Entry<K,V> parent; // split comparator and comparable paths Comparator<? super K> cpr = comparator; // 找出(key, value)在二叉排序树中的插入位置。 // 红黑树是以key来进行排序的,因此这里以key来进行查找。 if (cpr != null) { do { parent = t; cmp = cpr.compare(key, t.key); if (cmp < 0) t = t.left; else if (cmp > 0) t = t.right; else return t.setValue(value); } while (t != null); } else { 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); } // 为(key-value)新建节点 Entry<K,V> e = new Entry<K,V>(key, value, parent); if (cmp < 0) parent.left = e; else parent.right = e; // 插入新的节点后,调用fixAfterInsertion调整红黑树。 fixAfterInsertion(e); size++; modCount++; return null; }
TreeMap
的deleteEntry
方法,deleteEntry
方法一样也只需按照二叉排序树的操做步骤实现便可,删除指定节点后,再对树进行调整便可。deleteEntry
方法的实现源码以下:// 删除“红黑树的节点p” private void deleteEntry(Entry<K,V> p) { modCount++; size--; if (p.left != null && p.right != null) { Entry<K,V> s = successor (p); p.key = s.key; p.value = s.value; p = s; } Entry<K,V> replacement = (p.left != null ? p.left : p.right); if (replacement != null) { replacement.parent = p.parent; if (p.parent == null) root = replacement; else if (p == p.parent.left) p.parent.left = replacement; else p.parent.right = replacement; p.left = p.right = p.parent = null; if (p.color == BLACK) fixAfterDeletion(replacement); } else if (p.parent == null) { root = null; } else { if (p.color == BLACK) fixAfterDeletion(p); if (p.parent != null) { if (p == p.parent.left) p.parent.left = null; else if (p == p.parent.right) p.parent.right = null; p.parent = null; } } }
对于红黑树的源码分析到此就告一段落,由于最近时间有限,若是后期有空闲时间会继续对其源码进行分析。源码分析
1.
post
2.
3.
4.
5.
问题1解决方案:在寝室中跟王文彬同窗讨论相应问题的时候他提醒我说“虽然对于一棵表达式树来讲中序遍历获得的就是中缀表达式,后序遍历获得的就是后续表达式,但书上是利用后缀表达式构建了一棵树,而咱们的要求是利用中缀表达式构建一棵树。”这让我意识到了问题所在。好像问题没有那么简单,事实也证实如此,的确没有那么简单。
TreeMap
的源代码,看到那3013行代码时,脑袋都大了一圈。问题2解决方案:好在有于欣月同窗的提醒,网上有相似的分析,因此在网上搜了一下相应的问题,发现果真有相似的源码分析,便去参考了一番。