按层打印二叉树

Posted wakingshaw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了按层打印二叉树相关的知识,希望对你有一定的参考价值。

二叉树的遍历,无非就是按层遍历,先序遍历,中序遍历和后序遍历这几种。其中后三种的先,中,后等等是相对于根节点而言的。给出一棵二叉树,我们可以按照相对应的规则去输出它的遍历序列,同样的,如果满足一定的条件,那么也可以通过给出的序列来还原相对应的二叉树。

以满二叉树为例,如下图:(略丑,将就看看)

 

技术图片

这棵二叉树的中序遍历为:D -> B -> E -> A -> F -> C -> G

可以发现对于每棵子树,根节点肯定是在中间的位置,而左右孩子则分居左右。通过这个规律,我们可以通过给出的中序遍历序列,来还原这一棵二叉树:

以上述序列为例,先求序列中点,则可确定根节点;然后不断分割成左右子树,继续做相同的工作,则可不断求出每棵子树的根节点,一直到叶子为止。可通过把每次求出来的“根”节点加入到队列之中,来保证还原该二叉树时的节点顺序。

import java.util.Scanner;

public class printBinaryTree {
    static class Tree{
        int left;
        int right;
        int idx;
        int val;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int K = in.nextInt();   //the level of tree(<=10)
        int[] counts = {0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023};
        int[] nodes = new int[counts[K]]; //inorder traversal
        boolean[] visited = new boolean[counts[K]]; 
        for(int i = 0; i < counts[K]; i++) {
            nodes[i] = in.nextInt();
        }
        for(int j = 0; j < counts[K]; j++) {
            visited[j] = false;
        }
        operate(nodes, counts, visited, K);
        
    }
    
    public static void operate(int[] nodes, int[] counts, boolean[] visited, int k) {
        Tree root = new Tree();
        int mid = counts[k]/2;
        root.idx = mid;
        root.left = 0;
        root.right = counts[k]-1;
        root.val = nodes[mid];
        visited[mid] = true;
        Tree[] queue = new Tree[counts[k]];  
        int head = 0; //the head of the queue
        int tail = 0; //the tail of the queue
        queue[tail++] = root;  // enter the queue
        int times = 0; 
        int target = 1;
        while(head < tail) {
            Tree t = queue[head++];
            //print the tree by level
            System.out.print(t.val+ " ");
            times++;
            if(times == target) {
                System.out.println();
                times = 0;
                target *= 2;
            }
            //add left child
            Tree leftChild = new Tree();
            leftChild.left = t.left;
            leftChild.right = t.idx-1;
            leftChild.idx = (leftChild.left+leftChild.right)/2;
            leftChild.val = nodes[leftChild.idx];
            if(!visited[leftChild.idx]) {
                visited[leftChild.idx] = true;
                queue[tail++] = leftChild;
            }
            //add right child
            Tree rightChild = new Tree();
            rightChild.left = t.idx+1;
            rightChild.right = t.right;
            rightChild.idx = (rightChild.left+rightChild.right)/2;
            rightChild.val = nodes[rightChild.idx];
            if(!visited[rightChild.idx]) {
                visited[rightChild.idx] = true;
                queue[tail++] = rightChild;
            }
        }
        
    }

}

 

以上是关于按层打印二叉树的主要内容,如果未能解决你的问题,请参考以下文章

按层打印二叉树

按层打印二叉树

二叉树按层打印

按层打印二叉树

从上到下按层打印二叉树,每层打印顺序从左到右

62.把二叉树打印成多行