二叉树 遍历 查找指定节点

数据结构与方法:


public class MyBinaryTree<E> {

    //根节点
    private Node root;

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

    //树的前序遍历
    public void preOrder() throws RuntimeException {
        if(this.root!=null){
            this.root.preOrder();
        }else {
           throw new RuntimeException("root node is null");
        }
    }

    //树的中序遍历
    public void infixOrder() throws RuntimeException {
        if(this.root!=null){
            this.root.infixOrder();
        }else {
            throw new RuntimeException("root node is null");
        }
    }

    //树的后序遍历
    public void postOrder() throws RuntimeException {
        if(this.root!=null){
            this.root.postOrder();
        }else {
            throw new RuntimeException("root node is null");
        }
    }

    //前序遍历查找
    public Node preOrderSearch(E e){
        if(root != null){
            return this.root.preOrderSearch(e);
        }else {
            return null;
        }
    }
    //中序遍历查找
    public Node infixOrderSearch(E e){
        if(root != null){
            return this.root.infixOrderSearch(e);
        }else {
            return null;
        }
    }
    //后序遍历查找
    public Node postOrderSearch(E e){
        if(root != null){
            return this.root.postOrderSearch(e);
        }else {
            return null;
        }
    }

    /**
     * 叶子节点结构  E为对象泛型
     * @param <E>
     */
    public static class Node<E> {
        E item;
        Node<E> left;
        Node<E> right;

        public Node(E item) {
            this.item = item;
        }

        public Node(E item, Node<E> left, Node<E> right) {
            this.item = item;
            this.left = left;
            this.right = right;
        }

        public Node() {

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

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

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

        /**
         * 前序遍历节点
         */
        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(E e){
            System.out.println("进入前序查找");
            //比较当前节点是不是
            if(this.item.equals(e)){
                return this;
            }
            Node node = null;
            //判断左子节点是否为空 进行查找
            if(this.left != null ){
                node = this.left.preOrderSearch(e);
            }
            //如果找到了  就赋值返回
            if(node != null){
                return node;
            }
            //判断右子节点是否为空 查找
            if(this.right != null){
                node = this.right.preOrderSearch(e);
            }
            return node;
        }

        //中序遍历查找
        public Node infixOrderSearch(E e){
            Node node = null;
            //判断左子节点是否为空 进行查找
            if(this.left != null ){
                node = this.left.infixOrderSearch(e);
            }
            //如果找到了  就赋值返回
            if(node != null){
                return node;
            }
            System.out.println("进入中序查找");
            //比较当前节点是不是
            if(this.item.equals(e)){
                return this;
            }
            //判断右子节点是否为空 查找
            if(this.right != null){
                node = this.right.infixOrderSearch(e);
            }
            return node;
        }

        //后序遍历查找
        public Node postOrderSearch(E e){
            Node node = null;
            //判断左子节点是否为空 进行查找
            if(this.left != null ){
                node = this.left.postOrderSearch(e);
            }
            //如果找到了  就赋值返回
            if(node != null){
                return node;
            }
            //判断右子节点是否为空 查找
            if(this.right != null){
                node = this.right.postOrderSearch(e);
            }
            //如果找到了  就赋值返回
            if(node != null){
                return node;
            }
            System.out.println("进入后序查找");
            //比较当前节点是不是
            if(this.item.equals(e)){
                node = this;
            }
            return node;
        }
    }
}

测试方法、数据:


    //测试  根据值查找树节点
    @Test
    public void test02(){
        MyBinaryTree.Node node1 = new MyBinaryTree.Node<Integer>(1);
        MyBinaryTree.Node node2 = new MyBinaryTree.Node<Integer>(2);
        MyBinaryTree.Node node3 = new MyBinaryTree.Node<Integer>(3);
        MyBinaryTree.Node node4 = new MyBinaryTree.Node<Integer>(4);
        MyBinaryTree.Node node5 = new MyBinaryTree.Node<Integer>(5);
        node1.setLeft(node2);
        node1.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);
        MyBinaryTree tree = new MyBinaryTree();
        tree.setRoot(node1);
//        MyBinaryTree.Node node = tree.preOrderSearch(5);  //前序查找  查找4次
//        MyBinaryTree.Node node = tree.infixOrderSearch(5);  //中序查找  查找3次
        MyBinaryTree.Node node = tree.postOrderSearch(5);  //前序查找  查找2次
        if(node!=null){
            System.out.println(node);
        }else{
            System.out.println("没有找到");
        }

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值