二叉樹的創建和遍歷——Java

package tree;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class Tree {

    /*
    * 創建樹,前序
    * */
    public static Note createTree(Queue<Character> que) {
        char s = que.poll();
        if ( s == ',') return null;
        Note t = new Note();
        t.data = s;
        t.left = createTree(que);
        t.right = createTree(que);
        return t;
    }
    /*
    * 創建樹,中序
    * */
    public static Note createTree2(Queue<Character> que) {
        char c = que.poll();
        if (c == ',') return null;
        Note t = new Note();
        t.left = createTree2(que);
        t.data = c;
        t.right = createTree2(que);
        return t;
    }
    /*
    * 創建樹,後續
    * */
    public static Note createTree3(Queue<Character> que) {
        char c = que.poll();
        if (c == ',') return null;
        Note t = new Note();
        t.left = createTree3(que);
        t.right = createTree3(que);
        t.data = c;
        return t;
    }
    /*
    * 前序遍歷,遞歸
    * */
    public static void before (Note t) {
        if (t == null) return;
        System.out.print(t.data);
        before(t.left);
        before(t.right);
    }
    /*
    * 前序遍歷,非遞歸,方法一
    * */
    public static void before2(Note t) {
        Stack<Note> s = new Stack<>();
        s.push(t);
        System.out.print(t.data);
        t = t.left;
        while(!s.isEmpty() || t != null) {
            if (t != null) {
                s.push(t);
                System.out.print(t.data);
                t = t.left;
            } else {
                t = s.pop();
                t = t.right;
            }
        }
    }
    /*
     * 前序遍歷,非遞歸,方法二
     * */
    public static void before3(Note t) {
        Stack<Note> s  = new Stack<>();
        s.push(t);
        System.out.print(t.data);
        t = t.left;
        while (!s.isEmpty()) {
            while(t != null) {
                s.push(t);
                System.out.print(t.data);
                t = t.left;
            }
            t = s.pop();
            t = t.right;

        }
    }

    /*
    * 中序遍歷,遞歸
    * */
    public static void center (Note t) {
        if (t == null) return;
        center(t.left);
        System.out.print(t.data);
        center(t.right);
    }
    /*
     * 中序遍歷,非遞歸,方法一
     * */
    public static void center2(Note t) {
        Stack<Note> s = new Stack<>();
        s.push(t);
        t = t.left;
        while (!s.isEmpty() || t != null) {
            if (t != null) {
                s.push(t);
                t = t.left;
            } else {
                t = s.pop();
                System.out.print(t.data);
                t = t.right;
            }
        }
    }
    /*
    * 中序遍歷,非遞歸,方法二
    * */
    public static void center3(Note t) {
        Stack<Note> s = new Stack<>();
        s.push(t);
        t = t.left;
        while(!s.isEmpty()) {
            while(t != null) {
                s.push(t);
                t = t.left;
            }
            t = s.pop();
            System.out.print(t.data);
            t = t.right;
        }
    }

    /*
    * 後續遍歷,遞歸
    * */
    public static void after (Note t) {
        if (t == null) return;
        after(t.left);
        after(t.right);
        System.out.print(t.data);
    }
    /*
    * 後續遍歷,非遞歸,方法一
    * */
    public static void after2 (Note t) {
        Stack<Note> s = new Stack<>();
        s.push(t);
        s.push(t);
        t = t.left;
        while (!s.isEmpty()) {
            if (t != null) {
                s.push(t);
                s.push(t);
                t = t.left;
            } else {
                t = s.pop();
                if (!s.isEmpty() && t == s.peek()) {   //&&是惰性求值,如果前邊的就能確定結果將不會進行後面的表達式運行算(此處用到這個特性的原因是當棧爲空時使用peek會報錯)
                    // 第一次取得此節點要先遍歷其右子樹,不能輸出
                    t = t.right;
                } else {      //第二次輸出
                    System.out.print(t.data);
                    t = null;
                }
            }
        }
    }
    /*
    * 後序遍歷,非遞歸,方法二
    * */
    public static void after3 (Note t) {
        Stack<Note> s = new Stack<>();
        s.push(t);
        s.push(t);
        t = t.left;
        while (!s.isEmpty()) {
            while (t != null) {
                s.push(t);
                s.push(t);
                t = t.left;
            }
            t = s.pop();
            if (!s.isEmpty() && t == s.peek()) {
                t = t.right;
            } else {
                System.out.print(t.data);
                t = null;
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        Queue<Character> que = new LinkedList<>();   //因爲Java中沒有一次讀取一個字符的機制所以我把整個字符串整個讀進來然後一個字符一個字符的添加到隊列,在使用的時候依次拿出
        for (int i = 0;i < s.length(); i ++) {
            que.add(s.charAt(i));
        }
        Note tree = createTree(que);
        before(tree);
        System.out.println();
        before2(tree);
        System.out.println();
        before3(tree);
        System.out.println();
        center(tree);
        System.out.println();
        center2(tree);
        System.out.println();
        center3(tree);
        System.out.println();
        after(tree);
        System.out.println();
        after2(tree);
        System.out.println();
        after3(tree);
        System.out.println();
    }
    static class Note {
        public char data;
        public Note left = null;
        public Note right = null;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章