Preorder árvore binária, em seqüência, depois de travessia DFS (busca em profundidade) pensou; hierarquia travessia BFS (BFS) pensamento. DFS é geralmente implementada usando recursiva ou pilha; implementado com a fila de SFB.
private static List<Node> nodeList=null;
//定义二叉树
private static class Node{
private Node leftChild,rightChild;
private int data;
public Node(int newData){
leftChild=null;
rightChild=null;
data=newData;
}
}
//构建二叉树
public static void createBinaryTree(int[] objs){
nodeList=new ArrayList<Node>();
for(int o:objs){
nodeList.add(new Node(o));
}
for(int i=0;i<objs.length/2;i++){
nodeList.get(i).leftChild=nodeList.get(i*2+1);
if(i*2+2<nodeList.size()) {
nodeList.get(i).rightChild = nodeList.get(i * 2 + 2);
}
}
}
Preorder: root -> subárvore esquerda -> subárvore direita
//先序遍历 递归实现
public static void pre(Node node){
if(node!=null){
System.out.print(node.data+" ");
pre(node.leftChild);
pre(node.rightChild);
}
}
//先序遍历 非递归实现
public static void pre2(Node node){
Stack<Node> stack=new Stack<Node>();
while(node!=null || !stack.empty()){
if(node!=null){
System.out.print(node.data+" ");
stack.push(node);
node=node.leftChild;
}else{
Node tmpNode=stack.pop();
node=tmpNode.rightChild;
}
}
}
Preorder: subárvore esquerda -> root -> subárvore direita
//中序遍历 递归实现
public static void mid(Node node){
if(node!=null){
mid(node.leftChild);
System.out.print(node.data+" ");
mid(node.rightChild);
}
}
//中序遍历 非递归实现
public static void mid2(Node node){
Stack<Node> stack=new Stack<Node>();
while(node!=null || !stack.empty()){
if(node!=null){
stack.push(node);
node=node.leftChild;
}else{
Node tmpNode=stack.pop();
System.out.print(tmpNode.data+" ");
node=tmpNode.rightChild;
}
}
}
Postorder: esquerda sub -> subárvore direita -> root
//后序遍历 递归实现
public static void post(Node node){
if(node!=null){
post(node.leftChild);
post(node.rightChild);
System.out.print(node.data+" ");
}
}
//后序遍历 非递归实现
public static void post2(Node node){
Stack<Node> stack=new Stack<Node>();
Node p=node;
while(node!=null || !stack.empty()){
if(node!=null){
stack.push(node);
node=node.leftChild;
}else{//node==null && !stack.empty()
Node tmpNode=stack.peek().rightChild;
if(tmpNode==null || tmpNode==p){
node=stack.pop();
System.out.print(node.data+" ");
p=node;
node=null;
}else{
node=tmpNode;
}
}
}
}
Atravessar o nível
Java fila são geralmente usados em LinkedList, LinkedList alcançar tanto interface List, ele pode executar uma operação de lista; Deque e implementa a interface, é possível usar quando a fila. Quando usado como uma lista, geralmente usam add / obter o método de push / obter o objeto. Quando usado como uma fila, usando apenas oferta / votação / tirar similares.
//层次遍历
public static void levalOrder(Node node){
if(node==null) return;
Queue<Node> q=new LinkedList<Node>();
q.offer(node);
Node temp=null;
while(!q.isEmpty()){
temp=q.poll();
System.out.print(temp.data+" ");
if(temp.leftChild!=null){
q.offer(temp.leftChild);
}
if(temp.rightChild!=null){
q.offer(temp.rightChild);
}
}
}
teste
public static void main(String[] args) {
int[] node=new int[]{1,2,3,4,5,6};
//创建二叉树
// 1
// / \
// 2 3
// / \ /
// 4 5 6
createBinaryTree(node);
//根节点
Node root=nodeList.get(0);
//先序遍历(根节点->左子树->右子数):1 2 4 5 3 6
System.out.println("先序遍历");
pre2(root);
//中序遍历(左子树->根节点->右子数):4 2 5 1 6 3
System.out.println("\n中序遍历");
mid2(root);
//后序遍历(左子树->右子树->根节点):4 5 2 6 3 1
System.out.println("\n后序遍历");
post2(root);
//层次遍历:1 2 3 4 5 6
System.out.println("\n层次遍历");
levalOrder(root);
}