遍历二叉树

来源:互联网 发布:access连接sql server 编辑:程序博客网 时间:2024/06/07 23:11
package test;
/** 
 * 功能:把一个数组的值存入二叉树中,然后进行遍历  (包括深度优先和广度优先,非递归前根遍历和非递归中根遍历,递归先根遍历,递归中根遍历,递归后根遍历)
 */  
import java.util.LinkedList;  
import java.util.List;  
  


public class Main {  
  
    private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };  //层序遍历
    private static List<Node> nodeList = null;
    
    public static void main(String[] args) {  
        Main binTree = new Main(); 
        //根据array数组构造二叉树
        binTree.createBinTree();  
        // nodeList中第0个索引处的值即为根节点  
        Node root = nodeList.get(0);  
  
        System.out.println("先序(深度优先)遍历:");  
        preOrderTraverse1(root);  
        System.out.println(); 
        
        System.out.println("层序(广度优先)遍历:");  
        levelOrderTraverse(root); 
        System.out.println(); 
        
        System.out.println("先序遍历:");  
        preOrderTraverse2(root);  
        System.out.println(); 
        
        System.out.println("中序遍历:");  
        inOrderTraverse(root);  
        System.out.println(); 
        
        System.out.println("先序递归遍历:");  
        preOrderTraverse3(root);  
        System.out.println(); 
          
        System.out.println("中序递归遍历:");  
        inOrderTraverse2(root);  
        System.out.println();  
  
        System.out.println("后序递归遍历:");  
        postOrderTraverse(root); 
        System.out.println(); 
        
         
    }  
  
    /** 
     * 内部类:节点  
     */  
    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>();  
        // 将一个数组的值依次转换为Node节点  
        for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {  
            nodeList.add(new Node(array[nodeIndex]));  
        }  
        // 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树  
        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);  
        }  
    }  
    /**
     * 深度优先遍历第一种方法:使用栈,出栈的同时右左孩子依次进栈,又叫先根遍历或先序遍历
     * 与下面的与广度优先类似
     */
    
    public static void preOrderTraverse1(Node root) {
    if(root == null)
    return;
    LinkedList<Node> stack = new LinkedList<Node>();
    stack.push(root);
    while(!stack.isEmpty()){
    Node node = stack.pop();
    System.out.print(node.data+" ");
    if(node.rightChild != null){
    stack.push(node.rightChild);
    }
    if(node.leftChild != null){
    stack.push(node.leftChild);
    }
   
    }
    }
    
    /**
     * 广度优先遍历:使用队列,出队列的同时左右孩子依次进队列,又叫层序遍历
     */
    public static void levelOrderTraverse(Node root) {  
            if (root == null) {  
                return;  
            }  
            LinkedList<Node> queue = new LinkedList<>();  
            queue.offer(root);  
            while (!queue.isEmpty()) {  
            Node node = queue.poll();  
                System.out.print(node.data+" ");  
                if (node.leftChild != null) {  
                    queue.offer(node.leftChild);  
                }  
                if (node.rightChild != null) {  
                    queue.offer(node.rightChild);  
                }  
            }  
        } 
    
    /**
     * 深度优先遍历第二种方法:比第一种难记,使用栈,
     * 与下面的中序遍历类似,存储入栈的元素是先序遍历,存储出栈的元素是中序遍历
     */
    public static void preOrderTraverse2(Node root) {  
            LinkedList<Node> stack = new LinkedList<>();  
            Node pNode = root;  
            while (pNode != null || !stack.isEmpty()) {  
                if (pNode != null) {  
                    System.out.print(pNode.data+" ");  
                    stack.push(pNode);  
                    pNode = pNode.leftChild;  
                } else { //pNode == null && !stack.isEmpty()  
                    Node node = stack.pop();  
                    pNode = node.rightChild;  
                }  
            }  
        }
    
    /**
     * 中序遍历
     */
    public static void inOrderTraverse(Node root) {  
            LinkedList<Node> stack = new LinkedList<>();  
            Node pNode = root;  
            while (pNode != null || !stack.isEmpty()) {  
                if (pNode != null) {  
                      
                    stack.push(pNode);  
                    pNode = pNode.leftChild;  
                } else { //pNode == null && !stack.isEmpty()
               
                    Node node = stack.pop();  
                    System.out.print(node.data+" ");
                    pNode = node.rightChild;  
                }  
            }  
        }
    /** 
     * 先序遍历 
     * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已 
     */  
    public static void preOrderTraverse3(Node node) {  
        if (node == null)  
            return;  
        System.out.print(node.data + " ");  
        preOrderTraverse3(node.leftChild);  
        preOrderTraverse3(node.rightChild);  
    }  
  
    /** 
     * 中序遍历 
     * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已 
     */  
    public static void inOrderTraverse2(Node node) {  
        if (node == null)  
            return;  
        inOrderTraverse2(node.leftChild);  
        System.out.print(node.data + " ");  
        inOrderTraverse2(node.rightChild);  
    }  
  
    /** 
     * 后序遍历 
     * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已 
     */  
    public static void postOrderTraverse(Node node) {  
        if (node == null)  
            return;  
        postOrderTraverse(node.leftChild);  
        postOrderTraverse(node.rightChild);  
        System.out.print(node.data + " ");  
    }  
    
     
  
    
  
}
原创粉丝点击