# [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.
*/
protected final static String ENDFLAG = "null";

}

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

//初始化二叉树
String item;
Scanner in = new Scanner(System.in);
item = in.next();
if (!item.equalsIgnoreCase(ENDFLAG)) {
}
item = in.next();
if (!item.equalsIgnoreCase(ENDFLAG)) {
}
}

//创建线索二叉树
{

}

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

}
}


package com.chen.arithmetic_test.ThreadBiTree_test;

/**
* 中序线索二叉树类
* Created by ChenMP on 2017/8/1.
*/

super();
}

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

@Override

}

//中序线索化二叉树
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() {

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) {

tree.initBiTree();
System.out.println("中序遍历序列是：");
tree.traversal();

}
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120