使用java实现一棵二叉树,并使用他完成二叉树的层次遍历,两种中序遍历(递归 &非递归)

使用java实现一棵二叉树,并使用他完成二叉树的层次遍历,两种中序遍历(递归 &非递归)

1、二叉树基本结构

public class BinaryTree {

    BinaryTree left;
    BinaryTree right;
    Integer val;
    
    public BinaryTree getLeft() {
        return left;
    }

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

    public BinaryTree getRight() {
        return right;
    }

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

    public Integer getVal() {
        return val;
    }

    public void setVal(Integer val) {
        this.val = val;
    }
    
}

2、二叉树层次遍历
首先将二叉树根节点放入到队列中,当队列不为空时,依次出队,并将出队的节点的左右孩子入队,再将该节点加入到结果集中

	/**
     * 二叉树层次遍历
     * @param root
     * @return
     */
    public List<BinaryTree> levelOrder(BinaryTree root){

        Queue<BinaryTree> queue = new LinkedList<>();
        List<BinaryTree> list = new LinkedList<>();

        queue.offer(root);
        while(!queue.isEmpty()){
            BinaryTree head = queue.poll();
            if(head.getLeft() != null){
                queue.offer(head.getLeft());
            }
            if(head.getRight() != null){
                queue.offer(head.getRight());
            }
            list.add(head);
        }

        return list;
    }

3、递归中序遍历

	 /**
     * 二叉树递归中序遍历
     * @param root
     */
    public void midOrderRecur(BinaryTree root){
        if (root == null){
            return;
        }
        midOrderRecur(root.left);
        System.out.print(root.getVal());
        midOrderRecur(root.right);
    }

4、非递归中序遍历

	/**
     * 二叉树中序遍历
     * @param root
     */
    public void midOrder(BinaryTree root){
        BinaryTree current = root;

        LinkedList<BinaryTree> list = new LinkedList<>();
        while(current != null || !list.isEmpty()){
            while(current != null){
                list.addFirst(current);
                current = current.left;
            }
            if(!list.isEmpty()){
                current = list.removeFirst();
                System.out.print(current.getVal());
                current = current.right;
            }
        }
    }

5、全部代码以及测试

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTree {

    BinaryTree left;
    BinaryTree right;
    Integer val;


    /**
     * 二叉树层次遍历
     * @param root
     * @return
     */
    public List<BinaryTree> levelOrder(BinaryTree root){

        Queue<BinaryTree> queue = new LinkedList<>();
        List<BinaryTree> list = new LinkedList<>();

        queue.offer(root);
        while(!queue.isEmpty()){
            BinaryTree head = queue.poll();
            if(head.getLeft() != null){
                queue.offer(head.getLeft());
            }
            if(head.getRight() != null){
                queue.offer(head.getRight());
            }
            list.add(head);
        }

        return list;
    }

    /**
     * 二叉树递归中序遍历
     * @param root
     */
    public void midOrderRecur(BinaryTree root){
        if (root == null){
            return;
        }
        midOrderRecur(root.left);
        System.out.print(root.getVal());
        midOrderRecur(root.right);
    }

    /**
     * 二叉树中序遍历
     * @param root
     */
    public void midOrder(BinaryTree root){
        BinaryTree current = root;

        LinkedList<BinaryTree> list = new LinkedList<>();
        while(current != null || !list.isEmpty()){
            while(current != null){
                list.addFirst(current);
                current = current.left;
            }
            if(!list.isEmpty()){
                current = list.removeFirst();
                System.out.print(current.getVal());
                current = current.right;
            }
        }
    }

    public static void main(String[] args) {
        BinaryTree node1 = new BinaryTree();
        BinaryTree node2 = new BinaryTree();
        BinaryTree node3 = new BinaryTree();
        BinaryTree node4 = new BinaryTree();

        node1.setVal(1);
        node2.setVal(5);
        node3.setVal(3);
        node4.setVal(4);

        node1.setLeft(node2);
        node1.setRight(node3);
        node2.setLeft(node4);

        System.out.println("层次遍历");
        List<BinaryTree> binaryTreeList = node1.levelOrder(node1);
        for(BinaryTree binaryTree : binaryTreeList){
            System.out.print(binaryTree.getVal());
        }
        System.out.println();

        System.out.println("中序遍历");
        node1.midOrder(node1);
        System.out.println();

        System.out.println("递归中序遍历");
        node1.midOrderRecur(node1);
    }

    public BinaryTree getLeft() {
        return left;
    }

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

    public BinaryTree getRight() {
        return right;
    }

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

    public Integer getVal() {
        return val;
    }

    public void setVal(Integer val) {
        this.val = val;
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值