整理了一下这几种遍历的精简版代码,思路简介明确,其中后序非递归方法用到了双栈,代码如下:
import LinkedStack;
import BinaryNode;
import org.junit.Test;
public class PosIterator<T> {
private BinaryNode root;
//递归的算法 ---先根遍历
public void preRead(BinaryNode<Integer> p) {
if (p != null) {
System.out.println(p.data);
preRead(p.left);
preRead(p.right);
}
}
//递归的算法 ---中根遍历
public void inRead(BinaryNode<Integer> p) {
if (p != null) {
inRead(p.left);
System.out.println(p.data);
inRead(p.right);
}
}
//递归的算法 ---后根遍历
public void posRead(BinaryNode<Integer> p) {
if (p != null) {
posRead(p.left);
posRead(p.right);
System.out.println(p.data);
}
}
//非递归算法 ---先根遍历
public void preOrderTraverse() {
System.out.println("根遍历(非递归): ");
LinkedStack<BinaryNode<T>> stack = new LinkedStack<BinaryNode<T>>();
BinaryNode<T> p = this.root;
while (p != null || !stack.isEmpty()) {
if (p != null) {
stack.push(p);
p = p.left;
}
else {
System.out.print("^");
p = stack.pop();
System.out.print(p.data);//访问数据。
p = p.right;
}
}
System.out.println();
}
//非递归算法 ---中根遍历
public void inOrderTraverse() {
System.out.println("中根遍历(非递归): ");
LinkedStack<BinaryNode<T>> stack = new LinkedStack<BinaryNode<T>>();
BinaryNode<T> p = this.root;
while (p != null || !stack.isEmpty()) {
if (p != null) {
System.out.print(p.data);//访问数据。
stack.push(p);
p = p.left;
}
else {
System.out.print("^");
p = stack.pop();
p = p.right;
}
}
System.out.println();
}
//非递归算法 ---后根遍历 --双栈
public void posOrderTraverse(BinaryNode<T> root) {
System.out.println("后根遍历(非递归): ");
LinkedStack<BinaryNode<T>> stack1 = new LinkedStack<BinaryNode<T>>(); //创建立空栈1
LinkedStack<BinaryNode<T>> stack2 = new LinkedStack<BinaryNode<T>>(); //创建立空栈1
stack1.push(root);
while (!stack1.isEmpty()) {
BinaryNode<T> p = stack1.pop();
stack2.push(p);
if (p.right != null) {
stack1.push(p.right);
}
if (p.left != null) {
stack1.push(p.left);
}
}
while (!stack2.isEmpty()) {
System.out.print(stack2.pop().data);
}
System.out.println();
}
@Test
public void testIt() {
this.root = new BinaryNode<String>("A");
this.root.left = new BinaryNode("B");
this.root.left.left = new BinaryNode("D");
this.root.left.left.right = new BinaryNode("G");
this.root.right = new BinaryNode("C");
this.root.right.left = new BinaryNode("E");
this.root.right.right = new BinaryNode("F");
this.root.right.right.left = new BinaryNode("H");
this.posOrderTraverse(this.root);
this.inOrderTraverse();
this.preOrderTraverse();
}
}
附赠引用到的两个类的代码:
public class BinaryNode<T> {
public T data;
public BinaryNode<T> left, right;
public BinaryNode(T data, BinaryNode<T> left, BinaryNode<T> right) {
this.data = data;
this.left = left;
this.right = right;
}
public BinaryNode(T data) {
this(data,null,null);
}
public String toString() {
return this.data.toString();
}
public boolean isLeaf() {
return this.left == null && this.right == null;
}
}
package dataDistructure;
import java.util.LinkedList;
import java.util.List;
public class LinkedStack<T> {
private List<T> list;
public LinkedStack() {
this.list = new LinkedList<T>();
}
public boolean isEmpty() {
return this.list.isEmpty();
}
public void push(T t) {
this.list.add(t);
}
public T peek() {
return this.list.get(this.list.size()-1);
}
public T pop() {
T t = this.list.get(this.list.size()-1);
this.list.remove(this.list.size()-1);
return t;
}
}