算法練習(xí)第四天(二叉樹相關(guān))

https://blog.csdn.net/qq_37186947/article/details/90681403

package com.baidu.insurprod.biz.util;

import java.util.LinkedList;
import java.util.Stack;

/**
 * @author meizhenhao
 * @Date 2020/11/9 下午4:52
 */
public class TwoTree {

    public static void main(String[] args) {
        int[] arr = {3, 1, 4, 2, 5};
        Node root = buildTwoChaTree(arr);
        System.out.print("遞歸實現(xiàn)先序遍歷  :");
        preForEach(root);
        System.out.println();

        System.out.print("非遞歸實現(xiàn)先序遍歷:");
        preForEachNO(root);
        System.out.println();

        System.out.print("遞歸實現(xiàn)中序遍歷  :");
        inForEach(root);
        System.out.println();

        System.out.print("非遞歸實現(xiàn)中序遍歷:");
        inForEachNO(root);
        System.out.println();

        System.out.print("遞歸實現(xiàn)后序遍歷  :");
        lastForEach(root);
        System.out.println();

        System.out.print("非遞歸實現(xiàn)后序遍歷:");
        lastForEachNO(root);
        System.out.println();

        System.out.print("實現(xiàn)層序遍歷     :");
        levelForEach(root);
        System.out.println();
    }

    private static void levelForEach(Node root) {
        if (root == null) {
            return;
        }
        // 使用隊列
        LinkedList<Node> queue = new LinkedList<>();
        queue.addLast(root);
        // 當(dāng)前層節(jié)點數(shù)、下一層節(jié)點數(shù)
        int high = 0, curLevelNum = 1, nextLevelNum = 0;
        while (!queue.isEmpty()) {
            Node temp = queue.removeFirst();
            System.out.print(temp.value + "     ");
            curLevelNum--;

            if (temp.left != null) {
                queue.addLast(temp.left);
                nextLevelNum++;
            }

            if (temp.right != null) {
                queue.addLast(temp.right);
                nextLevelNum--;
            }

            if (curLevelNum == 0) {
                high++;
                curLevelNum = nextLevelNum;
                nextLevelNum = 0;
            }
        }
    }

    // 先序遍歷,非遞歸
    private static void preForEachNO(Node root) {
        if (root == null) {
            return;
        }

        Stack<Node> stack = new Stack<>();
        while (root != null || !stack.isEmpty()) {
            // 先處理  根和左,全部入棧
            while (root != null) {
                stack.push(root);
                System.out.print(root.value + "      ");
                root = root.left;
            }

            // 在處理  右
            if (!stack.isEmpty()) {
                root = stack.pop().right;
            }
        }
    }

    // 先序遍歷,遞歸
    public static void preForEach(Node root) {
        if (root == null) {
            return;
        }
        System.out.print(root.value + "     ");
        preForEach(root.left);
        preForEach(root.right);
    }

    // 中序遍歷,非遞歸
    private static void inForEachNO(Node root) {
        if (root == null) {
            return;
        }

        Stack<Node> stack = new Stack<>();
        while (root != null || !stack.isEmpty()) {
            // 先處理 左根
            while (root != null) {
                stack.push(root);
                root = root.left;
            }

            if (!stack.isEmpty()) {
                root = stack.pop();
                System.out.print(root.value + "       ");
                root = root.right;
            }
        }
    }

    // 中序遍歷,遞歸
    public static void inForEach(Node root) {
        if (root == null) {
            return;
        }
        inForEach(root.left);
        System.out.print(root.value + "     ");
        inForEach(root.right);
    }

    // 后序遍歷,非遞歸
    private static void lastForEachNO(Node root) {
        if (root == null) {
            return;
        }
        Stack<Node> inStack = new Stack<>();
        Stack<Node> outStack = new Stack<>();
        inStack.push(root);

        while (!inStack.isEmpty()) {
            Node temp = inStack.pop();
            outStack.push(temp);

            if (temp.left != null) {
                inStack.push(temp.left);
            }

            if (temp.right != null) {
                inStack.push(temp.right);
            }
        }

        while (!outStack.isEmpty()) {
            System.out.print(outStack.pop().value + "       ");
        }

    }

    // 后序遍歷,遞歸
    public static void lastForEach(Node root) {
        if (root == null) {
            return;
        }
        lastForEach(root.left);
        lastForEach(root.right);
        System.out.print(root.value + "     ");
    }

    // 創(chuàng)建一個二叉排序樹
    public static Node buildTwoChaTree(int[] arr) {

        if (arr == null || arr.length <= 0) {
            return null;
        }

        Node root = new Node(arr[0]);
        for (int i = 1; i < arr.length; i++) {
            if (i <= arr.length - 1) {
                insertTwoChaTree(root, arr[i]);
            }
        }
        return root;
    }

    private static void insertTwoChaTree(Node root, int value) {
        if (value > root.value) {
            if (root.right == null) {
                root.right = new Node(value);
                return;
            }
            insertTwoChaTree(root.right, value);
        } else if (value < root.value) {
            if (root.left == null) {
                root.left = new Node(value);
                return;
            }
            insertTwoChaTree(root.left, value);
        }
    }
}

class Node {
    int value;
    Node left;
    Node right;
    public Node(int value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容