Java数据结构——前序中序后序查找

根据前序、中序、后序遍历方法,我们可以通过这种方式来查找节点。查找方法与遍历类似。

前序查找先查找根节点,再查找左子节点与右子节点;中序查找先查找左子节点、再查找根节点与右子节点;后序查找先查找左子节点与右子节点,再查找根节点。

public class Main {
    public static void main(String[] args) {
        Node node1 = new Node(1, "Amy");
        Node node2 = new Node(2, "Bob");
        Node node3 = new Node(3, "Candy");
        Node node4 = new Node(4, "Doff");
        Node node5 = new Node(5, "Emma");
        BinaryTree tree = new BinaryTree();
        tree.setRoot(node1);
        node1.setLeft(node2);
        node1.setRight(node3);
        node3.setLeft(node5);
        node3.setRight(node4);
        Node resNode = null;
        System.out.println("前序查找第5个节点");
        resNode = tree.preOrderSearch(5);
        if (resNode != null) {
            System.out.println(resNode);
        } else {
            System.out.println("查找不到该节点");
        }
        System.out.println("中序查找第5个节点");
        resNode = tree.infixOrderSearch(5);
        if (resNode != null) {
            System.out.println(resNode);
        } else {
            System.out.println("查找不到该节点");
        }
        System.out.println("后序查找第5个节点");
        resNode = tree.postOrderSearch(5);
        if (resNode != null) {
            System.out.println(resNode);
        } else {
            System.out.println("查找不到该节点");
        }
    }
}

//节点
class Node {
    private int id;
    private String name;
    Node left;
    Node right;

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

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

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

    @Override
    public String toString() {
        return "Node{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    //前序查找
    public Node preOrderSearch(int id) {
        System.out.println("前序进行比较");
        if (this.id == id) {
            return this;
        }
        Node node = null;
        if (this.left != null) {
            node = this.left.preOrderSearch(id);
        }
        if (node != null) {
            return node;
        }
        if (this.right != null) {
            node = this.right.preOrderSearch(id);
        }
        return node;
    }

    //中序查找
    public Node infixOrderSearch(int id) {
        Node node = null;
        if (this.left != null) {
            node = this.left.infixOrderSearch(id);
        }
        if (node != null) {
            return node;
        }
        System.out.println("中序进行比较");
        if (this.id == id) {
            return this;
        }
        if (this.right != null) {
            node = this.right.infixOrderSearch(id);
        }
        return node;
    }

    //后序查找
    public Node postOrderSearch(int id) {
        Node node = null;
        if (this.left != null) {
            node = this.left.postOrderSearch(id);
        }
        if (node != null) {
            return node;
        }
        if (this.right != null) {
            node = this.right.postOrderSearch(id);
        }
        if (node != null) {
            return node;
        }
        System.out.println("后序进行比较");
        if (this.id == id) {
            return this;
        }
        return node;
    }

}

//二叉树
class BinaryTree {
    private Node root;

    public void setRoot(Node root) {
        this.root = root;
    }

    //前序查找
    public Node preOrderSearch(int id) {
        if (this.root != null) {
            return this.root.preOrderSearch(id);
        } else {
            return null;
        }
    }

    //中序查找
    public Node infixOrderSearch(int id) {
        if (this.root != null) {
            return this.root.infixOrderSearch(id);
        } else {
            return null;
        }
    }

    //后序查找
    public Node postOrderSearch(int id) {
        if (this.root != null) {
            return this.root.postOrderSearch(id);
        } else {
            return null;
        }
    }

}
前序查找第5个节点
前序进行比较
前序进行比较
前序进行比较
前序进行比较
Node{id=5, name='Emma'}
中序查找第5个节点
中序进行比较
中序进行比较
中序进行比较
Node{id=5, name='Emma'}
后序查找第5个节点
后序进行比较
后序进行比较
Node{id=5, name='Emma'}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值