Java数据结构与算法 树结构基础

本文探讨了二叉树作为数据结构的优势,包括在搜索、插入和删除操作中的高效性,并深入讲解了线索化二叉树的原理和其在中序遍历中的应用。通过实例展示了如何在顺序存储和线索化中实现前序、中序和后序遍历,以及如何删除特定节点。
摘要由CSDN通过智能技术生成

二叉树

为什么需要树这种数据结构?

数组存储方式的分析

  • 优点:
    通过下标方式访问元素,速度快。对于有序数组,还可使用二分查找提高检索速度
  • 缺点:
    如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低

在这里插入图片描述


链式存储方式的分析

  • 优点:
    在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,链接到链表中即可, 删除效率也很好)。
  • 缺点:
    在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历)

在这里插入图片描述


树存储方式的分析
能提高数据存储,读取的效率, 比如利用 二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度。

在这里插入图片描述

二叉树

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二叉树遍历
  • 前序遍历: 先输出父节点,再遍历左子树和右子树
  • 中序遍历: 先遍历左子树,再输出父节点,再遍历右子树
  • 后序遍历: 先遍历左子树,再遍历右子树,最后输出父节点


在这里插入图片描述
在这里插入图片描述
代码

package tree;
/*
 * @Author: Min
 * @Date:   2021/10/30
 * @description
 */

public class BinaryTreeDemo {

    public static void main(String[] args) {
        //先需要创建一棵二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建需要的结点
        HeroNode root = new HeroNode(1,"宋江");
        HeroNode node2 = new HeroNode(2,"吴用");
        HeroNode node3 = new HeroNode(3,"卢俊义");
        HeroNode node4 = new HeroNode(4,"林冲");
        HeroNode node5 = new HeroNode(5,"关羽");

        //说明:我们先手动创建二叉树
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);//新加一个节点5测试

        //测试
        binaryTree.setRoot(root);//设置根节点
        System.out.println("前序遍历");//1 2 3 4->12354
        binaryTree.preOrder();
        System.out.println("中序遍历");//2 1 3 4->21534
        binaryTree.infixOrder();
        System.out.println("后序遍历");//4 3 1 2->43512
        binaryTree.postOrder();

    }
}

//定义BinaryTree二叉树
class BinaryTree {
    private HeroNode root;

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

//先创建HeroNode结点
class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(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 HeroNode getLeft() {
        return left;
    }

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

    public HeroNode getRight() {
        return right;
    }

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

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name=" + name +'}';
    }

    //编写前序遍历的方法
    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.right != null) {
            this.right.postOrder();
        }
        //输出父节点
        System.out.println(this);
        //递归向左子树中序遍历
        if (this.left != null) {
            this.left.postOrder();
        }
    }
}


二叉树查找指定的节点

要求

  • 请编写前序查找,中序查找和后序查找的方法。
  • 并分别使用三种查找方式,查找 heroNO = 5 的节点
  • 并分析各种查找方式,分别比较了多少次

在这里插入图片描述

代码

package tree;
/*
 * @Author: Min
 * @Date:   2021/10/30
 * @description
 */

public class BinaryTreeDemo {

    public static void main(String[] args) {
        //先需要创建一棵二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建需要的结点
        HeroNode root = new HeroNode(1,"宋江");
        HeroNode node2 = new HeroNode(2,"吴用");
        HeroNode node3 = new HeroNode(3,"卢俊义");
        HeroNode node4 = new HeroNode(4,"林冲");
        HeroNode node5 = new HeroNode(5,"关羽");

        //说明:我们先手动创建二叉树
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);//新加一个节点5测试
        binaryTree.setRoot(root);//设置根节点

        //测试——遍历
//        System.out.println("前序遍历");//1 2 3 4->12354
//        binaryTree.preOrder();
//        System.out.println("中序遍历");//2 1 3 4->21534
//        binaryTree.infixOrder();
//        System.out.println("后序遍历");//4 3 1 2->43512
//        binaryTree.postOrder();

