import java.util.LinkedList;
/*
* 非栈顶元素,已经判断过左节点是否存在
* 对于栈中的栈顶元素,可以分为两个状态:
* 1、没有判断过左节点
* 2、判断过左节点
* 布尔值 b 用于记录栈顶元素状态
* true表示状态1
* false表示状态2
*
* 对于起始的根节点,认为其是状态1,所以 b 的初值赋为 true
*/
public class ErgodicTree<T> {
//这里将LinkedList当成栈使用
private LinkedList<Node<T>> link = new LinkedList<>();
//栈顶指针
private Node<T> node;
//状态指针
private boolean b = true;
ErgodicTree(Node<T> node) {
if(node == null) {
throw new IllegalArgumentException("cann't give me a null");
}
this.node = node;
link.add(node);
}
public void eachNode() {
while(this.node != null) {
if(b) {
Node<T> leftNode = this.node.getLeftNode();
if(leftNode == null) {
//改变栈顶元素的状态
b = false;
continue;
}
link.add(leftNode);
//将指针指向栈顶
this.node = leftNode;
}else {
//这里可以抽象出一个接口进行元素操作,可用模式:策略模式、模板模式
System.out.println(this.node.get().hashCode());
//对栈顶值进行出栈操作(弹栈)
link.removeLast();
//将指针指向栈顶
this.node = this.node.getRightNode();
if(this.node == null) {
if(link.size() == 0) {
//栈中无值,循环结束
this.node = null;
}else {
//将指针指向栈顶
this.node = link.getLast();
}
}else {
//对元素进行入栈操作(压栈)
link.add(this.node);
//改变栈顶元素的状态
this.b = true;
}
}
}
}
/*
* 以下为二叉树的迭代器逻辑
*/
public boolean hashNext() {
if(link.size() != 0 || this.node.getRightNode() != null) {
return true;
}
return false;
}
public Node<T> next() {
if(!hashNext()) {
throw new IllegalArgumentException("not have more node");
}
/*
* 要获取下一个栈顶元素:
* 首先b = true
* 获取首个栈顶元素
*/
while(true) {
if(b) {
Node<T> leftNode = this.node.getLeftNode();
if(leftNode == null) {
//改变栈顶元素的状态
b = false;
//对栈顶值进行出栈操作(弹栈)
link.removeLast();
return this.node;
}
link.add(leftNode);
//将指针指向栈顶
this.node = leftNode;
}else {
this.node = this.node.getRightNode();
if(this.node == null) {
//将指针指向栈顶
this.node = link.getLast();
link.removeLast();
return this.node;
}else {
//对元素进行入栈操作(压栈)
link.add(this.node);
//改变栈顶元素的状态
this.b = true;
}
}
}
}
}
一下为使用到的节点
节点接口:
public interface Node <T> {
public void add(T t);
public T get();
public Node<T> getRightNode();
public Node<T> getLeftNode();
public void setRightNode(Node<T> rightNode);
public void setLeftNode(Node<T> leftNode);
}
节点实例:
package www.test.tree;
public class NodeRealize<T> implements Node<T>{
private Node<T> leftNode;
private Node<T> rightNode;
private T t;
@Override
public void add(T t) {
if(t == null) {
throw new IllegalArgumentException("cann't give me a null");
}
if(this.t == null) {
this.t = t;
return;
}
//这里可以抽象出一个比较的方法或接口,可以使用的模式:策略模式、模板模式
boolean b = t.hashCode() > this.t.hashCode();
if(b && rightNode != null) {
this.rightNode.add(t);
return;
}
if(!b && leftNode != null) {
this.leftNode.add(t);
return;
}
NodeRealize<T> nodeRealize = new NodeRealize<>();
nodeRealize.set(t);
//经过之前的判断过程,可以得出b 对应的right或left为空,所以这里只需要进行布尔值的判断即可
if(b) {
this.rightNode = nodeRealize;
}else {
this.leftNode = nodeRealize;
}
}
@Override
public T get() {
return this.t;
}
public void set(T t) {
this.t = t;
}
@Override
public Node<T> getLeftNode() {
return leftNode;
}
@Override
public void setLeftNode(Node<T> leftNode) {
this.leftNode = leftNode;
}
@Override
public Node<T> getRightNode() {
return rightNode;
}
@Override
public void setRightNode(Node<T> rightNode) {
this.rightNode = rightNode;
}
}
package www.test.tree;
使用样例:
public class TestDemo {public static void main(String[] args) {
Node<String> realize = new NodeRealize<String>();
realize.add("3");
realize.add("2");
realize.add("3");
realize.add("4");
realize.add("5");
realize.add("6");
ErgodicTree<String> tree = new ErgodicTree<String>(realize);
while(tree.hashNext()) {
System.out.println(tree.next().get());
}
}
}