树的非递归代码(Java实现)
建树:
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
public class TreeBuilder {
/**
* 从文件读取数据按层次遍历的方式创建一个二叉树 (测试用)
* 文件输入只能是'#'或数字,'#'表示null
* 测试用例1: 1 2 3 4 5 # # 6 # 7 9
* 测试用例2: 1 2 3 4 5
*
* @return
*/
public TreeNode createTree(Scanner in) {
String val = in.next();
if (val.equals("#")) {
return null;
}
TreeNode node = new TreeNode<>(Integer.valueOf(val));
TreeNode root = node;
Queue> queue = new ArrayDeque<>();
queue.add(node);
while (!queue.isEmpty()) {
int len = queue.size();
for (int i = 0; i < len; i++) {
node = queue.remove();
if (!in.hasNext()) {
break;
}
val = in.next();
if (val.equals("#")) {
node.left = null;
} else {
node.left = new TreeNode<>(Integer.valueOf(val));
queue.add(node.left);
}
if (!in.hasNext()) {
break;
}
val = in.next();
if (val.equals("#")) {
node.right = null;
} else {
node.right = new TreeNode<>(Integer.valueOf(val));
queue.add(node.right);
}
}
}
return root;
}
}
树节点定义:
public class TreeNode {
TreeNode(T val) {
this.val = val;
}
T val;
TreeNode left;
TreeNode right;
}
层次遍历、前中后遍历的代码:
import java.util.*;
public class TreeIter {
/**
* 层次遍历,用队列实现
*
* @param root
* @return
*/
public List levelOrder(TreeNode root) {
List res = new ArrayList<>();
if (root == null) {
return res;
}
Queue> queue = new ArrayDeque<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.remove();
res.add(node.val);
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
}
return res;
}
/**
* 前序遍历非递归
*
* @param root
* @return
*/
public List preOrder(TreeNode root) {
List res = new ArrayList<>();
if (root == null) {
return res;
}
TreeNode curNode = root;
Stack> stack = new Stack<>();
while (curNode != null || !stack.isEmpty()) {
while (curNode != null) {
stack.add(curNode);
res.add(curNode.val);
curNode = curNode.left;
}
curNode = stack.pop();
curNode = curNode.right;
}
return res;
}
/**
* 中序遍历非递归
*
* @param root
* @return
*/
public List midOrder(TreeNode root) {
List res = new ArrayList<>();
if (root == null) {
return res;
}
Stack> stack = new Stack<>();
TreeNode curNode = root;
while (curNode != null || !stack.isEmpty()) {
while (curNode != null) {
stack.push(curNode);
curNode = curNode.left;
}
curNode = stack.pop();
res.add(curNode.val);
curNode = curNode.right;
}
return res;
}
/**
* 后序遍历非递归(双栈法,好理解)
*
* @param root
* @return
*/
public List postOrder(TreeNode root) {
List res = new ArrayList<>();
if (root == null) {
return res;
}
Stack> stackRes = new Stack<>();
Stack> stackTmp = new Stack<>();
stackTmp.push(root);
TreeNode curNode;
while (!stackTmp.isEmpty()) {
curNode = stackTmp.pop();
stackRes.push(curNode);
if (curNode.left != null) {
stackTmp.add(curNode.left);
}
if (curNode.right != null) {
stackTmp.add(curNode.right);
}
}
while (!stackRes.isEmpty()) {
res.add(stackRes.pop().val);
}
return res;
}
/**
* 后序遍历非递归
*
* 参考链接,注释非常详细
* http://www.cnblogs.com/ybwang/archive/2011/10/04/lastOrderTraverse.html
*
* @param root
* @return
*/
public List postOrder2(TreeNode root) {
List res = new ArrayList<>();
if (root == null) {
return res;
}
Stack> stack = new Stack<>();
stack.push(root);
TreeNode curNode = null;
TreeNode preNode = null;
while (!stack.isEmpty()) {
curNode = stack.peek();
if (preNode == null || preNode.left == curNode || preNode.right == curNode) {
if (curNode.left != null) {
stack.push(curNode.left);
} else if (curNode.right != null) {
stack.push(curNode.right);
} else {
res.add(stack.pop().val);
}
} else if (curNode.left == preNode) {
if (curNode.right != null) {
stack.push(curNode.right);
} else {
res.add(stack.pop().val);
}
} else if (curNode.right == preNode) {
res.add(stack.pop().val);
}
preNode = curNode;
}
return res;
}
}
测试用例:
input.txt
1 2 3 4 5 # # 6 # 7 9
测试代码:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
TreeIter treeIter = new TreeIter<>();
Scanner in = new Scanner(new FileInputStream("input.txt"));
TreeNode root = new TreeBuilder().createTree(in);
List preOrder = treeIter.preOrder(root);
System.out.println(preOrder);
List midOrder = treeIter.midOrder(root);
System.out.println(midOrder);
List postOrder = treeIter.postOrder(root);
System.out.println(postOrder);
List postOrder2 = treeIter.postOrder2(root);
System.out.println(postOrder2);
List levelOrder = treeIter.levelOrder(root);
System.out.println(levelOrder);
}
}