        //测试——查找
        System.out.println("前序查找");
        HeroNode resNode = binaryTree.preOrderSearch(15);
        if (resNode != null) {
            System.out.printf("找到了,no=%d name=%s",resNode.getNo(),resNode.getName());
        } else {
            System.out.println("没有找到 ");
        }
//        System.out.println("中序查找");
//        HeroNode resNode = binaryTree.infixOrderSearch(5);
//        if (resNode != null) {
//            System.out.printf("找到了,no=%d name=%s",resNode.getNo(),resNode.getName());
//        } else {
//            System.out.println("没有找到 ");
//        }
//        System.out.println("后序查找");
//        HeroNode resNode = binaryTree.postOrderSearch(5);
//        if (resNode != null) {
//            System.out.printf("找到了,no=%d name=%s",resNode.getNo(),resNode.getName());
//        } else {
//            System.out.println("没有找到 ");
//        }


    }
}

//定义BinaryTree二叉树
class BinaryTree {
    private HeroNode root;

    public void setRoot(HeroNode 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 HeroNode preOrderSearch(int no) {
        if (this.root != null) {
            return root.preOrderSearch(no);
        }else {
            return null;
        }
    }
    //中序查找
    public HeroNode infixOrderSearch(int no) {
        if (this.root != null) {
            return root.infixOrderSearch(no);
        }else {
            return null;
        }
    }
    //后序查找
    public HeroNode postOrderSearch(int no) {
        if (this.root != null) {
            return root.postOrderSearch(no);
        }else {
            return null;
        }
    }
}

//先创建HeroNode结点
class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(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 HeroNode getLeft() {
        return left;
    }

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

    public HeroNode getRight() {
        return right;
    }

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

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name=" + name +'}';
    }

