闲来无事,对二叉树的遍历做个总结,下面是使用java对二叉树遍历的各种实现,与大家分享
public class Main {
/**
* 递归先序遍历二叉树
* @author zhangmeng
* @date 2016年9月21日
* @param binaryTree
* @param solver
*/
public static <T> void preTraersal(BinaryTree<T> binaryTree ,BinaryTreeNodeSolver<T> solver){
if(null == binaryTree){
return;
}
solver.solve(binaryTree);
preTraersal(binaryTree.left ,solver);
preTraersal(binaryTree.right ,solver);
}
/**
* 递归中序遍历二叉树
* @author zhangmeng
* @date 2016年9月21日
* @param binaryTree
* @param solver
*/
public static <T> void midTraversal(BinaryTree<T> binaryTree ,BinaryTreeNodeSolver<T> solver){
if(null == binaryTree){
return;
}
preTraersal(binaryTree.left ,solver);
solver.solve(binaryTree);
preTraersal(binaryTree.right ,solver);
}
/**
* 递归后续遍历二叉树
* @author zhangmeng
* @date 2016年9月21日
* @param binaryTree
* @param solver
*/
public static <T> void afterTraversal(BinaryTree<T> binaryTree ,BinaryTreeNodeSolver<T> solver){
if(null == binaryTree){
return;
}
preTraersal(binaryTree.left ,solver);
preTraersal(binaryTree.right ,solver);
solver.solve(binaryTree);
}
/**
* 非递归先序遍历二叉树
* @author zhangmeng
* @date 2016年9月21日
* @param binaryTree
* @param solver
*/
public static <T> void preTraersalNoRecursion(BinaryTree<T> binaryTree ,BinaryTreeNodeSolver<T> solver){
Stack<BinaryTree<T>> stack = new Stack<BinaryTree<T>>();
stack.push(binaryTree);
while(!stack.isEmpty()){
BinaryTree parent = stack.pop();
if(null != parent.right)
stack.push(parent.right);
if(null != parent.left)
stack.push(parent.left);
solver.solve(parent);
}
}
/**
* 非递归中序遍历二叉树
* @author zhangmeng
* @date 2016年9月21日
* @param binaryTree
* @param solver
*/
public static <T> void midTraersalNoRecursion(BinaryTree<T> binaryTree ,BinaryTreeNodeSolver<T> solver){
Stack<BinaryTree<T>> stack1 = new Stack<BinaryTree<T>>();
Stack<BinaryTree<T>> stack2 = new Stack<BinaryTree<T>>();
stack1.push(binaryTree);
while(true){
if(!stack1.isEmpty()){
BinaryTree parent = stack1.pop();
if(null != parent.left)
stack1.push(parent.left);
stack2.push(parent);
}else if(!stack2.isEmpty()){
BinaryTree parent = stack2.pop();
solver.solve(parent);
if(null != parent.right)
stack1.push(parent.right);
}else{
return ;
}
}
}
/**
* 非递归后续遍历二叉树
* @author zhangmeng
* @date 2016年9月21日
* @param binaryTree
* @param solver
*/
public static <T> void afterTraersalNoRecursion(BinaryTree<T> binaryTree ,BinaryTreeNodeSolver<T> solver){
Stack<BinaryTree<T>> stack1 = new Stack<BinaryTree<T>>();
Stack<BinaryTree<T>> stack2 = new Stack<BinaryTree<T>>();
stack1.push(binaryTree);
while(!stack1.isEmpty()){
BinaryTree parent = stack1.pop();
if(null != parent.right)
stack1.push(parent.right);
if(null != parent.left)
stack1.push(parent.left);
stack2.push(parent);
}
while(!stack2.isEmpty()){
BinaryTree parent = stack2.pop();
solver.solve(parent);
}
}
/**
* 分层遍历二叉树
* @author zhangmeng
* @date 2016年9月21日
* @param binaryTree
* @param solver
*/
public static <T> void levelTraersal(BinaryTree<T> binaryTree ,BinaryTreeNodeSolver<T> solver){
Queue<BinaryTree<T>> queue = new ArrayDeque<BinaryTree<T>>();
queue.offer(binaryTree);
while(!queue.isEmpty()){
BinaryTree parent = queue.poll();
solver.solve(parent);
if(null != parent.left)
queue.offer(parent.left);
if(null != parent.right)
queue.offer(parent.right);
}
}
public static void main(String[] args) {
//创建根节点
BinaryTree<Integer> binaryTree = new BinaryTree<Integer>() ;
binaryTree.data=0;
//分层遍历的方式创建二叉树
levelTraersal(binaryTree, new BinaryTreeNodeSolver<Integer>() {
@Override
public void solve(BinaryTree<Integer> tree) {
if(tree.data<10){
tree.left = new BinaryTree<Integer>();
tree.left.data = tree.data*2+1;
tree.right = new BinaryTree<Integer>();
tree.right.data = tree.data*2+2;
}
}
});
BinaryTreeNodeSolver<Integer> solver = new BinaryTreeNodeSolver<Integer>() {
@Override
public void solve(BinaryTree<Integer> tree) {
System.out.print(tree.data+"|");
}
};
System.out.println("===============先序遍历===============");
preTraersal(binaryTree, solver);
System.out.println("\n===============先序遍历(非递归)===============");
preTraersalNoRecursion(binaryTree, solver);
System.out.println("\n===============中序遍历===============");
midTraversal(binaryTree, solver);
System.out.println("\n===============中序遍历(非递归)===============");
midTraersalNoRecursion(binaryTree, solver);
System.out.println("\n===============后序遍历===============");
afterTraversal(binaryTree, solver);
System.out.println("\n===============后序遍历(非递归)===============");
afterTraersalNoRecursion(binaryTree, solver);
System.out.println("\n===============分层遍历===============");
levelTraersal(binaryTree, solver);
}
}
interface BinaryTreeNodeSolver<T>{
public void solve(BinaryTree<T> tree);
}
运行结果:
===============先序遍历===============
0|1|3|7|15|16|8|17|18|4|9|19|20|10|2|5|11|12|6|13|14|
===============先序遍历(非递归)===============
0|1|3|7|15|16|8|17|18|4|9|19|20|10|2|5|11|12|6|13|14|
===============中序遍历===============
1|3|7|15|16|8|17|18|4|9|19|20|10|0|2|5|11|12|6|13|14|
===============中序遍历(非递归)===============
15|7|16|3|17|8|18|1|19|9|20|4|10|0|11|5|12|2|13|6|14|
===============后序遍历===============
1|3|7|15|16|8|17|18|4|9|19|20|10|2|5|11|12|6|13|14|0|
===============后序遍历(非递归)===============
14|13|6|12|11|5|2|10|20|19|9|4|18|17|8|16|15|7|3|1|0|
===============分层遍历===============
0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|