PTA 04-树6 Complete Binary Search Tree (30分)
题目地址
https://pta.patest.cn/pta/test/16/exam/4/question/669
5-7 Complete Binary Search Tree (30分)
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 integer NN (\le 1000≤1000). Then NN 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:
10
1 2 3 4 5 6 7 8 9 0
Sample Output:
6 3 8 1 5 7 9 0 2 4
/*
评测结果
时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2017-06-29 10:45 正在评测 0 5-7 gcc 无 无
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 18/18 1 1
测试点2 答案正确 3/3 2 1
测试点3 答案正确 2/2 1 1
测试点4 答案正确 2/2 1 1
测试点5 答案正确 3/3 1 1
测试点6 答案正确 2/2 3 1 完全二叉搜索树
结构:数组形式存储完全树,可以很方便进行层序遍历
解法:思考后发现完全二叉搜索树的子树也是完全二叉搜索树。故先将接收到的数据排序,递归计算一个节点的左子树和右子树数量,类似于快排,每次可以确定root的值。
然后按顺序打印数组即可。
*/ #include<stdio.h>
#define MAXLEN 2000 int workarray[MAXLEN];
int CBST[MAXLEN+1];//0号元素空着,从1号开始左子树2*root,右子树2*root+1;
int NODECOUNT[MAXLEN+1];
int Len;
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
} void InsertionSort(int A[],int N)
{
int i;
int k;
for(i=1;i<N;i++)
{
int temp=A[i];
k=i;
while(temp < A[k-1])
{
swap(&A[k],&A[k-1]);
k--;
}
A[k]=temp;
}
} void GetInput()
{
int i;
scanf("%d",&Len);
for(i=0;i<Len;i++)
{
scanf("%d",&workarray[i]);
}
} void Output()
{
int i;
for(i=1;i<Len+1;i++)
{
printf("%d",CBST[i]);
if(i!=Len)
printf(" ");
}
}
void Outputcount() //调试用函数
{
int i;
for(i=1;i<Len+1;i++)
{
printf("%d ",NODECOUNT[i]);
}
}
void Outputraw() //调试用函数
{
int i;
for(i=0;i<Len;i++)
{
printf("%d ",workarray[i]);
}
} void CountNodes() //太懒了,数据量不大,直接让它自己数节点数量得了。 逆序算出此节点之下所有子树节点之和
{
int i;
for(i=0;i<Len+1;i++)
{
NODECOUNT[i]=0;//先全体置零
} for(i=Len;i>0;i--)//在1开头的数组中,有效下标范围应该是1~Len
{
NODECOUNT[i/2] += NODECOUNT[i]+1; //父节点的子树,加上本节点的子树,再加上该节点自身
} } void CalcTree(int raw[],int calc[],int left,int index)
{
if (index>Len) return; //越界了,没子树
int raw_position=left+(2*index>Len?0:NODECOUNT[2*index]+1);//注,函数首次调用,left最初传入的是0, 判断是否为叶节点,如果没有左子树,那么它自己就是left
//此处加个注释说明下,一个坐标的构成。left(此递归段偏移起点)+NODECOUNT[2*index](所求的点的左儿子的全部结点数量)+1(该结点本身,即此递归段的root)
calc[index]=raw[raw_position];
CalcTree(raw,calc,left,2*index);//递归左子树
CalcTree(raw,calc,raw_position+1,2*index+1);//递归右子树
} int main()
{
GetInput();
InsertionSort(workarray,Len);
// printf("done sort\n");
// Outputraw();
// printf("\n");
CountNodes();
CalcTree(workarray,CBST,0,1);
Output();
printf("\n");
// Outputcount();
}
PTA 04-树6 Complete Binary Search Tree (30分)的更多相关文章
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 04-树6 Complete Binary Search Tree (30 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 【PAT甲级】1064 Complete Binary Search Tree (30 分)
题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- pat04-树8. Complete Binary Search Tree (30)
04-树8. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHE ...
- pat1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
随机推荐
- 利用uiautomator实现Android移动app启动时间的测试
为了减少因手工测试的反应误差,这里介绍下如何利用Android自带的自动化测试工具uiautomator实现app启动时间的测试. 测试基本思路如下: 1.启动前记录当前的时间戳 2.启动app,直至 ...
- PowerShell和Bash的介绍
PowerShell是运行在windows平台的脚本,而Bash是运行在linux平台的脚本 现在bash能做的事情,PowerShell也能做,PowerShell的强大之处是它可以管理window ...
- Spark SQL catalyst概述和SQL Parser的具体实现
之前已经对spark core做了较为深入的解读,在如今SQL大行其道的背景下,spark中的SQL不仅在离线batch处理中使用广泛,structured streamming的实现也严重依赖spa ...
- AJPFX关于表结构的相关语句
//表结构的相关语句==================================== 建表语句: create table 表名( ...
- 【转】javap -c命令详解
javap -c命令详解 一直在学习Java,碰到了很多问题,碰到了很多关于i++和++i的难题,以及最经典的String str = "abc" 共创建了几个对象的疑难杂症. 知 ...
- iframe及其引出的页面跳转问题
前提:在前一段的工作中碰到了一些页面跳转,子页面跳到父页面上的等等问题,当时页面总是跳不对,或者跳错,要不就是不需要重新打开窗口,却又重新打开一个了,特此搜寻网上各大博客论坛,加上项目经验整理一篇文章 ...
- MySQL 当记录不存在时insert,当记录存在时更新
网上基本有三种解决方法. 第一种: 示例一:insert多条记录 假设有一个主键为 client_id 的 clients 表,可以使用下面的语句: INSERT INTO clients (clie ...
- 在Android上使用酷狗歌词API
参考自http://blog.csdn.net/u010752082/article/details/50810190 代码先贴出来: public void searchLyric(){ final ...
- mkdir touch vim
vim和touch都用于新建文件 mkdir用于新建文件夹
- ADB相关指令实例详解
1.获取物理设备相关信息: adb devices 2.apk安装 2.1 新的应用(该应用未安装) adb -s 设备序列号 install apk路径 2.2 应用已安装,重安装覆盖 adb -s ...