PAT04-树6 Complete Binary Search Tree 【JAVA实现】

来源:互联网 发布:投资组合优化模型 编辑:程序博客网 时间:2024/05/18 20:36

一、题目内容

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

    A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

    Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains a positive integerNNN (≤1000\le 10001000). Then NNN distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.

    Output Specification:

    For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

    Sample Input:

    101 2 3 4 5 6 7 8 9 0

    Sample Output:

    6 3 8 1 5 7 9 0 2 4

  • 二、题目分析

    由于是CBT,左子树的所有节点都小于根节点,右子树的所有节点都大于或者等于根节点,当给定完全二叉搜索树的节点总数之后,树的结构就确定了,所以只要对输入的数据进行排序,求出左子树的节点数量就可以得到根节点在排序的序列中的位置,输出根节点,然后再依次对左右子树执行相同操作。

    (1)对输入的序列进行排序;

    (2)计算完全二叉树左子树的节点个数,就可以确定根节点的位置,再对左子树和右子树进行递归操作;


    (3)因为输出数组中就是按照层序遍历的顺序存储的,所以直接输出就可以了。


    三、代码分析


    package struct;import java.util.Arrays;import java.util.Scanner;public class CompleteSearchTree {//全局变量,输入和输出数组public static String input[] = new String[1000];public static String output[] = new String[1000];public static void main(String[] args) {Scanner scan = new Scanner(System.in);String num = scan.next();String[] array = new String[Integer.parseInt(num)];for (int i = 0; i < array.length; i++) {array[i] = scan.next();}Arrays.sort(array);//对输入序列快速排序input = array;solve(0,Integer.parseInt(num)-1,0);display(num);}public static void solve(int left,int right,int Troot){int n = right-left+1;if (n==0) {return;}int root = findRoot(n);output[Troot] = input[left+root];int leftChild = Troot*2+1;//当前节点左孩子的位置int rightChild = leftChild+1;//当前节点右孩子的位置solve(left,left+root-1,leftChild);solve(left+root+1,right,rightChild);}//计算出n个节点的完全二叉树其左子树有多少个节点public static int findRoot(int n){int H = log(n);//计算n个节点的完全二叉搜索树有几个完整的层int X = (int) (n-Math.pow(2, H)+1);//计算完全二叉搜索树的叶节点个数int L = (int) (Math.pow(2, H-1)-1+Math.min(X, Math.pow(2, H-1)));//计算完全二叉搜索的当前节点的左子树节点个数return L;}public static int log(int value){//计算以2为底的对数return (int)Math.floor(Math.log(value)/Math.log(2));}public static void display(String num){for (int i = 0; i < Integer.parseInt(num); i++) {if(i==0){System.out.print(output[i]);}else{System.out.print(" "+output[i]);}}}}


    四、结果展示

    101 2 3 4 5 6 7 8 9 06 3 8 1 5 7 9 0 2 4


    0 0