java语言实现二叉树的遍历和查找(前序、中序、后序)

这篇博客介绍了如何使用Java语言实现二叉树的前序、中序和后序遍历。首先定义了节点类,包含遍历方法,接着创建二叉树类,设置根节点并实现遍历操作。最后通过测试用例展示具体的遍历过程。
摘要由CSDN通过智能技术生成
  • 首先,创建节点类,并在节点中定义前序、中序、后序的相关方法
public class Node {
    private int id;
    private String name;
    private Node left;
    private Node right;

    public Node(int id, String name) {
        this.id = id;
        this.name = name;
    }

	//getter,setter,toString方法省略

	//前序遍历
    public void preOrder(){
        //先遍历根节点
        System.out.println(this);
        //再遍历左子树
        //如果左子树不为空,则对左子树进行前序遍历
        if (this.left != null){
            this.left.preOrder();
        }
        //再遍历右子树
        //如果右子树不为空,则对右子树进行前序遍历
        if (this.right != null){
            this.right.preOrder();
        }
    }

	//中序遍历
    public void midOrder(){
        if (this.left != null){
            this.left.midOrder();
        }
        System.out.println(this);

        if (this.right != null){
            this.right.midOrder();
        }
    }

	//后序遍历
    public void postOrder(){

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

    //   查找     /
    //前序查找
    public Node preSearch(int no){
        //先判断当前节点的值是否为查找的值
        //是的话就直接返回
        if (this.id == no){
            return this;
        }
        //当前节点不是要找的就转到左子树
        Node current = null;
        //左子树不为空的话,前序查找
        //用current记录查找到的节点
        if (this.left != null){
            current =  this.left.preSearch(no);
        }
        //如果current不为空的话,说明在左子树中找到了,直接返回
        //否则继续遍历右子树
        if (current != null){
            return current;
        }
        //左子树找不到就对右子树进行前序查找
        if (this.right != null){
            current = this.right.preSearch(no);
        }
        //最后返回current,右子树中找到了话,current就是那个要找的节点
        //找不到的话,current为空,这时候返回也说明整棵树都没找到
        return current;
    }

	//中序查找
    public Node midSearch(int no){
        Node current = null;
        if (this.left != null){
            current = this.left.midSearch(no);
        }
        if (current != null){
            return current;
        }
        if (this.id == no){
            return this;
        }
        if (this.right != null){
            current = this.right.midSearch(no);
        }
        return current;
    }

	//后序查找
    public Node postSearch(int no){
        Node current = null;
        if (this.left != null){
            current = this.left.postSearch(no);
        }
        if (current != null){
            return current;
        }
        if (this.right != null){
            current = this.right.postSearch(no);
        }
        if (current != null){
            return current;
        }
        if (this.id == no){
            return this;
        }
        return current;
    }
}

  • 然后,创建二叉树类,定义根节点属性,也需要前序、中序、后序的相关方法
public class BinaryTree {
    private Node root;

    public BinaryTree(Node root) {
        this.root = root;
    }
    
    public void preOrder(){
        if (this.root != null){
            this.root.preOrder();
        }
        else {
            System.out.println("binary tree is empty");
        }
    }
    public void midOrder(){
        if (this.root != null){
            this.root.midOrder();
        }
        else {
            System.out.println("binary tree is empty");
        }
    }
    public void postOrder(){
        if (this.root != null){
            this.root.postOrder();
        }
        else {
            System.out.println("binary tree is empty");
        }
    }

    /
    public Node preSearch(int no){
        if (this.root != null){
            return this.root.preSearch(no);
        }else {
            return null;
        }
    }

    public Node midSearch(int no){
        if (this.root != null){
            return this.root.midSearch(no);
        }else {
            return null;
        }
    }

    public Node postSearch(int no){
        if (this.root != null){
            return this.root.postSearch(no);
        }else {
            return null;
        }
    }
}

  • 测试,先定义节点和二叉树,设置好节点之间的关系
public class BinaryTreeTest {
    public static void main(String[] args) {
        Node node1 = new Node(1,"one");
        Node node2 = new Node(2,"two");
        Node node3 = new Node(3,"three");
        Node node4 = new Node(4,"four");
        Node node5 = new Node(5,"five");

        node1.setLeft(node2);
        node1.setRight(node3);
        node3.setRight(node5);
        node3.setLeft(node4);
        BinaryTree binaryTree = new BinaryTree(node1);

        binaryTree.preOrder();
        System.out.println("------------------------");
        binaryTree.midOrder();
        System.out.println("------------------------");
        binaryTree.postOrder();
        System.out.println("------------------------");
        Node node = binaryTree.preSearch(4);
        System.out.println(node);
    }
}

  • 二叉树
    在这里插入图片描述
  • 遍历结果
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值