溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java通過遞歸進(jìn)行二叉樹遍歷的代碼

發(fā)布時(shí)間:2020-04-08 09:40:38 來源:網(wǎng)絡(luò) 閱讀:277 作者:gugudajiao 欄目:編程語言

寫代碼過程中,將寫代碼過程重要的代碼片段收藏起來,下面的代碼是關(guān)于Java通過遞歸進(jìn)行二叉樹遍歷的代碼,應(yīng)該是對(duì)各朋友有一些好處。

package com.wzs;

public class TestBinaryTree {
    public static void main(String[] args) {
        Node<String> g = new Node<String>("G", null, null);
        Node<String> e = new Node<String>("E", null, null);
        Node<String> f = new Node<String>("F", null, null);
        Node<String> d = new Node<String>("D", null, g);
        Node<String> b = new Node<String>("B", d, e);
        Node<String> c = new Node<String>("C", null, f);
        Node<String> a = new Node<String>("A", b, c);

        System.out.println("生成的二叉樹:");
        System.out.println("            A");
        System.out.println("            |     ");
        System.out.println("       |---------|");
        System.out.println("       B         C");
        System.out.println("       |         |");
        System.out.println("  |---------|     -----|");
        System.out.println("  D         E          F");
        System.out.println("  |");
        System.out.println("   ----|");
        System.out.println("       G");

        System.out.println("二叉樹深度:" + BinaryTree.getDepth(a));

        System.out.print("前序遍歷:");
        BinaryTree.priorderTraversal(a);
        System.out.println();

        System.out.print("中序遍歷:");
        BinaryTree.inorderTraversal(a);
        System.out.println();

        System.out.print("后序遍歷:");
        BinaryTree.postorderTraversal(a);
        System.out.println();
    }
}

class BinaryTree {
    static <T> void priorderTraversal(Node<T> node) {
        if (node != null) {
            visitNode(node);
            priorderTraversal(node.getLeftChild());
            priorderTraversal(node.getRightChild());
        }
    }

    static <T> void inorderTraversal(Node<T> node) {
        if (node != null) {
            inorderTraversal(node.getLeftChild());
            visitNode(node);
            inorderTraversal(node.getRightChild());
        }
    }

    static <T> void postorderTraversal(Node<T> node) {
        if (node != null) {
            postorderTraversal(node.getLeftChild());
            postorderTraversal(node.getRightChild());
            visitNode(node);
        }
    }

    static <T> int getDepth(Node<T> node) {
        if (node == null) {
            return 0;
        }
        int leftDepth = 0;
        int rightDepth = 0;
        leftDepth = getDepth(node.getLeftChild());
        rightDepth = getDepth(node.getRightChild());
        return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1;
    }

    static <T> void visitNode(Node<T> node) {
        System.out.print(node.getKey() + " ");
    }
}

class Node<T> {
    private T key;
    private Node<T> leftChild;
    private Node<T> rightChild;

    public Node() {

    }

    public Node(T key, Node<T> leftChild, Node<T> rightChild) {
        super();
        this.key = key;
        this.leftChild = leftChild;
        this.rightChild = rightChild;
    }

    public T getKey() {
        return key;
    }

    public void setKey(T key) {
        this.key = key;
    }

    public Node<T> getLeftChild() {
        return leftChild;
    }

    public void setLeftChild(Node<T> leftChild) {
        this.leftChild = leftChild;
    }

    public Node<T> getRightChild() {
        return rightChild;
    }

    public void setRightChild(Node<T> rightChild) {
        this.rightChild = rightChild;
    }

}

輸出結(jié)果:

生成的二叉樹:  
            A  
            |       
       |---------|  
       B         C  
       |         |  
  |---------|     -----|  
  D         E          F  
  |  
   ----|  
       G  
二叉樹深度:4  
前序遍歷:A B D G E C F   
中序遍歷:D G B E A C F   
后序遍歷:G D E B F C A   
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI