重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Java通过递归进行二叉树遍历的代码

写代码过程中,将写代码过程重要的代码片段收藏起来,下面的代码是关于Java通过递归进行二叉树遍历的代码,应该是对各朋友有一些好处。

创新互联专注于企业全网整合营销推广、网站重做改版、上海网站定制设计、自适应品牌网站建设、H5场景定制电子商务商城网站建设、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为上海等各大城市提供网站开发制作服务。

package com.wzs;

public class TestBinaryTree {
    public static void main(String[] args) {
        Node g = new Node("G", null, null);
        Node e = new Node("E", null, null);
        Node f = new Node("F", null, null);
        Node d = new Node("D", null, g);
        Node b = new Node("B", d, e);
        Node c = new Node("C", null, f);
        Node a = new Node("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  void priorderTraversal(Node node) {
        if (node != null) {
            visitNode(node);
            priorderTraversal(node.getLeftChild());
            priorderTraversal(node.getRightChild());
        }
    }

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

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

    static  int getDepth(Node 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  void visitNode(Node node) {
        System.out.print(node.getKey() + " ");
    }
}

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

    public Node() {

    }

    public Node(T key, Node leftChild, Node 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 getLeftChild() {
        return leftChild;
    }

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

    public Node getRightChild() {
        return rightChild;
    }

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

}

输出结果:

生成的二叉树:  
            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   

本文名称:Java通过递归进行二叉树遍历的代码
文章起源:http://cqcxhl.cn/article/jgcgjo.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP