二叉树的层次遍历、先序遍历、中序遍历、后序遍历以及满二叉树的建立方法的java代码实现(先中后遍历采用递归法)

节点定义

/**
 * className:Node
 *
 * @author:zjl
 * @version:0.1
 * @date:2020/7/1219:39
 * @since:jdk1.8
 */
public class Node {

    private Node lChild;//左指针域
    private Object data;//数据域
    private Node rChild;//右指针域

    public Node(Node lChild, Object data, Node rChild) {
        this.lChild = lChild;
        this.data = data;
        this.rChild = rChild;
    }

    public Node getlChild() {
        return lChild;
    }

    public void setlChild(Node lChild) {
        this.lChild = lChild;
    }

    public Object getData() {
        return data;
    }

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

    public Node getrChild() {
        return rChild;
    }

    public void setrChild(Node rChild) {
        this.rChild = rChild;
    }

    @Override
    public void finalize() throws Throwable {
        super.finalize();
    }
}

树定义(定义了指向根节点的指针)

/**
 * className:BinaryTree
 *
 * @author:zjl
 * @version:0.1
 * @date:2020/7/1219:43
 * @since:jdk1.8
 */
public class BinaryTree {

    private Node p;//指向根节点

    public BinaryTree() {
        this.p = new Node(null,null,null);
    }

    public Node getP() {
        return p;
    }

    public void setP(Node p) {
        this.p = p;
    }

}

工具类

import java.util.ArrayList;

/**
 * className:Util
 *
 * @author:zjl
 * @version:0.1
 * @date:2020/7/1219:46
 * @since:jdk1.8
 */
public class Util {
    /**
     * 通过一个数组创建一个满二叉树
     *
     * @param obj
     * @return
     */
    public BinaryTree creat(Object[] obj) {
        BinaryTree binaryTree = new BinaryTree();
        Node q = binaryTree.getP();
        q.setData(obj[0]);
        ArrayList<Node> list = new ArrayList<>();
        list.add(q);
        int i = 1;
        while (list.size() != 0) {
            // System.out.println(list.size());
//            System.out.println("------------");
//            System.out.println(binaryTree.getP().getData());
//            System.out.println("------------");

            Node firstNode = list.get(0);
            for (int j = 0; j < list.size() - 1; j++) {
                list.set(j, list.get(j + 1));
            }
            list.remove(list.size() - 1);
            Node node1 = new Node(null, obj[i], null);
            firstNode.setlChild(node1);
            list.add(node1);
            i++;
            if (i >= obj.length)
                break;
            Node node2 = new Node(null, obj[i], null);
            firstNode.setrChild(node2);
            list.add(node2);
            i++;
            if (i >= obj.length)
                break;

        }
       // level(binaryTree);
        return binaryTree;
    }


    /**
     * 层次遍历
     * @param binaryTree
     */
    public void level(BinaryTree binaryTree) {
        Node q = binaryTree.getP();
        //System.out.println(binaryTree.getP().getData());
        ArrayList<Node> list = new ArrayList<>();
        list.add(q);
        while (list.size() != 0) {
            Node firstNode = list.get(0);
            System.out.print(firstNode.getData() + "->");
            for (int j = 0; j < list.size() - 1; j++) {
                list.set(j, list.get(j + 1));
            }
            list.remove(list.size() - 1);
            if (firstNode.getlChild() != null) {
                list.add(firstNode.getlChild());
            }
            if (firstNode.getrChild() != null) {
                list.add(firstNode.getrChild());
            }

        }

    }

    /**
     * 先序遍历
     * @param p 指向根节点的指针(模拟)
     */

    public void preOrder(Node p){
        if(p!=null){
            System.out.print(p.getData()+"->");
            preOrder(p.getlChild());
            preOrder(p.getrChild());
        }
    }

    /**
     * 中序遍历
     * @param p 指向根节点的指针(模拟)
     */
    public void inOrder(Node p){
        if(p!=null){
            preOrder(p.getlChild());
            System.out.print(p.getData()+"->");
            preOrder(p.getrChild());
        }
    }
    /**
     * 后序遍历
     * @param p 指向根节点的指针(模拟)
     */
    public void postOrder(Node p){
        if(p!=null){
            preOrder(p.getlChild());
            preOrder(p.getrChild());
            System.out.print(p.getData()+"->");
        }
    }
}

测试类

/**
 * className:Test
 *
 * @author:zjl
 * @version:0.1
 * @date:2020/7/1221:47
 * @since:jdk1.8
 */
public class Test {

    public static void main(String[] args) {
        Object[] test = {0,1,2,3,4,5,6,7,8,9};
        BinaryTree binaryTree = new BinaryTree();
        Util util = new Util();
        BinaryTree binaryTree1 = util.creat(test);
        System.out.println();
        System.out.println("========层次遍历========");
        util.level(binaryTree1);
        System.out.println();
        System.out.println("========先序遍历========");
        util.preOrder(binaryTree1.getP());
        System.out.println();
        System.out.println("========中序遍历========");
        util.inOrder(binaryTree1.getP());
        System.out.println();
        System.out.println("========后序遍历========");
        util.postOrder(binaryTree1.getP());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值