package edu.lnu.fang.BiTree;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
/**
* 二叉树的三种遍历 递归 非递归 +层次遍历
* @author Fangchao
* 2016年12月3日
*/
public class BinTreeTra {
private int[] array = {
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15 };
private static List<Node> nodeList =
null;
/**
* 内部类:节点
*/
private static class Node {
Node leftChild;
Node rightChild;
int data;
Node(
int newData) {
leftChild =
null;
rightChild =
null;
data = newData;
}
}
public void createBinTree() {
nodeList =
new LinkedList<Node>();
for (
int nodeIndex =
0; nodeIndex < array.length; nodeIndex++) {
nodeList.add(
new Node(array[nodeIndex]));
}
for (
int parentIndex =
0; parentIndex < array.length /
2 -
1; parentIndex++) {
nodeList.get(parentIndex).leftChild = nodeList
.get(parentIndex *
2 +
1);
nodeList.get(parentIndex).rightChild = nodeList
.get(parentIndex *
2 +
2);
}
int lastParentIndex = array.length /
2 -
1;
nodeList.get(lastParentIndex).leftChild = nodeList
.get(lastParentIndex *
2 +
1);
if (array.length %
2 ==
1) {
nodeList.get(lastParentIndex).rightChild = nodeList
.get(lastParentIndex *
2 +
2);
}
}
/**
* 先序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @param node
* 遍历的节点
*/
public static void preOrderTraverse(Node node) {
if (node ==
null)
return;
System.out.print(node.data +
" ");
preOrderTraverse(node.leftChild);
preOrderTraverse(node.rightChild);
}
/**
* 中序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @param node
* 遍历的节点
*/
public static void inOrderTraverse(Node node) {
if (node ==
null)
return;
inOrderTraverse(node.leftChild);
System.out.print(node.data +
" ");
inOrderTraverse(node.rightChild);
}
/**
* 后序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @param node
* 遍历的节点
*/
public static void postOrderTraverse(Node node) {
if (node ==
null)
return;
postOrderTraverse(node.leftChild);
postOrderTraverse(node.rightChild);
System.out.print(node.data +
" ");
}
public static void printNode(Node node){
System.out.print(node.data+
" ");
}
/**
* 非递归先序遍历
* @param root
*/
public static void preOrder_stack(Node root){
Stack<Node> stack =
new Stack<Node>();
Node node = root;
while(node !=
null || stack.size()>
0){
if(node !=
null){
printNode(node);
stack.push(node);
node = node.leftChild;
}
else{
node = stack.pop();
node = node.rightChild;
}
}
}
/**
* 非递归 中序遍历
* @param root
*/
public static void inOrder_stack(Node root){
Stack<Node> stack =
new Stack<Node>();
Node node = root;
while(node !=
null || stack.size()>
0){
if(node !=
null){
stack.push(node);
node = node.leftChild;
}
else{
node = stack.pop();
printNode(node);
node = node.rightChild;
}
}
}
/**
* 非递归的 后续遍历
* @param root
*/
public static void postOrder_Stack(Node root){
Stack<Node> stack =
new Stack<Node>();
Stack<Node> output =
new Stack<Node>();
Node node = root;
while(node !=
null || stack.size()>
0){
if(node !=
null){
output.push(node);
stack.push(node);
node = node.rightChild;
}
else{
node = stack.pop();
node = node.leftChild;
}
}
while(output.size()>
0){
printNode(output.pop());
}
}
/**
* 层次遍历
* @param root
*/
static void LevelOrder(Node root){
Queue<Node> queue=
new LinkedList <>();
queue.add(root);
while(queue.size()>
0){
Node p=queue.poll();
printNode(p);
if(p.leftChild!=
null){
queue.add(p.leftChild);
}
if(p.rightChild!=
null){
queue.add(p.rightChild);
}
}
}
public static void main(String[] args) {
BinTreeTra binTree =
new BinTreeTra();
binTree.createBinTree();
Node root = nodeList.get(
0);
System.out.println(
"先序遍历:");
preOrderTraverse(root);
System.out.println();
System.out.println(
"中序遍历:");
inOrderTraverse(root);
System.out.println();
System.out.println(
"后序遍历:");
postOrderTraverse(root);
System.out.println();
System.out.println(
"非递归 先序遍历:");
preOrder_stack(root);
System.out.println();
System.out.println(
"非递归 中序遍历:");
inOrder_stack(root);
System.out.println();
System.out.println(
"非递归后序遍历:");
postOrder_Stack(root);
System.out.println();
System.out.println(
"层次 遍历:");
LevelOrder(root);
System.out.println();
}
}
另一种非递归后序遍历: http://www.cnblogs.com/ybwang/archive/2011/10/04/lastOrderTraverse.html
输出结果:
先序遍历:
1 2 4 8 9 5 10 11 3 6 12 13 7 14 15
中序遍历:
8 4 9 2 10 5 11 1 12 6 13 3 14 7 15
后序遍历:
8 9 4 10 11 5 2 12 13 6 14 15 7 3 1
非递归 先序遍历:
1 2 4 8 9 5 10 11 3 6 12 13 7 14 15
非递归 中序遍历:
8 4 9 2 10 5 11 1 12 6 13 3 14 7 15
非递归后序遍历:
8 9 4 10 11 5 2 12 13 6 14 15 7 3 1
转载请注明原文地址: https://ju.6miu.com/read-970434.html