java实现中序线索化二叉树

java实现中序线索化二叉树

节点类

/**
 * 节点类
 */
class Node {
    private int id;
    private Node left;
    private Node right;

    /**
     * 规定:
     * 1.如果leftType==0表示指向是左子树,如果等于1表示指向前驱节点
     * 2.如果rightType==0表示指向是右子树,如果等于1表示指向后继节点
     */
    private int leftType;
    private int rightType;

    
    @Override
    public String toString() {
        return "Node{" +
                "id=" + id +
                '}';
    }

    public Node(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

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

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

二叉树类

/**
 * 线索化二叉树
 */
class ThreadBinaryTree {
    private Node root;

    // 为了实现线索化,需要当前节点的前驱节点
    private Node pre = null;

    /**
     * 重载
     */
    public void threadedNodes() {
        this.threadedNodes(root);
    }

    /**
     * 中序线索化二叉树
     *
     * @param node 当前需要线索化的节点
     */
    public void threadedNodes(Node 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
        pre = node;

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

    }



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

测试类

/**
 * 线索化二叉树
 */
public class ThreadedBinaryTree {
    public static void main(String[] args) {
        ThreadBinaryTree threadBinaryTree = new ThreadBinaryTree();

        Node root = new Node(000);

        Node node01 = new Node(001);
        Node node02 = new Node(002);
        Node node03 = new Node(003);
        Node node04 = new Node(004);
        Node node05 = new Node(005);

        // 手动创建二叉树
        threadBinaryTree.setRoot(root);
        root.setLeft(node01);
        root.setRight(node02);
        node01.setLeft(node03);
        node01.setRight(node04);
        node02.setLeft(node05);

        threadBinaryTree.threadedNodes();

        // 测试node04
        System.out.println(node04);
        System.out.println("node04的前后节点和状态:");
        System.out.println("node04的左节点 ==> " + node04.getLeft());
        System.out.println("node04左节点状态 ==> " + node04.getLeftType());
        System.out.println("node04的右节点  ==> " + node04.getRight());
        System.out.println("node04右节点状态 ==> " + node04.getRightType());

    }
}
posted @ 2022-04-18 17:04  CoderCatIce  阅读(70)  评论(0编辑  收藏  举报