欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Java怎么實(shí)現(xiàn)的二叉樹常用操作-創(chuàng)新互聯(lián)

小編給大家分享一下Java怎么實(shí)現(xiàn)的二叉樹常用操作,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),望花企業(yè)網(wǎng)站建設(shè),望花品牌網(wǎng)站建設(shè),網(wǎng)站定制,望花網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,望花網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

具體如下:

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;
//二叉樹的建樹,前中后 遞歸非遞歸遍歷 層序遍歷
//Node節(jié)點(diǎn)
class Node {
    int element;
    Node left;
    Node right;
    public Node() {
    }
    public Node(int element) {
        this.element = element;
    }
}
// BinaryTree
public class Tree {
    // creat tree from array
    public static Node creatTree(int[] data, int i) {
        if (i >= data.length || data[i] == -1)
            return null;
        Node temp = new Node(data[i]);
        temp.left = creatTree(data, i * 2 + 1);
        temp.right = creatTree(data, i * 2 + 2);
        return temp;
    }
    // pre前序遍歷遞歸
    public static void pre(Node temp) {
        if (temp == null)
            return;
        System.out.print(temp.element + " ");
        pre(temp.left);
        pre(temp.right);
    }
    // mid中序遍歷遞歸
    public static void mid(Node temp) {
        if (temp == null)
            return;
        mid(temp.left);
        System.out.print(temp.element + " ");
        mid(temp.right);
    }
    // last后序遍歷遞歸
    public static void last(Node temp) {
        if (temp == null)
            return;
        last(temp.left);
        last(temp.right);
        System.out.print(temp.element + " ");
    }
    // pre1前序遍歷非遞歸
    public static void pre1(Node temp) {
        Stack<Node> stack = new Stack<>();
        while (temp != null || !stack.isEmpty()) {
            while (temp != null) {
                stack.push(temp);
                System.out.print(temp.element + " ");
                temp = temp.left;
            }
            if (!stack.isEmpty()) {
                temp = stack.pop().right;
            }
        }
    }
    // mid1中序遍歷非遞歸
    public static void mid1(Node temp) {
        Stack<Node> stack = new Stack<>();
        while (temp != null || !stack.isEmpty()) {
            while (temp != null) {
                stack.push(temp);
                temp = temp.left;
            }
            if (!stack.isEmpty()) {
                temp = stack.pop();
                System.out.print(temp.element + " ");
                temp = temp.right;
            }
        }
    }
    // last1后序遍歷非遞歸
    public static void last1(Node temp) {
        Stack<Node> stack = new Stack<>();
        Stack<Node> stack2 = new Stack<>();
        while (temp != null || !stack.isEmpty()) {
            while (temp != null) {
                stack.push(temp);
                stack2.push(temp);
                temp = temp.right;
            }
            if (!stack.isEmpty()) {
                temp = stack.pop().left;
            }
        }
        while (!stack2.isEmpty())
            System.out.print(stack2.pop().element + " ");
    }
    // ceng層序遍歷
    public static void ceng(Node temp) {
        if (temp == null)
            return;
        Queue<Node> queue = new ArrayDeque<>();
        queue.offer(temp);
        while (!queue.isEmpty()) {
            temp = queue.poll();
            System.out.print(temp.element + " ");
            if (temp.left != null)
                queue.offer(temp.left);
            if (temp.right != null)
                queue.offer(temp.right);
        }
    }
    // Demo
    public static void main(String[] args) {
        int[] array = { 1, 2, 3, 4, 5, 6, 7, -1, -1, 10, -1, -1, 13 };
        Node tree = creatTree(array, 0);
        System.out.println("創(chuàng)新互聯(lián)測試結(jié)果:");
        pre(tree);
        System.out.println();
        pre1(tree);
        System.out.println();
        mid(tree);
        System.out.println();
        mid1(tree);
        System.out.println();
        last(tree);
        System.out.println();
        last1(tree);
        System.out.println();
        ceng(tree);
    }
}

運(yùn)行結(jié)果:

Java怎么實(shí)現(xiàn)的二叉樹常用操作

看完了這篇文章,相信你對(duì)“Java怎么實(shí)現(xiàn)的二叉樹常用操作”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)頁題目:Java怎么實(shí)現(xiàn)的二叉樹常用操作-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://chinadenli.net/article48/cepshp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、ChatGPT網(wǎng)站改版響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名
成年午夜在线免费视频| 麻豆剧果冻传媒一二三区| 精品一区二区三区乱码中文| 国产精品欧美日韩中文字幕| 欧美久久一区二区精品| 中文字幕亚洲精品乱码加勒比| 免费观看潮喷到高潮大叫| 日韩一区二区三区观看| 久草视频这里只是精品| 国产精品亚洲欧美一区麻豆| 亚洲男人的天堂色偷偷| 国产又粗又深又猛又爽又黄| 日韩特级黄色大片在线观看| 日韩精品视频一二三区| 欧美国产在线观看精品| 国产日韩欧美在线亚洲| 日系韩系还是欧美久久| 午夜精品在线视频一区| 高清国产日韩欧美熟女| 成年女人午夜在线视频| 亚洲精品蜜桃在线观看| 一区二区三区在线不卡免费| 国产一区二区三区色噜噜| 91人妻人澡人人爽人人精品| 亚洲一区二区三区有码| 99国产精品国产精品九九 | 99日韩在线视频精品免费| 亚洲深夜精品福利一区| 中文字幕亚洲人妻在线视频 | 成人欧美一区二区三区视频| 免费特黄一级一区二区三区| 精品精品国产自在久久高清| 日本免费熟女一区二区三区| 亚洲综合色在线视频香蕉视频| 国产免费一区二区不卡| 精品欧美日韩一区二区三区 | 久久re6热在线视频| 黄男女激情一区二区三区| 午夜福利视频六七十路熟女| 国产精品伦一区二区三区四季 | 在线观看视频成人午夜|