PTA 是否完全二叉搜索树 Java

PTA 是否完全二叉搜索树 Java

将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果。
输入格式:
输入第一行给出一个不超过20的正整数N;第二行给出N个互不相同的正整数,其间以空格分隔。
输出格式:
将输入的N个正整数顺序插入一个初始为空的二叉搜索树。在第一行中输出结果树的层序遍历结果,数字间以1个空格分隔,行的首尾不得有多余空格。第二行输出YES,如果该树是完全二叉树;否则输出NO。
输入样例1:
9
38 45 42 24 58 30 67 12 51
输出样例1:
38 45 24 58 42 30 12 67 51
YES

输入样例2:
8
38 24 12 45 58 67 42 51
输出样例2:
38 45 24 58 42 12 67 51
NO

利用完全二叉树的性质(从0开始编号)
父亲 = (儿子 -1) / 2
左儿子 = (父亲 + 1)× 2 - 1
右儿子 = (父亲 + 1)× 2
并且可以轻易的确定最后一个节点的下标(节点数量-1)
则如果有一个节点的编号超过了最后一个节点的下标,则表示这不是一棵完全二叉树(即不是一棵完全二叉搜索树)
层序遍历 = 顺序输出数组的值

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        Reader.init(System.in);
        N = Reader.nextInt();
        int[] tree = new int[4194303];
        /*
         * fa = (child - 1) / 2 left_child = (fa + 1) * 2 - 1 right_child = (fa + 1) * 2
         */
        for (int i = 0; i < N; i++) {
            int node = Reader.nextInt();
            insert(tree, node);
        }
        show(tree);
        if(isOK) {
            System.out.println("YES");
        }else {
            System.out.println("NO");
        }
    }

    static int N;
    static boolean isOK = true;

    static void insert(int[] tree, int node) {
        int current = 0;
        while (tree[current] != 0) {
            if (node < tree[current]) {
                current = (current + 1) * 2;
            } else {
                current = (current + 1) * 2 - 1;
            }
        }
        if (current >= N) {
            isOK = false;
        }
        tree[current] = node;
    }

    static void show(int[] tree) {
        int count = 0;
        for (int i = 0; i < tree.length; i++) {
            if(tree[i] != 0) {
                count++;
                if(count == N) {
                    System.out.println(tree[i]);
                    break;
                }
                System.out.print(tree[i] + " ");
            }
        }
        
    }

}

// Class for buffered reading int and double values *//*
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    // ** call this method to initialize reader for InputStream *//*
    static void init(InputStream input) {
        reader = new BufferedReader(new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    // ** get next word *//*
    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            // TODO add check for eof if necessary
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static String nextLine() throws IOException {
        return reader.readLine();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static char nextChar() throws IOException {
        return next().toCharArray()[0];
    }

    static float nextFloat() throws IOException {
        return Float.parseFloat(next());
    }
}

发布了27 篇原创文章 · 获赞 2 · 访问量 1476

猜你喜欢

转载自blog.csdn.net/Samil_Hy/article/details/104197500
今日推荐