线索二叉树的定义:
把结点中指向前驱结点和后继结点的指针称为线索。在二叉树的结点上加上线索的二叉树称作线索二叉树。对二叉树以某种方法(如前序、中序或后序方法)遍历使其变为线索二叉树的过程称作按该方法对二叉树进行的线索化。
首先定义二叉树节点用于初始化二叉树:
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();
}
}