二叉树的遍历及创建(java)

直接上代码

package com.example.zhanghaohao089.mytest;

/**
 * Node类
 *
 * @author ZHANGHAOHAO089
 * @date 2017/5/31
 */

public class Node {
    private int data;
    private Node left;
    private Node right;

    public Node(int data) {
        this.data = data;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public Node getLeft() {
        return left;
    }

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

    public Node getRight() {
        return right;
    }

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

创建Tree类

package com.example.zhanghaohao089.mytest;

/**
 * 创建Tree类
 *
 * @author ZHANGHAOHAO089
 * @date 2017/5/31
 */

public class CreateTree {

    public static void create() {
        int[] array = {5,1,2,3,4,6,7,8,9};
        Node root = new Node(array[0]);
        for(int i = 1; i< array.length; i++) {
            insert(root, array[i]);
        }
        System.out.println("------------before----------------");
        FindTree.beforeOrder(root);
        System.out.println();
        System.out.println("------------in----------------");
        FindTree.inOrder(root);
        System.out.println();
        System.out.println("------------after----------------");
        FindTree.afterOrder(root);
    }

    //left < node < right
    public static void insert(Node node, int data) {
        if (node == null)
            return;
        if (data < node.getData()) {
            if (node.getLeft() == null) {
                node.setLeft(new Node(data));
            } else {
                insert(node.getLeft(), data);
            }
        } else {
            if (node.getRight() == null) {
                node.setRight(new Node(data));
            } else {
                insert(node.getRight(), data);
            }
        }
    }

}

编辑类

package com.example.zhanghaohao089.mytest;

/**
 * 遍历Tree类
 *
 * @author ZHANGHAOHAO089
 * @date 2017/5/31
 */

public class FindTree {
    private static void visit(int data) {
        System.out.print(data + " ");
    }

    //root左右
    public static void beforeOrder(Node node) {
        if (node == null)
            return;
        visit(node.getData());
        beforeOrder(node.getLeft());
        beforeOrder(node.getRight());
    }

    //左root右
    public static void inOrder(Node node) {
        if (node == null)
            return;
        inOrder(node.getLeft());
        visit(node.getData());
        inOrder(node.getRight());
    }

    //左右root
    public static void afterOrder(Node node) {
        if (node == null)
            return;
        afterOrder(node.getLeft());
        afterOrder(node.getRight());
        visit(node.getData());
    }
}

测试类

package com.example;

public class Test {
    public static void main(String args[]) {
        CreateTree.create();
    }
}

输出结果:

------------before----------------
5 2 3 4 6 7 8 9 
------------in----------------
2 3 4 5 6 7 8 9 
------------after----------------
4 3 2 9 8 7 6 5 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值