二叉树笔记四、线索化二叉树代码实现

package com.hao.firstdemo.datastruct.threade;

import lombok.Data;
import lombok.Setter;

/**
 * @author haoxiansheng
 * @data 2020/5/9 23:30
 */
public class ThreadBinaryTreeTest {

    public static void main(String[] args) {
        //测试
        HeroNode root = new HeroNode(1, "java");
        HeroNode node2 = new HeroNode(3, "hadoop");
        HeroNode node3 = new HeroNode(6, "flink");
        HeroNode node4 = new HeroNode(8, "spark");
        HeroNode node5 = new HeroNode(10, "shell");
        HeroNode node6 = new HeroNode(14, "scala");

        // 先手动创建
        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);

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

        System.out.println("10号节点的前驱节点" + node5.getLeft());
        System.out.println("10号节点的后驱节点" + node5.getRight());

        //遍历线索化二叉树 8、3 10 1 14 6
        threadedBinaryTree.threadedList();

    }
}

//线索化 功能二叉树
class ThreadedBinaryTree{
    @Setter
    private HeroNode root;

    //为了实现线索化、需要创建要给指向当前节点的前驱节点的指针
    //在递归进行线索化时,pre总是保留当前一个节点
    private HeroNode pre = null;

    //遍历x线索化二叉树的方法
    public void threadedList() {
        //临时变量
        HeroNode node = root;
        while (node != null) {
            //循环找到leftType==1 后面随着遍历会变化因为当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 threadNodes() {
        this.threadNodes(root);
    }

    /**
     * 编写对二叉树进行中序线索化的方法
     * @param node
     */
    public void threadNodes(HeroNode node) {
        if (node == null) {
            return;
        }
        // 1、先线索化左子树
        threadNodes(node.getLeft());
        // 2、线索化当前节点
        if (node.getLeft() == null) {
            node.setLeft(pre);   //当前节点的左指针指向前驱节点
            node.setLeftType(1); //设置当前的指针类型 指向前驱节点
        }

        //处理后继节点
        if (pre != null && pre.getRight() == null) {
            pre.setRight(node);  //让前驱节点右指针指向当前节点
            pre.setRightType(1); //修改前驱节点的右指针类型
        }

        // 每处理一个节点后,让当前节点是下一个节点的前驱节点
        pre = node;

        // 3、线索化右子树
        threadNodes(node.getRight());
    }


    public void delNode(int no) {
        if (root != null) {
            //判断当前节点是不是要删除的节点
            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("二叉树为null");
        }
    }

    //中序遍历
    public void infixOrder() {
        if (this.root != null) {
            this.root.infixOrder();
        } else {
            System.out.println("二叉树为null");
        }
    }

    //后序遍历
    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        } else {
            System.out.println("二叉树为null");
        }
    }

    //前序遍历搜索
    public HeroNode preOrderSearch(int no) {
        if (root != null) {
            return   this.root.preOrderSearch(no);
        } else {
            return null;
        }
    }

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

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

//先创建HeroNode 节点
@Data
//get set 方法
class HeroNode {
    private int no;

    private String name;

    private HeroNode left;   //默认为null

    private HeroNode right;  //默认为null

    private int leftType; // left == 0  指向左子树  1 前驱节点

    private int rightType; //0  指向右子树  1 后驱节点

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

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

    /**
     * 递归删除节点 这个只是一个开始思路并不完全对 可以先当作一个简单理解
     * 1、如果删除的节点是叶子节点,则删除该节点
     * 2、如果删除节点是非叶子节点,则删除该子树
     */
    public void delNode(int no) {

        // 当前节点的左子节点不为null,并且就是要删除的节点,就讲this.left = null 并返回
        if (this.left != null &&  this.left.no == no) {
            this.left = null;
            return;
        }
        // 当前节点的右子节点不为null,并且就是要删除的节点,就讲this.right = null 并返回
        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
     * @return 如果存在就返回HeroNode 否则返回null
     */
    public HeroNode preOrderSearch(int no) {
        System.out.println("比较次数");
        if (this.no == no) {
            return this;
        }
        HeroNode resNode = null;
        if (this.left != null) { //判断当前节点是否为null, 如果没有找到返回null
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null) { //说明找到了
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }

    /**
     * 中序遍历查找
     * @param no
     * @return
     */
    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;
    }

    /**
     * 后序遍历
     * @param no
     * @return
     */
    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;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值