二叉树的遍历

package com.cxy.springdataredis.data;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();

        BinaryNode binaryNode = new BinaryNode(1,"111");
        BinaryNode binaryNode1 = new BinaryNode(2,"222");
        BinaryNode binaryNode2 = new BinaryNode(3,"333");
        BinaryNode binaryNode3 = new BinaryNode(4,"444");
        BinaryNode binaryNode4 = new BinaryNode(5,"555");

        binaryNode.setLeft(binaryNode1);
        binaryNode.setRight(binaryNode2);
        binaryNode2.setLeft(binaryNode3);
        binaryNode3.setLeft(binaryNode4);

        binaryTree.setRoot(binaryNode);
        binaryTree.preOrder();
        System.out.println("----------------");
        binaryTree.postOrder();
        System.out.println("----------------");
        binaryTree.infixOrder();


    }

}
class BinaryTree{
    private BinaryNode root;

    public void setRoot(BinaryNode root) {
        this.root = root;
    }
    //前序遍历
    public  void preOrder(){
        if(this.root !=null){
            this.root.preOrder();
        }else{
            System.out.println("二叉树为空");
        }
    }
    public  void infixOrder(){
        if(this.root !=null){
            this.root.infixOrder();
        }else{
            System.out.println("二叉树为空");
        }
    }
    public  void postOrder(){
        if(this.root !=null){
            this.root.postOrder();
        }else{
            System.out.println("二叉树为空");
        }
    }
}
class BinaryNode{
    private int num;
    private String name;
    private BinaryNode left;
    private BinaryNode right;

    public BinaryNode(int num ,String name){
        this.num =num;
        this.name =name;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BinaryNode getLeft() {
        return left;
    }

    public void setLeft(BinaryNode left) {
        this.left = left;
    }

    public BinaryNode getRight() {
        return right;
    }

    public void setRight(BinaryNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "BinaryNode{" +
                "num=" + num +
                ", name='" + name + '\'' +
                '}';
    }

    //前序遍历方法:
    public void preOrder(){
        System.out.println(this);
        if (this.left != null){
            this.left.preOrder();
        }
        if (this.right != null){
            this.right.preOrder();
        }
    }

    public void infixOrder(){

        if (this.left != null){
            this.left.preOrder();
        }
        System.out.println(this);
        if (this.right != null){
            this.right.preOrder();
        }
    }

    public void postOrder(){

        if (this.left != null){
            this.left.preOrder();
        }

        if (this.right != null){
            this.right.preOrder();
        }
        System.out.println(this);
    }

}

 

转载于:https://www.cnblogs.com/xiufengchen/p/11540441.html

你可能感兴趣的:(二叉树的遍历)