线索二叉树
package com.gykalc.tree;
public class ThreadedBinaryTreeDemo {
}
class ThreadedBinaryTree<E> {
private Node<E> root;
private Node<E> preNode = null;
public static void main(String[] args) {
Integer[] array = {1,2,3,4,5,6,7};
ThreadedBinaryTree<Integer> tbt = new ThreadedBinaryTree<>();
tbt.createBinaryTree(array, 0);
System.out.println("=====前序排序=====");
tbt.preOrder();
System.out.println("=====中序排序=====");
tbt.midOrder();
System.out.println("=====后序排序=====");
tbt.postOrder();
System.out.println("=====中序线索化=====");
tbt.midOrderThreaded();
System.out.println("======中序线索化后遍历=====");
tbt.midOrderThreadedList();
}
private Node<E> createBinaryTree(E[] array, int index) {
Node<E> node = null;
if (index < array.length) {
node = new Node<>(array[index]);
node.left = createBinaryTree(array, 2 * index + 1);
node.right = createBinaryTree(array, 2 * index + 2);
}
this.root = node;
return node;
}
public void preOrder() {
this.preOrder(this.root);
}
public void preOrder(Node<E> root) {
if (root != null) {
System.out.println(root.getE());
preOrder(root.left);
preOrder(root.right);
}
}
public void preOrderThreaded() {
this.preOrderThreaded(this.root);
}
public void preOrderThreaded(Node<E> root) {
if (root != null) {
boolean leftFlag = false;
if (root.left == null) {
root.left = this.preNode;
root.ltag = true;
leftFlag = true;
}
if (this.preNode != null && this.preNode.right == null) {
this.preNode.right = root;
this.preNode.rtag = true;
}
this.preNode = root;
if (!leftFlag) {
preOrderThreaded(root.left);
}
preOrderThreaded(root.right);
}
}
public void midOrderThreaded() {
this.midOrderThreaded(this.root);
}
public void midOrderThreaded(Node<E> root) {
if (root != null) {
midOrderThreaded(root.left);
if (root.left == null) {
root.left = preNode;
root.ltag = true;
}
if (preNode != null && preNode.right == null) {
preNode.right = root;
preNode.rtag = true;
}
this.preNode = root;
midOrderThreaded(root.right);
}
}
public void midOrderThreadedList(Node<E> root) {
if (root == null) {
return;
}
Node<E> temp = root;
while (temp != null) {
while (!temp.ltag) {
if (temp.left == null) {
break;
}
temp = temp.left;
}
System.out.println(temp.getE());
while(temp.rtag) {
temp = temp.right;
System.out.println(temp.getE());
}
temp = temp.right;
}
}
public void midOrderThreadedList() {
this.midOrderThreadedList(this.root);
}
public void midOrder() {
this.midOrder(this.root);
}
public void midOrder(Node<E> root) {
if (root != null) {
midOrder(root.left);
System.out.println(root.getE());
midOrder(root.right);
}
}
public void postOrder() {
this.postOrder(this.root);
}
public void postOrder(Node<E> root) {
if(root != null) {
postOrder(root.left);
postOrder(root.right);
System.out.println(root.getE());
}
}
class Node<E> {
private E e;
private Node<E> left;
private Node<E> right;
private boolean ltag = false;
private boolean rtag = false;
public Node() {
}
public Node(E e) {
this.e = e;
}
public E getE() {
return e;
}
public void setE(E e) {
this.e = e;
}
public Node<E> getLeft() {
return left;
}
public void setLeft(Node<E> left) {
this.left = left;
}
public Node<E> getRight() {
return right;
}
public void setRight(Node<E> right) {
this.right = right;
}
public boolean isLtag() {
return ltag;
}
public void setLtag(boolean ltag) {
this.ltag = ltag;
}
public boolean isRtag() {
return rtag;
}
public void setRtag(boolean rtag) {
this.rtag = rtag;
}
}
}