    //编写前序遍历的方法
    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);
    }

    /**
     *
     * @param no 查找no
     * @return 如果找到返回该node,否则返回null
     */
    //前序查找
    public HeroNode preOrderSearch(int no) {
        //比较当前节点是不是
//        System.out.println("进入前序遍历");//查看次数
        if (this.no == no) {
            return this;
        }
        //1.否则判断当前节点的左节点是否为空,若不,则递归前序查找
        //2.如果左递归前序查找,找到节点,则返回
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        //1.左递归前序查找,找到节点,则返回,否则继续
        //2.当前的节点的右子节点是否空,若不,则继续向右递归前序查找
        if (this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
            return resNode;
    }
    //中序查找
    public HeroNode infixOrderSearch(int no) {
        //判断当前节点的左子节点是否为空,若不为空,则递归中序查找
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.infixOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
//        System.out.println("中序查找");//计算次数
        //如果找到,则返回,如果没有找到,就和当前节点比较,如果是返回当前节点
        if (this.no == no) {
            return this;
        }
        //否则继续进行右递归的中序查找
        if (this.right != null) {
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }
    //后序查找
    public HeroNode postOrderSearch(int no) {
        //判断当前节点的左子节点是否为空,如果不,则递归后序查找
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.postOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        //如果左子树没有找到,则向右子树进行后续遍历查找
        if (this.right != null) {
            resNode = this.right.postOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        //如果左右子树都没有找到,就比较当前节点是不是
        if (this.no == no) {
            return this;
        }
        return resNode;
    }

}
二叉树删除节点

要求

  • 如果删除的节点是叶子节点,则删除该节点
  • 如果删除的节点是非叶子节点,则删除该子树.
  • 测试,删除掉 5号叶子节点 和 3号子树.

在这里插入图片描述

在这里插入图片描述

代码

package tree;
/*
 * @Author: Min
 * @Date:   2021/10/30
 * @description
 */

public class BinaryTreeDemo {

    public static void main(String[] args) {
        //先需要创建一棵二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建需要的结点
        HeroNode root = new HeroNode(1,"宋江");
        HeroNode node2 = new HeroNode(2,"吴用");
        HeroNode node3 = new HeroNode(3,"卢俊义");
        HeroNode node4 = new HeroNode(4,"林冲");
        HeroNode node5 = new HeroNode(5,"关羽");

        //说明:我们先手动创建二叉树
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);//新加一个节点5测试
        binaryTree.setRoot(root);//设置根节点

        //测试——遍历
//        System.out.println("前序遍历");//1 2 3 4->12354
//        binaryTree.preOrder();
//        System.out.println("中序遍历");//2 1 3 4->21534
//        binaryTree.infixOrder();
//        System.out.println("后序遍历");//4 3 1 2->43512
//        binaryTree.postOrder();

        //测试——查找
//        System.out.println("前序查找");
//        HeroNode resNode = binaryTree.preOrderSearch(15);
//        if (resNode != null) {
//            System.out.printf("找到了,no=%d name=%s",resNode.getNo(),resNode.getName());
//        } else {
//            System.out.println("没有找到 ");
//        }
//        System.out.println("中序查找");
//        HeroNode resNode = binaryTree.infixOrderSearch(5);
//        if (resNode != null) {
//            System.out.printf("找到了,no=%d name=%s",resNode.getNo(),resNode.getName());
//        } else {
//            System.out.println("没有找到 ");
//        }
//        System.out.println("后序查找");
//        HeroNode resNode = binaryTree.postOrderSearch(5);
//        if (resNode != null) {
//            System.out.printf("找到了,no=%d name=%s",resNode.getNo(),resNode.getName());
//        } else {
//            System.out.println("没有找到 ");
//        }

        //测试 —— 删除
        //删除叶子节点
//        System.out.println("删除前(前序):");//12354
//        binaryTree.preOrder();
//        binaryTree.delNode(5);
//        System.out.println("删除5后(前序):");//1234
//        binaryTree.preOrder();
        //删除非叶子节点
        System.out.println("删除前(前序):");//12354
        binaryTree.preOrder();
        binaryTree.delNode(3);
        System.out.println("删除3后(前序):");//1234
        binaryTree.preOrder();
        
        
    }
}

//定义BinaryTree二叉树
class BinaryTree {
    private HeroNode root;

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

    //删除节点
    public void delNode(int no) {
        if (root != null) {
            //如果只有一个root节点,这里立即判断root是不是要删除的节点
            if (root.getNo() == no) {
                root = null;
            } else {
                //递归删除
                root.delNode(no);
            }
        } else {
            System.out.println("空树,不能删除");
        }
    }

    //前序遍历
    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 HeroNode preOrderSearch(int no) {
        if (this.root != null) {
            return root.preOrderSearch(no);
        }else {
            return null;
        }
    }
    //中序查找
    public HeroNode infixOrderSearch(int no) {
        if (this.root != null) {
            return root.infixOrderSearch(no);
        }else {
            return null;
        }
    }
    //后序查找
    public HeroNode postOrderSearch(int no) {
        if (this.root != null) {
            return root.postOrderSearch(no);
        }else {
            return null;
        }
    }
}

//先创建HeroNode结点
class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(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 HeroNode getLeft() {
        return left;
    }

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

    public HeroNode getRight() {
        return right;
    }

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

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name=" + name +'}';
    }

    //递归删除节点
    //1.如果删除的节点是叶子点,则删除该节点
    //2.如果删除的节点是非叶子点,则删除该子树
    public void delNode(int no) {
        /*思路:
        1.因为我们的二叉制是单向的,所以我们是判断当前结点的子结点是否需要删除结点,而不能去判断当前这个结点是不是需要删除结点。
        2.如果当前结点的左子结点不为空,并且左子结点就是要删除结点,就将this.left = null;并且就返回(结束递归删除)
        3.如果当前结点的右子结点不为空,并且右子结点就是要删除结点,就将this.right= null ;并且就返回(结束递归删除)
        4.如果第2和第3步没有删除结点,那么我们就需要向左子树进行递归删除
        5.如果第4步也没有删除结点,则应当向右子树进行递归删除。
        */
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        if (this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }
        if (this.left != null) {
            this.left.delNode(no);
        }
        if (this.right != null) {
            this.right.delNode(no);
        }
    }

    //编写前序遍历的方法
    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);
    }

    /**
     *
     * @param no 查找no
     * @return 如果找到返回该node,否则返回null
     */
    //前序查找
    public HeroNode preOrderSearch(int no) {
        //比较当前节点是不是
//        System.out.println("进入前序遍历");//查看次数
        if (this.no == no) {
            return this;
        }
        //1.否则判断当前节点的左节点是否为空,若不,则递归前序查找
        //2.如果左递归前序查找,找到节点,则返回
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        //1.左递归前序查找,找到节点,则返回,否则继续
        //2.当前的节点的右子节点是否空,若不,则继续向右递归前序查找
        if (this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
            return resNode;
    }
    //中序查找
    public HeroNode infixOrderSearch(int no) {
        //判断当前节点的左子节点是否为空,若不为空,则递归中序查找
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.infixOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
//        System.out.println("中序查找");//计算次数
        //如果找到,则返回,如果没有找到,就和当前节点比较,如果是返回当前节点
        if (this.no == no) {
            return this;
        }
        //否则继续进行右递归的中序查找
        if (this.right != null) {
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }
    //后序查找
    public HeroNode postOrderSearch(int no) {
        //判断当前节点的左子节点是否为空,如果不,则递归后序查找
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.postOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        //如果左子树没有找到,则向右子树进行后续遍历查找
        if (this.right != null) {
            resNode = this.right.postOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        //如果左右子树都没有找到,就比较当前节点是不是
        if (this.no == no) {
            return this;
        }
        return resNode;
    }

}

顺序存储二叉树

概念

  • 基本说明
    从数据存储来看,数组存储方式和树
    的存储方式可以相互转换,即数组可
    以转换成树,树也可以转换成数组,
    看右面的示意图。

  • 要求:
    下图的二叉树的结点,要求以数组的方式来存放 arr : [1, 2, 3, 4, 5, 6, 7]
    要求在遍历数组 arr时,仍然可以以前序遍历,中序遍历和后序遍历的方式完成结点的遍历

在这里插入图片描述

特点:

  1. 顺序二叉树通常只考虑完全二叉树
  2. 第n个元素的左子节点为 2 * n + 1
  3. 第n个元素的右子节点为 2 * n + 2
  4. 第n个元素的父节点为 (n-1) / 2
  5. n : 表示二叉树中的第几个元素(按0开始编号如图所示)

需求: 给你一个数组 {1,2,3,4,5,6,7},要求以二叉树前序遍历的方式进行遍历。 前序遍历的结果应当为 1,2,4,5,3,6,7

代码

package tree;
/*
 * @Author: Min
 * @Date:   2021/11/2
 * @description
 */

public class ArrBinaryTreeDemo {

    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7};
        //创建一个ArrBinaryTree
        ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
        System.out.println("前序遍历");
        arrBinaryTree.preOrder();//1245367
        System.out.println("中序遍历");
        arrBinaryTree.indixOrder();//4251637
        System.out.println("后序遍历");
        arrBinaryTree.postOrder();//7361524
    }
}

