数据结构 线索化二叉树

HeroNode root

public class ClurBinaryTree {
    private HeroNode root;
    //当前节点前驱结点指针
    private HeroNode pre=null;

    public void setRoot(HeroNode root) {
        this.root = root;
    }
    /**
     * xiansuohuaerchashu
     */
    public void clueNode(){
        this.clueNode(root);
    }
    /**
     * 把普通二叉树转化为线索二叉树
     * root
     */
    public void clueNode(HeroNode node){
        //判断是否可以线索化当前节点
        if(node==null){
            return;
        }
        //先线索化左子树
        clueNode(node.getLeft());
        //处理当前节点的前驱
        if(node.getLeft()==null){
            node.setLeft(pre);
            node.setNoleft(1);
        }
        //处理当前节点的后继
        if(pre!=null&&pre.getRight()==null){
            pre.setRight(node);
            pre.setNoright(1);
        }
        pre =node;
        clueNode(node.getRight());
    }
    /**
     * 遍历线索化的二叉树
     */
    public void clueList(){
        HeroNode node =root;
        while(node!=null){
            while (node.getNoleft()==0){
                node=node.getLeft();
            }
            System.out.println(node);
            while (node.getNoright()==1){
                node=node.getRight();
                System.out.println(node);
            }
            node=node.getRight();
        }
    }
}

class HeroNode

public class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;
    /**
     * 如果是0表示指向的是左子树。1.表示指向的前驱
     */
    private int noleft;
    /**
     * 如果是0,表示指向右子树。1.表示指向的是后继
     */
    private int noright;

    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 getNoleft() {
        return noleft;
    }

    public void setNoleft(int noleft) {
        this.noleft = noleft;
    }

    public int getNoright() {
        return noright;
    }

    public void setNoright(int noright) {
        this.noright = noright;
    }

    @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 perSelect(){
        System.out.println(this);
        if(this.left!=null){
            this.left.perSelect();
        }
        if(this.right!=null){
            this.right.perSelect();
        }
    }
    /**
     * 根据编号进行查询
     */
    public HeroNode preSearch(int no){
        if(this.no==no){
            return this;
        }
        HeroNode heroNode= null;
        //往左递归
        if(this.left!=null){
            heroNode = this.left.preSearch(no);
        }
        if(heroNode!=null){
            return heroNode;
        }
        //往右递归
        if(this.right!=null){
             heroNode = this.right.preSearch(no);
        }
        return heroNode;

    }

}

class test

public class test {
    public static void main(String[] args) {
        HeroNode root =new HeroNode(1,"吕布");
        HeroNode node2 =new HeroNode(3,"貂蝉");
        HeroNode node3 =new HeroNode(6,"曹操");
        HeroNode node4 =new HeroNode(8,"刘备");
        HeroNode node5 =new HeroNode(10,"关羽");
        HeroNode node6 =new HeroNode(14,"张飞");
        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);
        ClurBinaryTree clurBinaryTree = new ClurBinaryTree();
        clurBinaryTree.setRoot(root);
        clurBinaryTree.clueNode();
        HeroNode left=node5.getLeft();
        HeroNode right = node5.getRight();
        System.out.println(left);
        System.out.println(right);
        clurBinaryTree.clueList();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值