如下是我要解析的一個二叉樹的模型形狀node
接下來廢話很少直接上代碼this
一種是用遞歸的方法,另外一種是用堆棧的方法:spa
首先建立一棵樹:code
public class Node { private int data; private Node leftNode; private Node rightNode; public Node(int data, Node leftNode, Node rightNode){ this.data = data; this.leftNode = leftNode; this.rightNode = rightNode; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Node getLeftNode() { return leftNode; } public void setLeftNode(Node leftNode) { this.leftNode = leftNode; } public Node getRightNode() { return rightNode; } public void setRightNode(Node rightNode) { this.rightNode = rightNode; } }
遞歸:blog
public class BinaryTree { /** * @author yaobo * 二叉樹的先序中序後序排序 */ public Node init() {//注意必須逆序創建,先創建子節點,再逆序往上創建,由於非葉子結點會使用到下面的節點,而初始化是按順序初始化的,不逆序創建會報錯 Node J = new Node(8, null, null); Node H = new Node(4, null, null); Node G = new Node(2, null, null); Node F = new Node(7, null, J); Node E = new Node(5, H, null); Node D = new Node(1, null, G); Node C = new Node(9, F, null); Node B = new Node(3, D, E); Node A = new Node(6, B, C); return A; //返回根節點 } public void printNode(Node node){ System.out.print(node.getData()); } public void theFirstTraversal(Node root) { //先序遍歷 printNode(root); if (root.getLeftNode() != null) { //使用遞歸進行遍歷左孩子 theFirstTraversal(root.getLeftNode()); } if (root.getRightNode() != null) { //遞歸遍歷右孩子 theFirstTraversal(root.getRightNode()); } } public void theInOrderTraversal(Node root) { //中序遍歷 if (root.getLeftNode() != null) { theInOrderTraversal(root.getLeftNode()); } printNode(root); if (root.getRightNode() != null) { theInOrderTraversal(root.getRightNode()); } } public void thePostOrderTraversal(Node root) { //後序遍歷 if (root.getLeftNode() != null) { thePostOrderTraversal(root.getLeftNode()); } if(root.getRightNode() != null) { thePostOrderTraversal(root.getRightNode()); } printNode(root); } public static void main(String[] args) { BinaryTree tree = new BinaryTree(); Node root = tree.init(); System.out.println("先序遍歷"); tree.theFirstTraversal(root); System.out.println(""); System.out.println("中序遍歷"); tree.theInOrderTraversal(root); System.out.println(""); System.out.println("後序遍歷"); tree.thePostOrderTraversal(root); System.out.println(""); } }
堆棧:排序
public class BinaryTree1 { public Node init() {//注意必須逆序創建,先創建子節點,再逆序往上創建,由於非葉子結點會使用到下面的節點,而初始化是按順序初始化的,不逆序創建會報錯 Node J = new Node(8, null, null); Node H = new Node(4, null, null); Node G = new Node(2, null, null); Node F = new Node(7, null, J); Node E = new Node(5, H, null); Node D = new Node(1, null, G); Node C = new Node(9, F, null); Node B = new Node(3, D, E); Node A = new Node(6, B, C); return A; //返回根節點 } public void printNode(Node node){ System.out.print(node.getData()); } public void theFirstTraversal_Stack(Node root) { //先序遍歷 Stack<Node> stack = new Stack<Node>(); Node node = root; while (node != null || stack.size() > 0) { //將全部左孩子壓棧 if (node != null) { //壓棧以前先訪問 printNode(node); stack.push(node); node = node.getLeftNode(); } else { node = stack.pop(); node = node.getRightNode(); } } } public void theInOrderTraversal_Stack(Node root) { //中序遍歷 Stack<Node> stack = new Stack<Node>(); Node node = root; while (node != null || stack.size() > 0) { if (node != null) { stack.push(node); //直接壓棧 node = node.getLeftNode(); } else { node = stack.pop(); //出棧並訪問 printNode(node); node = node.getRightNode(); } } } public void thePostOrderTraversal_Stack(Node root) { //後序遍歷 Stack<Node> stack = new Stack<Node>(); Stack<Node> output = new Stack<Node>();//構造一箇中間棧來存儲逆後序遍歷的結果 Node node = root; while (node != null || stack.size() > 0) { if (node != null) { output.push(node); stack.push(node); node = node.getRightNode(); } else { node = stack.pop(); node = node.getLeftNode(); } } System.out.println(output.size()); while (output.size() > 0) { printNode(output.pop()); } } public static void main(String[] args) { BinaryTree1 tree = new BinaryTree1(); Node root = tree.init(); System.out.println("先序遍歷"); tree.theFirstTraversal_Stack(root); System.out.println(""); System.out.println("中序遍歷"); tree.theInOrderTraversal_Stack(root); System.out.println(""); System.out.println("後序遍歷"); tree.thePostOrderTraversal_Stack(root); System.out.println(""); } }