//编写一个ArrayBinaryTree,实现顺序存储二叉树遍历
class ArrBinaryTree {
    private int[] arr;//存储数据节点的数据

    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }

    //重载preOrder
    public void preOrder() {
        this.preOrder(0);
    }

    //编写一个方法,完成顺序存储二叉树的遍历
    public void preOrder(int index) {
        //如果数组为空,或者arr.length = 0
        if (arr == null || arr.length == 0) {
            System.out.println("数组为空,无法遍历");
        }
        //输出当前这个元素
        System.out.println(arr[index]);
        //向左递归遍历
        if ((index * 2 + 1) < arr.length) {
            preOrder(2 * index + 1);
        }
        //向右递归遍历
        if ((index * 2 + 2) < arr.length) {
            preOrder(2 * index + 2);
        }
    }

    //重载indixOrder
    public void indixOrder() {
        this.indixOrder(0);
    }

    //编写一个方法,完成顺序存储二叉树的遍历
    public void indixOrder(int index) {
        //如果数组为空,或者arr.length = 0
        if (arr == null || arr.length == 0) {
            System.out.println("数组为空,无法遍历");
        }
        //向左递归遍历
        if ((index * 2 + 1) < arr.length) {
            indixOrder(2 * index + 1);
        }
        //输出当前这个元素
        System.out.println(arr[index]);
        //向右递归遍历
        if ((index * 2 + 2) < arr.length) {
            indixOrder(2 * index + 2);
        }
    }

    //重载postOrder
    public void postOrder() {
        this.postOrder(0);
    }

    //编写一个方法,完成顺序存储二叉树的遍历
    public void postOrder(int index) {
        //如果数组为空,或者arr.length = 0
        if (arr == null || arr.length == 0) {
            System.out.println("数组为空,无法遍历");
        }
        //向右递归遍历
        if ((index * 2 + 2) < arr.length) {
            postOrder(2 * index + 2);
        }
        //输出当前这个元素
        System.out.println(arr[index]);
        //向左递归遍历
        if ((index * 2 + 1) < arr.length) {
            postOrder(2 * index + 1);
        }
    }
}


线索化二叉树

在这里插入图片描述


