源代码
public class ThreadedBiTreeDemo{
public static void main(String[] args) {
ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree();
TNode root = new TNode(1, "tom");
TNode node2 = new TNode(3, "jack");
TNode node3 = new TNode(6, "smith");
TNode node4 = new TNode(8, "mary");
TNode node5 = new TNode(10, "king");
TNode node6 = new TNode(14, "dim");
root.setLeft(node2);
root.setRight(node3);
node2.setLeft(node4);
node2.setRight(node5);
node3.setLeft(node6);
threadedBinaryTree.setRoot(root);
threadedBinaryTree.threadedNodes();
System.out.println(node5.getRight());
threadedBinaryTree.threadedList();
}
}
class ThreadedBinaryTree{
private TNode root;
private TNode pre = null;
public void setRoot(TNode root) {
this.root = root;
}
public void threadedList() {
TNode 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 void threadedNodes() {
this.threadedNodes(root);
}
public void threadedNodes(TNode node) {
if(node == null) {
return;
}
threadedNodes(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;
threadedNodes(node.getRight());
}
public void delNode(int no) {
if(this.root != null) {
if(this.root.getNo() == no) {
this.root = null;
}else {
this.root.delTNode(no);
}
}else {
System.out.println("这是一棵空树!");
}
}
public void preOrder() {
if(this.root != null) {
this.root.preOrder();
}else {
System.out.println("根节点为空,无法遍历!");
}
}
public void infixOrder() {
if(this.root != null) {
this.root.infixOrder();
}else {
System.out.println("根节点为空,无法遍历!");
}
}
public void postOrder() {
if(this.root != null) {
this.root.postOrder();
}else {
System.out.println("根节点为空,无法遍历!");
}
}
public TNode preOrderSearch(int no) {
if(this.root != null) {
return this.root.preOrderSearch(no);
}
return null;
}
public TNode infixOrderSearch(int no) {
if(this.root != null) {
return this.root.infixOrderSearch(no);
}
return null;
}
public TNode postOrderSearch(int no) {
if(this.root != null) {
return this.root.postOrderSearch(no);
}
return null;
}
}
class TNode{
private int no;
private String name;
private TNode left;
private TNode right;
private int leftType;
private int rightType;
public TNode(int no,String name) {
this.no = no;
this.name = name;
}
public int getLeftType() {
return leftType;
}
public void setLeftType(int leftType) {
this.leftType = leftType;
}
public int getRightType() {
return rightType;
}
public void setRightType(int rightType) {
this.rightType = rightType;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public TNode getLeft() {
return left;
}
public void setLeft(TNode left) {
this.left = left;
}
public TNode getRight() {
return right;
}
public void setRight(TNode right) {
this.right = right;
}
@Override
public String toString() {
return "TNode [no=" + no + ", name=" + name + "]";
}
public void preOrder() {
System.out.println(this);
if(this.left != null) {
this.left.preOrder();
}
if(this.right != null) {
this.right.preOrder();
}
}
public void infixOrder() {
if(this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if(this.right != null) {
this.right.infixOrder();
}
}
public void postOrder() {
if(this.left != null) {
this.left.postOrder();
}
if(this.right != null) {
this.right.postOrder();
}
System.out.println(this);
}
public TNode preOrderSearch(int no) {
TNode target = null;
if(this.no == no) {
return this;
}
if(this.left != null) {
target = this.left.preOrderSearch(no);
}
if(target != null) {
return target;
}
if(this.right != null) {
target = this.right.preOrderSearch(no);
}
return target;
}
public TNode infixOrderSearch(int no) {
TNode target = null;
if(this.left != null) {
target = this.left.infixOrderSearch(no);
}
if(target != null) {
return target;
}
if(this.no == no) {
return this;
}
if(this.right != null) {
target = this.right.infixOrderSearch(no);
}
return target;
}
public TNode postOrderSearch(int no) {
TNode target = null;
if(this.left != null) {
target = this.left.postOrderSearch(no);
}
if(target != null) {
return target;
}
if(this.right != null) {
target = this.right.postOrderSearch(no);
}
if(target != null) {
return target;
}
if(this.no == no) {
return this;
}
return null;
}
public void delTNode(int no) {
if(this.left != null && this.left.no == no) {
this.left = null;
return ;
}
if(this.right != null && this.right.no == no) {
this.right = null;
return;
}
if(this.left != null) {
this.left.delTNode(no);
}
if(this.right != null) {
this.right.delTNode(no);
}
}
}