Visit node Traverse(left child) Traverse(right child)
Traverse(left child) Visit node Traverse(right child)
Traverse(left child) TRaverse(right child) Visit node
Creat a queue called nodes Create an unordered list calles results Enqueue the root onto the nodes queue while the nodes queue is not empty { Dequeue the first element from the queue If that element is not null Add that element to the rear of the results list Enqueue the children of the element on the nodes queue Else Add null on the result list } Return an iterator for the result list
操做 | 最差時間複雜度 |
---|---|
getRoot | 返回指向二叉樹根的引用 |
isEmpty | 斷定該樹是否爲空 |
size | 斷定樹中的元素數目 |
contains | 斷定指定目標是否在該樹中 |
find | 若是找到指定元素,則返回指向其的引用 |
toString | 返回樹的字符串表示 |
itertorInOrder | 爲樹的中序遍歷返回一個迭代器 |
itertorPreOrder | 爲樹的前序遍歷返回一個迭代器 |
iteratorPostOrder | 爲樹的後序遍歷返回一個迭代器 |
iteratorLevelOrder | 爲樹的層序遍歷返回一個迭代器 |
兩種特殊的二叉樹
html
BinaryNode(T data , BinaryNode<T> left , BinaryNode<T> right )
ThreeNode(T data ,ThreeNode<T> parent,ThreeNode<T> left,ThreeNode<T> right)
UnorderedListADT<BinaryTreeNode<ExpressionTreeOp>> nodes = new ArrayUnorderedList<BinaryTreeNode<ExpressionTreeOp>>(); UnorderedListADT<Integer> levelList = new ArrayUnorderedList<Integer>(); BinaryTreeNode<ExpressionTreeOp> current; String result = ""; int printDepth = this.getHeight(); int possibleNodes = (int)Math.pow(2, printDepth + 1); int countNodes = 0; nodes.addToRear(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; }
if ((operator == '+') || (operator == '-') || (operator == '*') || (operator == '/')) { operand1 = getOperand(treeStack); operand2 = getOperand(treeStack); treeStack.push(new ExpressionTree (new ExpressionTreeOp(1,operator,0), operand2, operand1)); } else { treeStack.push(new ExpressionTree(new ExpressionTreeOp (2,' ',Integer.parseInt(tempToken)), null, null)); }
原來,termType的用法是接收傳入的形參,判斷傳入的形參是否爲1以此來返回ture或者false來判斷是否爲操做符。java
這周沒有錯題哦~node
這周的內容相比前面幾周所學習的內容來講難了許多,從代碼的理解難易程度上就可以感受的出來,這周也花費了我更多的實踐,同時也給本身敲了一個警鐘,本身不會和不清楚的只是還有不少,不能再像以前那樣佛系學習,要更加的努力纔是,但願能在之後的學習生活中繼續進步!git
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
---|---|---|---|---|
第一週 | 200/200 | 1/1 | 5/20 | |
第二週 | 981/1181 | 1/2 | 15/20 | |
第三週 | 1694/2875 | 1/3 | 15/35 | |
第四周 | 3129/6004 | 1/4 | 15/50 | |
第五週 | 1294/7298 | 1/5 | 15/65 | |
第六週 | 1426/8724 | 1/6 | 20/85 |
計劃學習時間:20小時web
實際學習時間:20小時算法