线索化二叉树

线索化二叉树

什么是线索化二叉树

原本二叉树
将数列 {1,3,6,8,10,14}构建成一颗二叉树.n+1=7
在这里插入图片描述
在这里插入图片描述

问题分析:

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

缺点: 没有充分的利用指针

有关的概念

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

思路分析

应用案例说明:将下面的二叉树,进行中序线索二叉树。
中序遍历的数列为{8,3,10, 1, 14,6)
在这里插入图片描述
思路分析: 中序遍历的结果: {8,3, 10, 1, 14,6}
在这里插入图片描述
说明:当线索化二叉树后,节点节点的属性左和右有如下情况:

  1. left指向的是左子树,也可能是指向的前驱节点.比如①节点left指向的左子树,而⑩节点的left指向的就是前驱节点.
  2. **right 指向的是右子树,也可能是指向后继节点,**比如①节点right指向的是右子树,而⑩节点的right 指向的是后继节点.

代码实现

先实现 把二叉树 变成线索化的二叉树,让后循环遍历线索化二叉树
注意: 遍历的方法和之前不一样的

package com.atguigus.tree.threadedbinarytree;

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, "mary");
        HeroNode node5 = new HeroNode(10, "king");
        HeroNode node6 = new HeroNode(14, "dim");

        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);

        ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree();
        threadedBinaryTree.setRoot(root);
        threadedBinaryTree.threadedNodes();

        HeroNode leftNode = node5.getLeft();
        HeroNode rightNOde = node5.getRight();
        System.out.println(String.format("10号节点的前驱节点是=%s", leftNode));
        System.out.println(String.format("10号节点的前驱节点是=%s", rightNOde));

        System.out.println("使用线索化的方式遍历 线索化二叉树");
        threadedBinaryTree.threadedList();//8,3,10,1,14,6
    }
}

//线索化二叉树
class ThreadedBinaryTree{
    private HeroNode root;

    //前驱节点的指针
    private HeroNode pre = null;

    public void threadedNodes(){
        this.threadedNodes(root);
    }


    public void threadedList(){
        //定义一个变量,存储当前遍历的节点
        HeroNode node = root;
        while (node != null) {
            //找到最左边的节点
            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());
        //线序化当前节点
        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 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 (root != null) {
            return root.preOrderSearch(no);
        }else {
            return null;
        }
    }

    //中序遍历
    public HeroNode infixOrderSearch(int no){
        if (root != null) {
            return root.infixOrderSearch(no);
        }else {
            return null;
        }
    }

    //后序遍历
    public HeroNode postOrderSearch(int no){
        if (root != null) {
            return this.root.postOrderSearch(no);
        }else{
            return null;
        }
    }

    public void delNode(int no){
        if (root != null) {
            if (root.getNo() == no) {
                root = null;
            }else {//递归删除
                root.delNode(no);
            }
        }else {
            System.out.println("空树,不能删除~");
        }
    }


}


//先创建HeroNode节点
class HeroNode{
    private int no ;
    private String name;
    private HeroNode left;//默认null
    private HeroNode right;
    //如果leftType == 0 表示指向的是左子树,如果1 则表示指向前驱节点
    //如果leftType == 0 表示指向的是右边子树,如果1 则表示指向后继节点
    private int leftType ;
    private int rightType;

    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;
    }

    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;
    }

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

    //递归删除节点
    public void delNode(int no){
        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);
    }

    //前序遍历查找
    public HeroNode preOrderSearch(int no ){
        System.out.println("进入前序遍历");
        if (this.no == no) {
            return this;
        }
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        //
        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;
        }
        System.out.println("进入后序查找");
        if (this.no == no) {
            return this;
        }
        return resNode;
    }

}

运行结果

E:\jdk\jdk1.8.0_161\bin\java.exe "-javaagent:D:\IDEA\IntelliJ IDEA 2022.2.1\lib\idea_rt.jar=60008:D:\IDEA\IntelliJ IDEA 2022.2.1\bin" -Dfile.encoding=UTF-8 -classpath E:\jdk\jdk1.8.0_161\jre\lib\charsets.jar;E:\jdk\jdk1.8.0_161\jre\lib\deploy.jar;E:\jdk\jdk1.8.0_161\jre\lib\ext\access-bridge-64.jar;E:\jdk\jdk1.8.0_161\jre\lib\ext\cldrdata.jar;E:\jdk\jdk1.8.0_161\jre\lib\ext\dnsns.jar;E:\jdk\jdk1.8.0_161\jre\lib\ext\jaccess.jar;E:\jdk\jdk1.8.0_161\jre\lib\ext\jfxrt.jar;E:\jdk\jdk1.8.0_161\jre\lib\ext\localedata.jar;E:\jdk\jdk1.8.0_161\jre\lib\ext\nashorn.jar;E:\jdk\jdk1.8.0_161\jre\lib\ext\sunec.jar;E:\jdk\jdk1.8.0_161\jre\lib\ext\sunjce_provider.jar;E:\jdk\jdk1.8.0_161\jre\lib\ext\sunmscapi.jar;E:\jdk\jdk1.8.0_161\jre\lib\ext\sunpkcs11.jar;E:\jdk\jdk1.8.0_161\jre\lib\ext\zipfs.jar;E:\jdk\jdk1.8.0_161\jre\lib\javaws.jar;E:\jdk\jdk1.8.0_161\jre\lib\jce.jar;E:\jdk\jdk1.8.0_161\jre\lib\jfr.jar;E:\jdk\jdk1.8.0_161\jre\lib\jfxswt.jar;E:\jdk\jdk1.8.0_161\jre\lib\jsse.jar;E:\jdk\jdk1.8.0_161\jre\lib\management-agent.jar;E:\jdk\jdk1.8.0_161\jre\lib\plugin.jar;E:\jdk\jdk1.8.0_161\jre\lib\resources.jar;E:\jdk\jdk1.8.0_161\jre\lib\rt.jar;D:\课程\java课程\guiGu_suanFa\out\production\guiGu_suanFa com.atguigus.tree.threadedbinarytree.ThreadedBinaryTreeDemo
10号节点的前驱节点是=HeroNode{no=3, name='jack'}
10号节点的前驱节点是=HeroNode{no=1, name='tom'}
使用线索化的方式遍历 线索化二叉树
HeroNode{no=8, name='mary'}
HeroNode{no=3, name='jack'}
HeroNode{no=10, name='king'}
HeroNode{no=1, name='tom'}
HeroNode{no=14, name='dim'}
HeroNode{no=6, name='smith'}

Process finished with exit code 0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值