问题分析:
当我们对上面的二叉树进行中序遍历时,数列为 {8, 3, 10, 1, 6, 14 }
但是 6, 8, 10, 14 这几个节点的 左右指针,并没有完全的利用上.
如果我们希望充分的利用 各个节点的左右指针, 让各个节点可以指向自己的前后节点,怎么办?
解决方案-线索二叉树

基本介绍

  • n个结点的二叉链表中含有n+1 【公式 2n-(n-1)=n+1】 个空指针域。利用二叉链表中的空指针域,存放指向该结点在某种遍历次序下的前驱和后继结点的指针(这种附加的指针称为"线索")
  • 这种加上了线索的二叉链表称为线索链表,相应的二叉树称为线索二叉树(Threaded BinaryTree)。根据线索性质的不同,线索二叉树可分为前序线索二叉树、中序线索二叉树和后序线索二叉树三种
  • 一个结点的前一个结点,称为前驱结点
  • 一个结点的后一个结点,称为后继结点

在这里插入图片描述

遍历线索化二叉树
  • 说明:对前面的中序线索化的二叉树, 进行遍历
  • 分析:因为线索化后,各个结点指向有变化,因此原来的遍历方式不能使用,这时需要使用新的方式遍历线索化二叉树,各个节点可以通过线型方式遍历,因此无需使用递归方式,这样也提高了遍历的效率。 遍历的次序应当和中序遍历保持一致。

代码

package tree.threadedbinarytree;
/*
 * @Author: Min
 * @Date:   2021/11/2
 * @description
 */

public class ThreadedBinaryTreeDemo {

    public static void main(String[] args) {
        //测试中序线索二叉树的功能
        HeroNode root = new HeroNode(1,"tom");
        HeroNode node2 = new HeroNode(3,"jack");
        HeroNode node3 = new HeroNode(6,"smith");
        HeroNode node4 = new HeroNode(8,"amy");
        HeroNode node5 = new HeroNode(10,"mary");
        HeroNode node6 = new HeroNode(14,"JFFDD");

        //二叉树,递归创建(目前手动创建)
        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);

        //测试线索化
        ThreadBinaryTree threadBinaryTree = new ThreadBinaryTree();
        threadBinaryTree.setRoot(root);
        threadBinaryTree.threadedNodes();

        //测试:10号节点做测试
        HeroNode leftnode = node5.getLeft();
        System.out.println("10号节点的前驱节点="+leftnode);    //3
        HeroNode rightnode = node5.getRight();
        System.out.println("10号节点的前驱节点="+rightnode);   //1

        //线索化二叉树后,使用原来的方法遍历会报错
        //编写方法threadList,进行新的遍历
        //遍历
        threadBinaryTree.threadList();
    }
}

//线索化二叉树
//定义ThreadBinaryTree二叉树
class ThreadBinaryTree {
    private HeroNode root;
    //为了实现线索化,需要创建要给当前节点的前驱节点的指针
    //在递归进行线索化时,pre总是保留前一个节点
    private HeroNode pre = null;

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

    public void setPre(HeroNode pre) {
        this.pre = pre;
    }

    //重载线索化的方法:threadedNodes
    public void threadedNodes() {
        this.threadedNodes(root);
    }

    //遍历线索化二叉树的方法
    public void threadList() {
        //定义一个变量,存储当前遍历的一个节点,从root开始
        HeroNode node = root;
        while (node != null) {
            //循环的找到leftType==1的节点,第一个找到的应该是8
            //后面随着遍历会变化,因为当leftType==1时,说明该节点是按照线索化处理后的有效节点
            while (node.getLeftType() == 0) {
                node = node.getLeft();
            }
            //打印当前节点
            System.out.println(node);
            //如果当前节点的右指针指向的是后继节点,就一直输出
            while (node.getRightType() == 1) {
                //说明当前节点的右指针指向的节点是他的后继节点
                node = node.getRight();
                System.out.println(node);
            }
            //替换这个遍历的节点
            node = node.getRight();
        }
    }

    //编写二叉树进行中序线索化的方法
    public void threadedNodes(HeroNode node) {
        //如果当前节点为空,无法线索化
        if (node == null) {
            return;
        }

        //线索化左子树
        threadedNodes(node.getLeft());
        //线索化当前节点!!!
        //处理当前节点的前驱节点
        //以8节点来理解
        //8节点的left=null,8节点的leftType=1
        if (node.getLeft() == null) {
            //让当前节点的左指针指向前驱节点
            node.setLeft(pre);
            //修改当前节点的左指针的类型,指向前驱节点
            node.setLeftType(1);
        }
        //处理后继结点
        if (pre != null && pre.getRight() == null) {
            //让前驱节点的右指针指向当前节点
            pre.setRight(node);
            //修改前驱节点的右指针类型
            pre.setRightType(1);
        }
        //!!!每处理一个节点后,让当前节点是下一个节点的前驱节点
        pre = node;

        //线索化右子树
        threadedNodes(node.getRight());

    }

