1. 程式人生 > >數據結構Java版之遍歷二叉樹(六)

數據結構Java版之遍歷二叉樹(六)

val unit 說明 後續遍歷 auth AD oot org tor

  二叉樹是我們在程序中用的最多的一種樹(個人觀點)。最簡單的一個二叉樹是由一個根節點,兩個子節點(一左一右成左右孩子節點)組成。二叉樹是數組和鏈表的結合,即包含了數組的快速查找優點,又包含了鏈表的快速添加刪除的優點。熟練掌握二叉樹的遍歷方法是很有必要的。下面來介紹一下基於二叉樹的四種遍歷方式。

  下面我們以一個棵簡單的二叉樹為例來說明四種遍歷:

  技術分享圖片

創建樹節點類:

    //樹節點
    class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        
public TreeNode(char x) { val = x; } }

  中序遍歷:

  先遍歷左孩子節點---根節點---右孩子節點

//中序遍歷        左-根-右    (前中後:根的位置)
public void InOrder(TreeNode current){
    if(current != null) {
        InOrder(current.left);
        Visit(current);
        InOrder(current.right);
    }
}

  前序遍歷:

  遍歷根節點---左孩子節點---右孩子節點

//前序遍歷        根-左-右
public void PreOrder(TreeNode current){
    if(current != null) {
        Visit(current);
        InOrder(current.left);
        InOrder(current.right);
    }
}

  後序遍歷:

  遍歷左孩子節點---右孩子節點---根節點

    //後續遍歷        左-右-根
    public void PostOrder(TreeNode current){
        
if(current != null) { InOrder(current.left); InOrder(current.right); Visit(current); } }

  層序遍歷:

  同一層節點從左往右依次遍歷。

    //層序遍歷        同一層節點從左往右依次遍歷  FIFO
    public void LevelOrder(TreeNode current){
        List<TreeNode> q = new ArrayList<TreeNode>();
        while(current != null) {
            Visit(current);
            if(current.left != null) {
                q.add(current.left);
            }
            if(current.right != null) {
                q.add(current.right);
            }
            if(q.isEmpty()) return ;
            current = q.remove(0);
        }
    }

以上就是四種基本遍歷方式。下面我們來測試一下,首先寫一個工具函數,用來顯示當前節點的值。

    //*********輔助工具****************
    public void Visit(TreeNode node) {
        System.out.print(node.val);
    }

寫Test測試函數:

  

    @Test
    public void fun() {
        TreeNode root = new TreeNode(‘+‘);
        root.left = new TreeNode(‘-‘);
        root.right = new TreeNode(‘E‘);
        root.left.left = new TreeNode(‘*‘);
        root.left.right = new TreeNode(‘D‘);
        root.left.left.left = new TreeNode(‘/‘);
        root.left.left.right = new TreeNode(‘C‘);
        root.left.left.left.left = new TreeNode(‘A‘);
        root.left.left.left.right = new TreeNode(‘B‘);
        
        System.out.println("中序遍歷:");
        InOrder(root);
        System.out.println();
        System.out.println("前序遍歷:");
        PreOrder(root);
        System.out.println();
        System.out.println("後序遍歷:");
        PostOrder(root);
        System.out.println();
        System.out.println("層序遍歷:");
        LevelOrder(root);
        System.out.println();
    }

由上面的測試函數得到遍歷結果:

技術分享圖片

全部代碼:

package tree;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
/**
 * 二叉數的遍歷
 *                     +
 *                 -         E
 *             *        D
 *        /        C
 *   A        B
 * @author Ranter
 *
 */
public class BinaryTree {
    @Test
    public void fun() {
        TreeNode root = new TreeNode(‘+‘);
        root.left = new TreeNode(‘-‘);
        root.right = new TreeNode(‘E‘);
        root.left.left = new TreeNode(‘*‘);
        root.left.right = new TreeNode(‘D‘);
        root.left.left.left = new TreeNode(‘/‘);
        root.left.left.right = new TreeNode(‘C‘);
        root.left.left.left.left = new TreeNode(‘A‘);
        root.left.left.left.right = new TreeNode(‘B‘);
        
        System.out.println("中序遍歷:");
        InOrder(root);
        System.out.println();
        System.out.println("前序遍歷:");
        PreOrder(root);
        System.out.println();
        System.out.println("後序遍歷:");
        PostOrder(root);
        System.out.println();
        System.out.println("層序遍歷:");
        LevelOrder(root);
        System.out.println();
    }
    
    //****************************************************************
    
    //中序遍歷        左-根-右    (前中後:根的位置)
    public void InOrder(TreeNode current){
        if(current != null) {
            InOrder(current.left);
            Visit(current);
            InOrder(current.right);
        }
    }
    //前序遍歷        根-左-右
    public void PreOrder(TreeNode current){
        if(current != null) {
            Visit(current);
            InOrder(current.left);
            InOrder(current.right);
        }
    }
    //後續遍歷        左-右-根
    public void PostOrder(TreeNode current){
        if(current != null) {
            InOrder(current.left);
            InOrder(current.right);
            Visit(current);
        }
    }
    //層序遍歷        同一層節點從左往右依次遍歷(使用隊列層序遍歷)
    public void LevelOrder(TreeNode current){
        List<TreeNode> q = new ArrayList<TreeNode>();
        while(current != null) {
            Visit(current);
            if(current.left != null) {
                q.add(current.left);
            }
            if(current.right != null) {
                q.add(current.right);
            }
            if(q.isEmpty()) return ;
            current = q.remove(0);
        }
    }
    
    //*********輔助工具****************
    public void Visit(TreeNode node) {
        System.out.print(node.val);
    }
    
    
    
    //樹節點
    class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char x) {
            val = x;
        }
    }
}

數據結構Java版之遍歷二叉樹(六)