二叉树的创建及前中后序遍历,前中后序查找(JAVA递归实现)

一、二叉树的创建:树是依据节点存在的,需要节点类。

class Node{
    private int no;
    private String name;
    private Node left;//指向左节点
    private Node right;//指向右节点
    public Node(int no,String name){
        this.no = no;
        this.name = name;
    }
    
    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

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

    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{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}
class BinaryTree{
//二叉树,创建根结点即可。
    private Node root;
    public void setRoot(Node root){
        this.root = root;
    }
}

二、前中后序遍历

代码如下:注意所有代码在节点类中写,在二叉树类中调用方法。

  1. 前序遍历:访问顺序:先根节点,再左子树,最后右子树;
    public void PreOrder(){
            //前序遍历
            //先打印当前节点
            System.out.println(this);
            //左边不为空,递归
            if (this.left != null){
                this.left.PreOrder();
            }
            //右边不为空,递归
            if (this.right != null){
                this.right.PreOrder();
            }
        }
    
  2. 中序遍历:访问顺序:先左子树,再根节点,最后右子树;
    public void MidOrder(){
            if (this.left != null){
                this.left.MidOrder();
            }
            System.out.println(this);
            if (this.right != null){
                this.right.MidOrder();
            }
        }
  3. 后序遍历:访问顺序:先左子树,再根节点,最后右子树;
     public void BackOrder(){
            if (this.left != null) {
                this.left.BackOrder();
            }
            if (this.right != null){
                this.right.BackOrder();
            }
            System.out.println(this);
        }

三,前中后序查找:

        1.前序查找:

public Node PreCheck(int no){
        //前序查找
        //判断当前结点是否等于要查找的值的no
        if (this.no == no){
            return this;
        }
        //创建临时变量存储
        Node res = null;
        if (this.left != null){
            //左节点不为空,递归查找,接收返回值
            res = this.left.PreCheck(no);
        }
        if (res != null){
            //有返回值则说明找到值,直接返回
            return res;
        }
        if (this.right != null){
            //右节点不为空,递归查找,接收返回值
            res = this.right.PreCheck(no);
        }
        if (res != null){
            //有返回值则说明找到值,直接返回
            return res;
        }
        //最后前面都不执行,返回默认的结果null
        return res;
    }

        2.中序查找:

public Node infixOrderSerach(int no){
        Node res = null;
        if (this.left != null){
            res = this.left.infixOrderSerach(no);
        }
        if (res != null){
            return res;
        }
        if (this.no == no){
            return this;
        }
        if (this.right != null){
            res = this.right.infixOrderSerach(no);
        }
        if (res != null){
            return res;
        }
        return res;
    }

        3.后序查找:

public Node postOrderSearch(int no){
        Node res = null;
        if (this.left != null){
            res = this.left.postOrderSearch(no);
        }
        if (res != null){
            return res;
        }
        
        if (this.right != null){
            res = this.right.postOrderSearch(no);
        }
        if (res != null){
            return res;
        }
        if (this.no == no){
            return this;
        }
        
        return res;
    }


总结

所有代码及测试:

public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        Node root =  new Node(1,"zhangsan");
        Node node2 =  new Node(3,"wangwu");
        Node node1 =  new Node(2,"lisi");
        Node node3 =  new Node(4,"sunliu");
        Node node4 =  new Node(5,"zhouqi");
        root.setRight(node2);
        root.setLeft(node1);
        node2.setRight(node3);
        node3.setRight(node4);

        System.out.println("前序遍历");
        binaryTree.setRoot(root);
        binaryTree.preOrder();
        System.out.println("中序遍历");
        binaryTree.midOrder();
        System.out.println("后序遍历");
        binaryTree.backOrder();
        System.out.println("=======================");

        System.out.println(binaryTree.preSearch(2));
        System.out.println(binaryTree.infixSearch(2));
        System.out.println(binaryTree.postSearch(2));
        
    }

}

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("二叉树为空,无法遍历");
            return;
        }

    }

    public Node preSearch(int no){
        Node node = null;
        if (this.root != null){
            node = this.root.PreCheck(no);
        }else {
            System.out.println("二叉树为空,无法遍历");
            return null;
        }
        return node;
    }

    public void midOrder(){
        if (this.root != null){
            this.root.MidOrder();
        }else {
            System.out.println("二叉树为空,无法遍历");
            return;
        }
    }
    
    public Node infixSearch(int no){
        Node node = null;
        if (this.root != null){
            node = this.root.infixOrderSerach(no);
        }else {
            System.out.println("二叉树为空,无法遍历");
            return null;
        }
        return node;
    }

    public void backOrder(){
        if (this.root != null){
            this.root.BackOrder();
        }else {
            System.out.println("二叉树为空,无法遍历");
            return;
        }
    }
    
    public Node postSearch(int no){
        Node node = null;
        if (this.root != null){
            node = this.root.postOrderSearch(no);
        }else {
            System.out.println("二叉树为空,无法遍历");
            return null;
        }
        return node;
    }


}

class Node{
    private int no;
    private String name;
    private Node left;//指向左节点
    private Node right;//指向右节点

    public void PreOrder(){
        //前序遍历
        //先打印当前节点
        System.out.println(this);
        //左边不为空,递归
        if (this.left != null){
            this.left.PreOrder();
        }
        //右边不为空,递归
        if (this.right != null){
            this.right.PreOrder();
        }
    }

    public Node PreCheck(int no){
        //前序查找
        //判断当前结点是否等于要查找的值的no
        if (this.no == no){
            return this;
        }
        //创建临时变量存储
        Node res = null;
        if (this.left != null){
            //左节点不为空,递归查找,接收返回值
            res = this.left.PreCheck(no);
        }
        if (res != null){
            //有返回值则说明找到值,直接返回
            return res;
        }
        if (this.right != null){
            //右节点不为空,递归查找,接收返回值
            res = this.right.PreCheck(no);
        }
        if (res != null){
            //有返回值则说明找到值,直接返回
            return res;
        }
        //最后前面都不执行,返回默认的结果null
        return res;
    }



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

    public Node infixOrderSerach(int no){
        Node res = null;
        if (this.left != null){
            res = this.left.infixOrderSerach(no);
        }
        if (res != null){
            return res;
        }
        if (this.no == no){
            return this;
        }
        if (this.right != null){
            res = this.left.infixOrderSerach(no);
        }
        if (res != null){
            return res;
        }
        return res;
    }


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

    public Node postOrderSearch(int no){
        Node res = null;
        if (this.left != null){
            res = this.left.postOrderSearch(no);
        }
        if (res != null){
            return res;
        }

        if (this.right != null){
            res = this.right.postOrderSearch(no);
        }
        if (res != null){
            return res;
        }
        if (this.no == no){
            return this;
        }

        return res;
    }

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

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

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

    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{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值