[Java算法分析与设计]中序线索化二叉树

线索二叉树的定义:
把结点中指向前驱结点和后继结点的指针称为线索。在二叉树的结点上加上线索的二叉树称作线索二叉树。对二叉树以某种方法(如前序、中序或后序方法)遍历使其变为线索二叉树的过程称作按该方法对二叉树进行的线索化。

这里写图片描述

首先定义二叉树节点用于初始化二叉树:

package com.chen.arithmetic_test.ThreadBiTree_test;

/**
 * 线索二叉树的节点类
 * Created by ChenMP on 2017/8/1.
 */
public class BiTreeNode {
    private BiTreeNode leftNode; //左孩子
    private BiTreeNode rightNode; //右孩子
    private Boolean leftFlag; //是否是左线索
    private Boolean rightFlag; //是否是右线索
    private Object data;

    public BiTreeNode() {
        this.leftNode = null;
        this.rightNode = null;
        this.leftFlag = false;
        this.rightFlag = false;
    }

    public BiTreeNode(Object data)
    {
        this();
        this.data = data;
    }

    public BiTreeNode getLeftNode() {
        return leftNode;
    }

    public void setLeftNode(BiTreeNode leftNode) {
        this.leftNode = leftNode;
    }

    public BiTreeNode getRightNode() {
        return rightNode;
    }

    public void setRightNode(BiTreeNode rightNode) {
        this.rightNode = rightNode;
    }

    public Boolean getLeftFlag() {
        return leftFlag;
    }

    public void setLeftFlag(Boolean leftFlag) {
        this.leftFlag = leftFlag;
    }

    public Boolean getRightFlag() {
        return rightFlag;
    }

    public void setRightFlag(Boolean rightFlag) {
        this.rightFlag = rightFlag;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}

定义线索二叉树类:

package com.chen.arithmetic_test.ThreadBiTree_test;

import java.util.Scanner;

/**
 * 线索二叉树类
 * Created by ChenMP on 2017/8/1.
 */
public class ThreadBiTree {
    protected BiTreeNode head;
    protected final static String ENDFLAG = "null";

    public ThreadBiTree() {
        this.head = null;
    }

    //初始化二叉树的根节点
    public void initBiTree() {
        String item;
        Scanner in = new Scanner(System.in);
        System.out.println("请输入二叉树的树根结点(输入null表示该结点为空):");
        item = in.next();
        if (!item.equalsIgnoreCase(ENDFLAG)) {
            head = new BiTreeNode(item);
            init(head);
        }
    }

    //初始化二叉树
    private void init(BiTreeNode head) {
        String item;
        Scanner in = new Scanner(System.in);
        System.out.println("请输入"+head.getData()+"结点的左孩子结点:");
        item = in.next();
        if (!item.equalsIgnoreCase(ENDFLAG)) {
            head.setLeftNode(new BiTreeNode(item));
            init(head.getLeftNode()); //递归
        }
        System.out.println("请输入"+head.getData()+"结点的右孩子结点:");
        item = in.next();
        if (!item.equalsIgnoreCase(ENDFLAG)) {
            head.setRightNode(new BiTreeNode(item));
            init(head.getRightNode()); //递归
        }
    }

    //创建线索二叉树
    public void createThreadBiTree()
    {

    }

    //线索遍历算法
    public void traversal()
    {

    }
}

定义中序线索二叉树类:

package com.chen.arithmetic_test.ThreadBiTree_test;

/**
 * 中序线索二叉树类
 * Created by ChenMP on 2017/8/1.
 */
public class InThreadBiTree extends ThreadBiTree {

    public InThreadBiTree() {
        super();
    }

    @Override
    public void initBiTree() {
        // TODO Auto-generated method stub
        super.initBiTree();
    }

    @Override
    public void createThreadBiTree() {

        create(head,null);
    }

    //中序线索化二叉树
    private BiTreeNode create(BiTreeNode curNode, BiTreeNode preNode) { 
        if (curNode != null) {
            BiTreeNode tempNode = create(curNode.getLeftNode(), preNode); //获取最左叶节点

            if ((!curNode.getLeftFlag()) && (curNode.getLeftNode() == null)) //设置左节点线索化
            {
                curNode.setLeftFlag(true);
                curNode.setLeftNode(preNode);
            } // end of if

            preNode = tempNode;

            if ((preNode != null) && (preNode.getRightNode() == null))  //设置右节点线索化
            {
                preNode.setRightFlag(true);
                preNode.setRightNode(curNode);
            } // end of if

            preNode = curNode;
            preNode = create(curNode.getRightNode(), preNode);
            return preNode;
        } // end of if

        return preNode;
    }

    //中序线索遍历算法
    @Override
    public void traversal() {
        BiTreeNode walker = this.head;

        if (this.head != null) {
            while (!walker.getLeftFlag())
                walker = walker.getLeftNode();

            System.out.print(walker.getData() + " ");

            while (walker.getRightNode() != null) {
                if (walker.getRightFlag())
                    walker = walker.getRightNode();
                else {
                    walker = walker.getRightNode();
                    while ((walker.getLeftNode() != null) && (!walker.getLeftFlag()))
                        walker = walker.getLeftNode();
                } // end of else

                System.out.print(walker.getData() + " ");
            } // end of while
        } // end of if
    }
}

测试类:

package com.chen.arithmetic_test.ThreadBiTree_test;

/**
 * Created by ChenMP on 2017/8/1.
 */
public class Test {
    public static void main(String[] args) {

        ThreadBiTree tree = new InThreadBiTree();
        tree.initBiTree();
        tree.createThreadBiTree();
        System.out.println("中序遍历序列是:");
        tree.traversal();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值