    //删除节点
    public void delNode(int no) {
        if (root != null) {
            //如果只有一个root节点,这里立即判断root是不是要删除的节点
            if (root.getNo() == no) {
                root = null;
            } else {
                //递归删除
                root.delNode(no);
            }
        } else {
            System.out.println("空树,不能删除");
        }
    }

    //前序遍历
    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 HeroNode preOrderSearch(int no) {
        if (this.root != null) {
            return root.preOrderSearch(no);
        }else {
            return null;
        }
    }
    //中序查找
    public HeroNode infixOrderSearch(int no) {
        if (this.root != null) {
            return root.infixOrderSearch(no);
        }else {
            return null;
        }
    }
    //后序查找
    public HeroNode postOrderSearch(int no) {
        if (this.root != null) {
            return root.postOrderSearch(no);
        }else {
            return null;
        }
    }
}

//先创建HeroNode结点
class HeroNode {
    private int no;
    private String name;
    private HeroNode left;//默认null
    private HeroNode right;//默认null

    //说明:
    //1.如果leftType == 0,表示指向的是左子树,如果leftType == 1,表示指向前驱节点
    //2.如果leftType == 0,表示指向的是右子树,如果leftType == 1,表示指向后继节点
    private int leftType;
    private int rightType;

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

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    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 HeroNode getLeft() {
        return left;
    }

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

    public HeroNode getRight() {
        return right;
    }

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

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name=" + name + '}';
    }

    //递归删除节点
    //1.如果删除的节点是叶子点,则删除该节点
    //2.如果删除的节点是非叶子点,则删除该子树
    public void delNode(int no) {
        /*思路:
        1.因为我们的二叉制是单向的,所以我们是判断当前结点的子结点是否需要删除结点,而不能去判断当前这个结点是不是需要删除结点。
        2.如果当前结点的左子结点不为空,并且左子结点就是要删除结点,就将this.left = null;并且就返回(结束递归删除)
        3.如果当前结点的右子结点不为空,并且右子结点就是要删除结点,就将this.right= null ;并且就返回(结束递归删除)
        4.如果第2和第3步没有删除结点,那么我们就需要向左子树进行递归删除
        5.如果第4步也没有删除结点,则应当向右子树进行递归删除。
        */
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        if (this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }
        if (this.left != null) {
            this.left.delNode(no);
        }
        if (this.right != null) {
            this.right.delNode(no);
        }
    }

    //编写前序遍历的方法
    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);
    }

    /**
     * @param no 查找no
     * @return 如果找到返回该node, 否则返回null
     */
    //前序查找
    public HeroNode preOrderSearch(int no) {
        //比较当前节点是不是
//        System.out.println("进入前序遍历");//查看次数
        if (this.no == no) {
            return this;
        }
        //1.否则判断当前节点的左节点是否为空,若不,则递归前序查找
        //2.如果左递归前序查找,找到节点,则返回
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        //1.左递归前序查找,找到节点,则返回,否则继续
        //2.当前的节点的右子节点是否空,若不,则继续向右递归前序查找
        if (this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }

    //中序查找
    public HeroNode infixOrderSearch(int no) {
        //判断当前节点的左子节点是否为空,若不为空,则递归中序查找
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.infixOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
//        System.out.println("中序查找");//计算次数
        //如果找到,则返回,如果没有找到,就和当前节点比较,如果是返回当前节点
        if (this.no == no) {
            return this;
        }
        //否则继续进行右递归的中序查找
        if (this.right != null) {
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }

    //后序查找
    public HeroNode postOrderSearch(int no) {
        //判断当前节点的左子节点是否为空,如果不,则递归后序查找
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.postOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        //如果左子树没有找到,则向右子树进行后续遍历查找
        if (this.right != null) {
            resNode = this.right.postOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        //如果左右子树都没有找到,就比较当前节点是不是
        if (this.no == no) {
            return this;
        }
        return resNode;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值