数据结构——在二叉树中查找指定的节点(Java)

依据二叉树的遍历方式,查找二叉树中的指定的节点,也有三种方式:

按照前序遍历的顺序查找:

正确代码:

public Node preOrderSearch(int num) {
    System.out.println("当前的节点数值为:" + this.num);
    Node res = null;
    if (this.num == num) {
        return this;
    }
    if (this.left != null) {
        res = this.left.preOrderSearch(num);
    }
    if (res != null) {
        return res;
    }
    if (this.right != null) {
        res = this.right.preOrderSearch(num);
    }
    return res;
}

 在刚开始的时候,我认为没必要设置一个res的变量记录this.left.preOrderSearch(num)或者this.right.preOrderSearch(num)的值,思路和前序遍历的一样,只要判断该节点的值是不是要找的那个值即可,但是在运行代码以后,发现了问题。代码中构建的二叉树如下图所示。

(个人觉得:弄清楚递归程序最好的方式就是debug)

假设,要找到值为5的节点

递归程序的运行是这样的:【如果看不下去,可以直接设断点调试】

节点1,判断1和5的值相不相等,结果是不相等,判断左节点不为空,则进入左节点,左节点2与5不相等,则判断节点2有无左节点,节点2没有左节点,这时res=null,判断节点2有没有右节点,节点2没有右节点,所以回退到节点1判断左节点的时候,res的值为null,接着判断节点1的右节点是否为空,右节点不为空,进入右节点3,3不等于5,则继续判断节点3的左节点,节点3的左节点是5,这时,节点5就是我们要找的节点,则会把该节点返回,注意,返回到的节点是节点3的左节点判断语句,这时res被赋值为节点5,跳出if,进入判断res是否为空if语句,这时返回res,返回的值是到了节点1的右节点判断语句,res被赋值为节点5,这时节点1的左节点和右节点都判断完毕了,返回res,就可以输出了。

中序遍历和后序遍历也都是一样的理解方式。

单从要查找节点5这个具体的例子来说,后续遍历能够最快找到。

 

按照中序遍历的顺序查找

public Node infixOrderSearch(int num) {

    Node res = null;
    if (this.left != null) {
        res = this.left.infixOrderSearch(num);
    }
    if (res != null) {
        return res;
    }
    System.out.println("当前的节点数值为:" + this.num);
    if (this.num == num) {
        return this;
    }
    if (this.right != null) {
        res = this.right.infixOrderSearch(num);
    }
    return res;
}

按照后序遍历的顺序查找

public Node postOrderSearch(int num) {
    Node res = null;
    if (this.left != null) {
        res = this.left.postOrderSearch(num);
    }
    if (res != null) {
        return res;
    }
    if (this.right != null) {
        res = this.right.postOrderSearch(num);
    }
    if (res != null) {
        return res;
    }
    System.out.println("当前的节点数值为:" + this.num);
    if (this.num == num) {
        return this;
    }
    return res;
}

完整的代码(可直接运行):

package tree;

public class BinaryTreeDemo {

    public static void main(String[] args) {
        //创建一棵二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建需要的节点
        Node root = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);
        Node n4 = new Node(4);

        //新增加的节点
        Node n5 = new Node(5);

        //暂时手动创建二叉树
        root.setLeft(n2);
        root.setRight(n3);
        n3.setRight(n4);
        n3.setLeft(n5);

        //测试
        //前序遍历 :1 2 3 4
        //添加节点之后 : 1 2 3 5 4
        System.out.println("前序遍历");
        binaryTree.setRoot(root);
        binaryTree.preOrder();

        //中序遍历 :2 1 3 4
        //添加节点之后 :2 1 5 3 4
        System.out.println("中序遍历");
        binaryTree.infixOrder();

        //后序遍历 2 4 3 1
        //添加节点之后 :2 5 4 3 1
        System.out.println("后序遍历");
        binaryTree.postOrder();

        //前序遍历查找
        System.out.println("前序遍历查找:");
        System.out.println(binaryTree.preOrderSearch(5));
//        System.out.println("中序遍历查找:");
//        System.out.println(binaryTree.infixOrderSearch(5));
//        System.out.println("后序遍历查找:");
//        System.out.println(binaryTree.postOrderSearch(5));

    }

}

class BinaryTree {
    private Node root;

