java二叉树前中后序遍历,查询

前中后序遍历时根绝根节点打印的顺序定义的

代码:

package com.wangyq.datastructrue.tree;

public class BinaryTree {
    public static void main(String[] args) {
        //定义6个节点
        Node node1 = new Node(1, "小李");
        Node node2 = new Node(2, "小赵");
        Node node3 = new Node(3, "小钱");
        Node node4 = new Node(4, "小孙");
        Node node5 = new Node(5, "小周");
        Node node6 = new Node(6, "小王");
        //先手动构建树
        node1.setLeftNode(node2);
        node1.setRightNode(node3);
        node3.setLeftNode(node4);
        node3.setRightNode(node5);
        node5.setRightNode(node6);

        Tree tree = new Tree();
        tree.setRoot(node1);
        //前序遍历
        System.out.println("前序遍历");
        tree.frontTraversal();
        //中序遍历
        System.out.println("中序遍历");
        tree.mediumTraversal();
        //后序遍历
        System.out.println("后序遍历");
        tree.queeTraversal();


        Node temp;
        //前序查询
        System.out.println("前序查询");
        temp = tree.frontSearch(3);
        System.out.println(temp != null ? temp.toString() : "没有查到");
        //中序查询
        System.out.println("中序查询");
        temp = tree.mediumSearch(3);
        System.out.println(temp != null ? temp.toString() : "没有查到");
        //后序查询
        System.out.println("后序查询");
        temp = tree.queeSearch(3);
        System.out.println(temp != null ? temp.toString() : "没有查到");


    }
}


class Tree {
    private Node root;

    public Node getRoot() {
        return root;
    }

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

    /**
     * 前序遍历
     */
    public void frontTraversal() {
        if (null == root) {
            System.out.println("这是空树");
            return;
        }
        root.frontTraversal();
    }

    /**
     * 中序遍历
     */
    public void mediumTraversal() {
        if (null == root) {
            System.out.println("这是空树");
            return;
        }
        root.mediumTraversal();
    }

    /**
     * 后序遍历
     */
    public void queeTraversal() {
        if (null == root) {
            System.out.println("这是空树");
            return;
        }
        root.queeTraversal();
    }

    public Node frontSearch(int i) {
        if (null == root) {
            System.out.println("这是空树");
            return null;
        }
        return root.frontSearch(i);
    }

    /**
     * 中序查询
     *
     * @param i
     * @return
     */
    public Node mediumSearch(int i) {
        if (null == root) {
            System.out.println("这是空树");
            return null;
        }
        return root.mediumSearch(i);
    }

    /**
     * 后序查询
     *
     * @param i
     * @return
     */
    public Node queeSearch(int i) {
        if (null == root) {
            System.out.println("这是空树");
            return null;
        }
        return root.queeSearch(i);
    }
}

class Node {
    private int id;
    private String name;
    private Node leftNode;
    private Node rightNode;

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

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Node getLeftNode() {
        return leftNode;
    }

    public void setLeftNode(Node leftNode) {
        this.leftNode = leftNode;
    }

    public Node getRightNode() {
        return rightNode;
    }

    public void setRightNode(Node rightNode) {
        this.rightNode = rightNode;
    }

    /**
     * 前序遍历
     */
    public void frontTraversal() {
        System.out.println(this.toString());
        if (null != leftNode) {
            leftNode.frontTraversal();
        }
        if (null != rightNode) {
            rightNode.frontTraversal();
        }

    }

    /**
     * 中序遍历
     */
    public void mediumTraversal() {
        if (null != leftNode) {
            leftNode.mediumTraversal();
        }
        System.out.println(this.toString());
        if (null != rightNode) {
            rightNode.mediumTraversal();
        }
    }

    /**
     * 后序遍历
     */
    public void queeTraversal() {
        if (null != leftNode) {
            leftNode.queeTraversal();
        }
        if (null != rightNode) {
            rightNode.queeTraversal();
        }
        System.out.println(this.toString());
    }

    /**
     * 前序查询
     */
    public Node frontSearch(int i) {
        System.out.println("前序查询");
        if (id == i) {
            return this;
        }
        Node temp = null;
        if (null != leftNode) {
            temp = leftNode.frontSearch(i);
        }
        if (null != temp) {
            return temp;
        }
        if (null != rightNode) {
            temp = rightNode.frontSearch(i);
        }
        return temp;

    }

    /**
     * 中序查找
     *
     * @param i
     * @return
     */
    public Node mediumSearch(int i) {
        Node temp = null;
        if (null != leftNode) {
            temp = leftNode.mediumSearch(i);
        }
        if (null != temp) {
            return temp;
        }
        System.out.println("中序查找");
        if (id == i) {
            return this;
        }
        if (null != rightNode) {
            temp = rightNode.mediumSearch(i);
        }
        return temp;
    }

    /**
     * 后续查找
     *
     * @param i
     * @return
     */
    public Node queeSearch(int i) {
        Node temp = null;
        if (null != leftNode) {
            temp = leftNode.queeSearch(i);
        }
        if (null != temp) {
            return temp;
        }
        if (null != rightNode) {
            temp = rightNode.queeSearch(i);
        }
        if (null != temp) {
            return temp;
        }
        System.out.println("后续查找");
        if (id == i) {
            return this;
        }
        return temp;
    }
}

运行结果:
前序遍历
Node{id=1, name=‘小李’}
Node{id=2, name=‘小赵’}
Node{id=3, name=‘小钱’}
Node{id=4, name=‘小孙’}
Node{id=5, name=‘小周’}
Node{id=6, name=‘小王’}
中序遍历
Node{id=2, name=‘小赵’}
Node{id=1, name=‘小李’}
Node{id=4, name=‘小孙’}
Node{id=3, name=‘小钱’}
Node{id=5, name=‘小周’}
Node{id=6, name=‘小王’}
后序遍历
Node{id=2, name=‘小赵’}
Node{id=4, name=‘小孙’}
Node{id=6, name=‘小王’}
Node{id=5, name=‘小周’}
Node{id=3, name=‘小钱’}
Node{id=1, name=‘小李’}
前序查询
前序查询中
前序查询中
前序查询中
Node{id=3, name=‘小钱’}
中序查询
中序查找中
中序查找中
中序查找中
中序查找中
Node{id=3, name=‘小钱’}
后序查询
后序查找中
后序查找中
后序查找中
后序查找中
后序查找中
Node{id=3, name=‘小钱’}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值