概述
遞歸(recursion) 又稱遞回,在數學與計算機科學中,是指在函數的定義中使用函數自身的方法。
一般來說,遞歸需要有邊界條件、遞歸前進段和遞歸返回段。當邊界條件不滿足時,遞歸前進;當邊界條件滿足時,遞歸返回。
構成遞歸需具備的條件:
1. 子問題須與原始問題為同樣的事,且更為簡單;
2. 不能無限制地調用本身,須有個出口,化簡為非遞歸狀況處理。
代碼取自美國課本 "Java How to Program "(Deitel & Detel)的練習: 20.25。 以中序遍歷遞歸方法為例,這里顯示的圖解,僅詮釋開始一小部分遞歸前進段與遞歸返回段的交叉過程。通過這一小段的繁瑣解釋,希望讀者可見到二叉樹遞歸遍歷的端倪。
private void inorderHelper( TreeNode node ){ if ( node == null ) //若節點為空 return; //無任何操作 inorderHelper( node.leftNode ); //有序遍歷下一級左子樹 System.out.print( node.data + " " ); //輸出節點數據 inorderHelper( node.rightNode );//有序遍歷下一級右子樹 }
插圖說明:
前進段的進程 1 :鑒於以樹根 節點 "49" 為參數,調用 inorderHelper(...),開始調用以下一級樹根 節點 "28" 為參數 的inorderHelper(...) 方法。
前進段的進程 2 :鑒於以樹根 節點 "28" 為參數,調用 inorderHelper(...),開始調用以下一級樹根 節點 "18" 為參數 的 inorderHelper(...) 方法。
前進段的進程 3 :鑒於以樹根 節點 "18" 為參數,調用 inorderHelper(...),開始調用以下一級樹根 節點 "11" 為參數 的 inorderHelper(...) 方法。
節點 "11" 為葉節點,遞歸前進到終點。開始啟動返回操作, 輸出其數值 11。
至此,參數為 節點 "11" 的 方法 inorderHelper(...) 執行完畢。返回進程 4 啟動下一個 輸出:18。
輸出 18 的代碼行執行完畢,進入前進段進程 5, 執行接下來一行的代碼:調用參數為 節點 "19" 的節點的方法 inorderHelper(...)
節點 "19" 為葉節點,遞歸前進到終點。開始啟動返回操作, 輸出其數值 19。
至此,參數為 節點 "19" 的 方法 inorderHelper(...) 執行完畢。返回進程 6 啟動下一個 輸出:28。
實現方法:
package test; import java.util.LinkedList; import java.util.List; //把一個數組的值存入二叉樹中,然后進行3種方式的遍歷 public class BinaryTree { private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; private static List<Node> nodeList = null; //內部類結點 private static class Node { Node leftChild; Node rightChild; int data; //構造方法初始化 Node(int newData) { leftChild = null; rightChild = null; data = newData; } } public void createBinTree() { nodeList = new LinkedList<Node>(); // 將一個數組的值依次轉換為Node節點 for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) { nodeList.add(new Node(array[nodeIndex])); } // 對前lastParentIndex-1個父節點按照父節點與孩子節點的數學關系建立二叉樹 for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) { // 左孩子 nodeList.get(parentIndex).leftChild = nodeList.get(parentIndex * 2 + 1); // 右孩子 nodeList.get(parentIndex).rightChild = nodeList.get(parentIndex * 2 + 2); } // 最后一個父節點:因為最后一個父節點可能沒有右孩子,所以單獨拿出來處理 int lastParentIndex = array.length / 2 - 1; // 左孩子 nodeList.get(lastParentIndex).leftChild = nodeList.get(lastParentIndex * 2 + 1); // 右孩子,如果數組的長度為奇數才建立右孩子 if (array.length % 2 == 1) { nodeList.get(lastParentIndex).rightChild = nodeList.get(lastParentIndex * 2 + 2); } } /** * 先序遍歷 * * 這三種不同的遍歷結構都是一樣的,只是先后順序不一樣而已 * * @param node * 遍歷的節點 */ public static void preOrderTraverse(Node node) { if (node == null) return; System.out.print(node.data + " "); preOrderTraverse(node.leftChild); preOrderTraverse(node.rightChild); } /** * 中序遍歷 * * 這三種不同的遍歷結構都是一樣的,只是先后順序不一樣而已 * * @param node * 遍歷的節點 */ public static void inOrderTraverse(Node node) { if (node == null) return; inOrderTraverse(node.leftChild); System.out.print(node.data + " "); inOrderTraverse(node.rightChild); } /** * 后序遍歷 * * 這三種不同的遍歷結構都是一樣的,只是先后順序不一樣而已 * * @param node * 遍歷的節點 */ public static void postOrderTraverse(Node node) { if (node == null) return; postOrderTraverse(node.leftChild); postOrderTraverse(node.rightChild); System.out.print(node.data + " "); } public static void main(String[] args) { BinaryTree binTree = new BinaryTree(); binTree.createBinTree(); // nodeList中第0個索引處的值即為根節點 Node root = nodeList.get(0); System.out.println("先序遍歷:"); preOrderTraverse(root); System.out.println(); System.out.println("中序遍歷:"); inOrderTraverse(root); System.out.println(); System.out.println("后序遍歷:"); postOrderTraverse(root); } }
輸出結果:
先序遍歷:
2 4 8 9 5 3 6 7
中序遍歷:
4 9 2 5 1 6 3 7
后序遍歷:
9 4 5 2 6 7 3 1