二叉樹(建立,節點個數,高度,遍歷)
阿新 • • 發佈:2019-02-05
二叉樹的基礎
今天學習了二叉樹,有點膨脹,趕緊寫個部落格記下來,不然又忘記了。
直接在程式碼裡面解釋吧。。。
package BinaryTreeText; import java.util.Stack; /** * 二叉樹 * */ public class BinaryTree { // 根節點 private TreeNode root; // 二叉樹的節點域 public class TreeNode{ private int index; private String data; // 節點的資料域 private TreeNode leftChild; //相當於節點的指標域 private TreeNode rifhtChild; public TreeNode(int index, String data){ this.index = index; this.data = data; this.leftChild = null; this.rifhtChild = null; } public String getData() { return data; } public void setData(String data) { this.data = data; } } /** * // 初始化有一個根節點 * */ public BinaryTree(){ root = new TreeNode(1,"A"); root.leftChild = null; root.rifhtChild = null; } /** // 二叉樹的構建 * A * b C * d E F * */ public void createBinaryTree(){ TreeNode nodeB = new TreeNode(2,"B"); TreeNode nodeC = new TreeNode(3,"C"); TreeNode nodeD = new TreeNode(4,"D"); TreeNode nodeE = new TreeNode(5,"E"); TreeNode nodeF = new TreeNode(6,"F"); root.leftChild = nodeB; root.rifhtChild = nodeC; nodeB.leftChild = nodeD; nodeB.rifhtChild = nodeE; nodeC.rifhtChild = nodeF; } /** * // 求二叉樹的高度 * */ public int getHigh(){ return getHigh(root); } public int getHigh(TreeNode root){ if (null == root){ return 0; }else{ // 求子節點的高度--> 左子樹和右子樹 -->選出高度最高的為整個二叉樹的高度 int i = getHigh(root.leftChild); int j = getHigh(root.rifhtChild); return ((i>j)?(i+1):(j+1)); } } /** * * 求二叉樹的節點個數 */ public int getSize(){ return getSize(root); } public int getSize(TreeNode root){ if (null == root){ return 0; }else{ return 1+ getSize(root.leftChild) + getSize(root.rifhtChild); } } /** * * 二叉樹的遍歷 * * */ /** * * 前序遍歷 -->根,左,右(迭代) * */ public void preOrder(){ preOrder(root); } public void preOrder(TreeNode root){ if(null == root){ return ; }else{ System.out.println(root.data); preOrder(root.leftChild); preOrder(root.rifhtChild); } } /** * 前序遍歷非迭代 * * */ public void nonRecOrder(){ nonRecOrder(root); } public void nonRecOrder(TreeNode root){ // 採用棧的方法 // 出去一個根節點,進去兩個子節點 if (null==root){ return; } Stack<TreeNode> stack = new Stack<>(); stack.push(root); while(!stack.isEmpty()){ TreeNode t = stack.pop(); System.out.println(t.data); // 此處的入棧是有順序的 /* * 因為是前序遍歷二叉樹,(根,左,右) * 而棧 FILO 的結構,所以得先讓右兒子壓棧 * 這樣左兒子才能先出棧 * */ if (t.rifhtChild!=null){ stack.push(t.rifhtChild); } if(t.leftChild!=null){ stack.push(t.leftChild); } } } /** * 中序遍歷-->左,根,右 * * */ public void midOrder(){ midOrder(root); } public void midOrder(TreeNode root){ if (null == root){ return; }else{ midOrder(root.leftChild); System.out.println(root.data); midOrder(root.rifhtChild); } } /** * 後序遍歷 左右根 * */ public void postOrder(){ postOrder(root); } public void postOrder(TreeNode root){ if (null == root){ return; }else{ postOrder(root.leftChild); postOrder(root.rifhtChild); System.out.println(root.data); } } public static void main(String[] args) { BinaryTree binaryTree = new BinaryTree(); binaryTree.createBinaryTree(); int high = binaryTree.getHigh(); System.out.println(high); int size = binaryTree.getSize(); System.out.println(size); System.out.println("前序遍歷"); binaryTree.preOrder(); System.out.println("前序遍歷(非迭代)"); binaryTree.nonRecOrder(); System.out.println("中序遍歷"); binaryTree.midOrder(); System.out.println("後序遍歷"); binaryTree.postOrder(); } }
就這些了。。。