Java實現的二叉樹常用操作【前序建樹,前中後遞迴非遞迴遍曆及層序遍曆】,二叉樹遞迴

來源:互聯網
上載者:User

Java實現的二叉樹常用操作【前序建樹,前中後遞迴非遞迴遍曆及層序遍曆】,二叉樹遞迴

本文執行個體講述了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("幫客之家測試結果:");        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);    }}

運行結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.