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:
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 1000≤1000). 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
二、题目分析
(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
- PAT04-树6 Complete Binary Search Tree 【JAVA实现】
- PAT1064:Complete Binary Search Tree (Java实现)
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- Complete Binary Search Tree
- Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree (30分)
- PTA 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree (30分)
- 04-树6 Complete Binary Search Tree (30分)
- 04-树6 Complete Binary Search Tree (30分)
- mysql字符串包含的四种写法
- 软工文档总结
- Android:通过JNI调用c++代码
- Run Time中请求系统权限Requesting Permissions at Run Time
- 一行代码实现java list去重
- PAT04-树6 Complete Binary Search Tree 【JAVA实现】
- File操作-RandomAccessFile
- CentOS7与MySQL
- Android 并发二三事之AsyncTask
- 《iOS移动开发从入门到精通》图书连载5:Xcode 8的使用(上)
- EditText的部分操作
- 内网一键生成 LetsEncrypt HTTPS证书 - 3 - 生成证书
- C++11语言可用性的强化
- 自定义控件无法直接添加到axml解决方法