1、节点
public class Node {
private int id;
private Node left;
private Node right;
private int leftType;
private int rightType;
}
2、前序线索二叉树
public class PreThreadedBinaryTree {
private Node root;
private Node pre;
public void createThreadedBinaryTree(Node node) {
if (node == null)
return;
if (node.getLeft() == null) {
node.setLeft(pre);
node.setLeftType(1);
}
if (pre != null && pre.getRight() == null) {
pre.setRight(node);
pre.setRightType(1);
}
pre = node;
if (node.getLeft() != null && node.getLeftType() == 0)
createThreadedBinaryTree(node.getLeft());
if (node.getRight() != null && node.getRightType() == 0)
createThreadedBinaryTree(node.getRight());
}
public void preOder() {
Node node = root;
while (node != null) {
System.out.println(node);
while (node.getLeftType() == 0) {
node = node.getLeft();
System.out.println(node);
}
while (node.getRightType() == 1) {
node = node.getRight();
System.out.println(node);
}
if (node.getLeftType() == 0)
node = node.getLeft();
else
node = node.getRight();
}
}
}
public static void main(String[] args) {
Node node = new Node(1);
Node node1 = new Node(2);
Node node2 = new Node(3);
Node node3 = new Node(4);
Node node4 = new Node(5);
Node node5 = new Node(6);
Node node6 = new Node(7);
PreThreadedBinaryTree pre = new PreThreadedBinaryTree();
pre.root = node;
node.setLeft(node1);
node.setRight(node2);
node1.setLeft(node3);
node1.setRight(node4);
node2.setLeft(node5);
node2.setRight(node6);
pre.createThreadedBinaryTree(pre.root);
pre.preOder();
}
3、中序线索二叉树
public class InThreadedBinaryTree {
private Node root;
private Node pre;
public void createThreadedBinaryTree(Node node) {
if (node == null)
return;
createThreadedBinaryTree(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 = node;
createThreadedBinaryTree(node.getRight());
}
public void inOder() {
Node node = root;
while (node != null) {
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 static void main(String[] args) {
Node node = new Node(1);
Node node1 = new Node(2);
Node node2 = new Node(3);
Node node3 = new Node(4);
Node node4 = new Node(5);
Node node5 = new Node(6);
Node node6 = new Node(7);
InThreadedBinaryTree in = new InThreadedBinaryTree();
in.root = node;
node.setLeft(node1);
node.setRight(node2);
node1.setLeft(node3);
node1.setRight(node4);
node2.setLeft(node5);
node2.setRight(node6);
in.createThreadedBinaryTree(in.root);
in.inOder();
}
}
4、后序线索二叉树
public class PostThreadedBinaryTree {
private Node root;
private Node pre;
public void createThreadedBinaryTree(Node node) {
if (node == null)
return;
createThreadedBinaryTree(node.getLeft());
createThreadedBinaryTree(node.getRight());
if (node.getLeft() == null) {
node.setLeft(pre);
node.setLeftType(1);
}
if (pre != null && pre.getRight() == null) {
pre.setRight(node);
pre.setRightType(1);
}
pre = node;
}
public static void main(String[] args) {
Node node = new Node(1);
Node node1 = new Node(2);
Node node2 = new Node(3);
Node node3 = new Node(4);
Node node4 = new Node(5);
Node node5 = new Node(6);
Node node6 = new Node(7);
PostThreadedBinaryTree post = new PostThreadedBinaryTree();
post.root = node;
node.setLeft(node1);
node.setRight(node2);
node1.setLeft(node3);
node1.setRight(node4);
node2.setLeft(node5);
node2.setRight(node6);
post.createThreadedBinaryTree(post.root);
}
}