国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java實現的二叉樹常用操作【前序建樹,前中后遞歸非遞歸遍歷及層序遍歷】

2024-07-13 10:17:00
字體:
來源:轉載
供稿:網友

本文實例講述了Java實現的二叉樹常用操作。分享給大家供大家參考,具體如下:

import java.util.ArrayDeque;import java.util.Queue;import java.util.Stack;//二叉樹的建樹,前中后 遞歸非遞歸遍歷 層序遍歷//Node節點class Node {    int element;    Node left;    Node right;    public Node() {    }    public Node(int element) {        this.element = element;    }}// BinaryTreepublic 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("VeVb武林網測試結果:");        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);    }}

運行結果:

Java,二叉樹,前序建樹,前中后遞歸,非遞歸遍歷,層序遍歷

 

希望本文所述對大家java程序設計有所幫助。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 策勒县| 崇仁县| 高尔夫| 凤凰县| 施秉县| 香河县| 拜泉县| 颍上县| 睢宁县| 嫩江县| 顺平县| 井冈山市| 和龙市| 故城县| 洪湖市| 昌都县| 普兰店市| 崇明县| 康乐县| 修水县| 昌平区| 南乐县| 施甸县| 景泰县| 新乐市| 桦南县| 新郑市| 邢台市| 新和县| 长泰县| 永康市| 南乐县| 文水县| 永安市| 土默特右旗| 宜章县| 金昌市| 淮北市| 西贡区| 满洲里市| 阿图什市|