    public void setRoot(Node 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("当前二叉树为空,无法遍历!");
        }
    }

    public Node preOrderSearch(int num) {
        if (root != null) {
            return root.preOrderSearch(num);
        }
        return null;
    }

    public Node infixOrderSearch(int num) {
        if (root != null) {
            return root.infixOrderSearch(num);
        }
        return null;
    }

    public Node postOrderSearch(int num) {
        if (root != null) {
            return root.postOrderSearch(num);
        }
        return null;
    }
}

class Node {
    private int num;
    private Node left;
    private Node right;

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

    public int getNum() {
        return num;
    }

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

    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;
    }

    @Override
    public String toString() {
        return "Node{" +
                "num=" + num +
                '}';
    }

    //前序遍历
    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.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

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

//    前序遍历查找指定的节点
    public Node preOrderSearch(int num) {
        System.out.println("当前的节点数值为:" + this.num);
        Node res = null;
        if (this.num == num) {
            return this;
        }
        if (this.left != null) {
            res = this.left.preOrderSearch(num);
        }
        if (res != null) {
            return res;
        }
        if (this.right != null) {
            res = this.right.preOrderSearch(num);
        }
        return res;
    }
    //中序遍历查找指定的节点
    public Node infixOrderSearch(int num) {

        Node res = null;
        if (this.left != null) {
            res = this.left.infixOrderSearch(num);
        }
        if (res != null) {
            return res;
        }
        System.out.println("当前的节点数值为:" + this.num);
        if (this.num == num) {
            return this;
        }
        if (this.right != null) {
            res = this.right.infixOrderSearch(num);
        }
        return res;
    }

    //后序遍历查找指定的节点
    public Node postOrderSearch(int num) {
        Node res = null;
        if (this.left != null) {
            res = this.left.postOrderSearch(num);
        }
        if (res != null) {
            return res;
        }
        if (this.right != null) {
            res = this.right.postOrderSearch(num);
        }
        if (res != null) {
            return res;
        }
        System.out.println("当前的节点数值为:" + this.num);
        if (this.num == num) {
            return this;
        }
        return res;
    }
}

运行部分结果:

前序遍历查找:
当前的节点数值为:1
当前的节点数值为:2
当前的节点数值为:3
当前的节点数值为:5
Node{num=5}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
1. 什么是二叉树二叉树是一种形结构,其每个节点最多有两个子节点一个节点的左子节点比该节点小,右子节点比该节点大。二叉树通常用于搜索和排序。 2. 二叉树的遍历方法有哪些? 二叉树的遍历方法包括前序遍历、序遍历和后序遍历。前序遍历是从根节点开始遍历,先访问根节点,再访问左子,最后访问右子序遍历是从根节点开始遍历,先访问左子,再访问根节点,最后访问右子。后序遍历是从根节点开始遍历,先访问左子,再访问右子,最后访问根节点。 3. 二叉树查找方法有哪些? 二叉树查找方法包括递归查找和非递归查找。递归查找是从根节点开始查找,如果当前节点的值等于要查找的值,则返回当前节点。如果要查找的值比当前节点小,则继续在左子查找;如果要查找的值比当前节点大,则继续在右子查找。非递归查找可以使用栈或队列实现,从根节点开始,每次将当前节点的左右子节点入栈/队列,直到找到要查找的值或者栈/队列为空。 4. 二叉树的插入与删除操作如何实现? 二叉树的插入操作是将要插入的节点与当前节点的值进行比较,如果小于当前节点的值,则继续在左子插入;如果大于当前节点的值,则继续在右子插入。当找到一个节点时,就将要插入的节点作为该空节点的子节点。删除操作需要分为三种情况:删除叶子节点、删除只有一个节点节点和删除有两个子节点节点。删除叶子节点很简单,只需要将其父节点的对应子节点置为空即可。删除只有一个节点节点,需要将其子节点替换为该节点的位置。删除有两个子节点节点,则可以找到该节点的后继节点(即右子最小的节点),将其替换为该节点,然后删除后继节点。 5. 什么是平衡二叉树? 平衡二叉树是一种特殊的二叉树,它保证左右子的高度差不超过1。这种平衡可以确保二叉树查找、插入和删除操作的时间复杂度都是O(logn)。常见的平衡二叉树包括红黑和AVL

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值