04-树8. Complete Binary Search Tree (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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 N (<=1000). Then N 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

提交代码

解法一:

数组做法:

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<string>
using namespace std;
int tree[],endtree[];
int GetLeftLength(int n){
int h=log(n+)/log();//除去最后一排的元素
int x=n+-pow(,h);
if(x>=pow(,h-)){
x=pow(,h-);
}
return x+pow(,h-)-;
}
void solve(int left,int right,int root){
int n=right-left+;
if(!n) return;
int l=GetLeftLength(n);
endtree[root]=tree[left+l];
solve(left,left+l-,root*+);
solve(left+l+,right,root*+);
}
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n;
scanf("%d",&n);
int i;
for(i=;i<n;i++){
scanf("%d",&tree[i]);
}
sort(tree,tree+n);
solve(,n-,);
printf("%d",endtree[]);
for(i=;i<n;i++){
printf(" %d",endtree[i]);
}
printf("\n");
return ;
}

方法二:

链表做法:

递归建树的思想:想找出当前的中间大的树,作为当前树根,然后遍历左子树,右子树,最后对所建的BST进行层序遍历。

当前元素总个数为n,则层数k为ceil(log2(n+1)):

1.n>=3*2^(k-2),即查找树的最后一个元素位于根的右子树部分。则此时右子树有 n-(3*2^(k-2)-1)+2^(k-2)-1 个元素,左边有 n-右子树元素个数-1=n-(n-(3*2^(k-2)-1)+2^(k-2)-1)-1=2^(k-1)-1个元素,则中间元素下标为 2^(k-1)-1 (从0开始)。

2.n<3*2^(k-2),即查找树的最后一个元素位于根的左子树部分。则此时右子树有 2^(k-2)-1 个元素,左边有 n-右子树元素个数-1=n-(2^(k-2)-1)-1=n-2^(k-2) 个元素,则中间元素下标为 n-2^(k-2) (从0开始)。

 #include<cstdio>
#include<functional>
#include<queue>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
int mem[];
struct node{
int v;
node *l,*r;
node(){
l=r=NULL;
}
};
void CBTBuild(int *mem,int n,int mid,node *&p){
//q.push(mem[mid]);
p=new node();
p->v=mem[mid];
//cout<<mem[mid]<<endl; if(mid->=){//left tree
int nn=mid;
int k=ceil(log(nn+)/log());
if(nn>=*pow(,k-)){//超过一半
CBTBuild(mem,nn,pow(,k-)-,p->l);
}
else{//未超过一半
CBTBuild(mem,nn,nn-pow(,k-),p->l);
}
}
if(mid+<n){//right tree
int nn=n-(mid+);
int k=ceil(log(nn+)/log());
if(nn>=*pow(,k-)){//超过一半
CBTBuild(mem+mid+,nn,pow(,k-)-,p->r);
}
else{//未超过一半
CBTBuild(mem+mid+,nn,nn-pow(,k-),p->r);
}
}
}
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n,i,j,k;
queue<node> q;
scanf("%d",&n);
for(i=;i<n;i++){
scanf("%d",&mem[i]);
}
sort(mem,mem+n); /*for(i=0;i<n;i++){
cout<<mem[i]<<endl;
}*/
node *h;
k=ceil(log(n+)/log());
if(n>=*pow(,k-)){//超过一半
CBTBuild(mem,n,pow(,k-)-,h); //cout<<mem[int(pow(2,k-1)-1)]<<endl; }
else{//未超过一半
CBTBuild(mem,n,n-pow(,k-),h); //cout<<mem[int(n-pow(2,k-2))]<<endl; }
int top;
node p=*h;
q.push(p);
printf("%d",p.v);
while(!q.empty()){
p=q.front();
q.pop();
if(p.l!=NULL){
q.push(*(p.l));
printf(" %d",p.l->v);
}
if(p.r!=NULL){
q.push(*(p.r));
printf(" %d",p.r->v);
}
}
printf("\n");
return ;
}

pat04-树8. Complete Binary Search Tree (30)的更多相关文章

  1. PAT题库-1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  2. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

  3. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  4. pat1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  5. pat 甲级 1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  6. 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 ...

  7. 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

    题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...

  8. PAT Advanced 1064 Complete Binary Search Tree (30) [⼆叉查找树BST]

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

  9. 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)

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

随机推荐

  1. vue 项目接口管理

    在vue开发中,会涉及到很多接口的处理,当项目足够大时,就需要定义规范统一的接口,如何定义呢? 方法可能不只一种,本文使用axios+async/await进行接口的统一管理. 本文使用vue-cli ...

  2. C# Linq及Lamda表达式实战应用之 GroupBy 分组统计

    在项目中做统计图表的时候,需要对查询出来的列表数据进行分组统计,首先想到的是避免频繁去操作数据库可以使用 Linq eg: //例如对列表中的Cu元素进行按年GroupBy分组统计 //包含年份,平均 ...

  3. UWP&WP8.1 基础控件—TextBlock和TextBox

    TextBlock:文本展示控件,有着强大的功能 TextBox:文本输入控件. 这两个控件是最为常用的基础控件. TextBlock 基础用法: 打开一个UWP项目,在XAML设计页面你可以从工具箱 ...

  4. 前端的异步解决方案之Promise和Await-Async

    异步编程模式在前端开发过程中,显得越来越重要.从最开始的XHR到封装后的Ajax都在试图解决异步编程过程中的问题.随着ES6新标准的出来,处理异步数据流的解决方案又有了新的变化.Promise就是这其 ...

  5. soj 131 找题

    soj 131 找题 给出两个长度为n,都含k个1的字符串A,B.现在令\(a_1,a_2,\dots,a_k\)是A中1的下标,\(b_1,b_2,\dots,b_k\)是B中1的下表,然后将a,b ...

  6. 青橙 A1255. 拉拉队排练(陶文博)

    A1255. 拉拉队排练(陶文博) 时间限制:1.0s   内存限制:512.0MB   总提交次数:   AC次数:   平均分:   将本题分享到:        查看未格式化的试题   提交   ...

  7. shared_ptr 和auto_ptr智能指针

    shared_ptr:计数的智能指针 它是一个包装了new操作符在堆上分配的动态对象,但它实现的是引用计数型的智能指针 ,可以被自由地拷贝和赋值,在任意的地方共享它,当没有代码使用(引用计数为0)它时 ...

  8. 【模板】文艺平衡树(Splay) 区间翻转 BZOJ 3223

    您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 N,M<= ...

  9. Qt 学习之路 2(12):菜单栏、工具栏和状态栏

    Home / Qt 学习之路 2 / Qt 学习之路 2(12):菜单栏.工具栏和状态栏 Qt 学习之路 2(12):菜单栏.工具栏和状态栏  豆子  2012年9月10日  Qt 学习之路 2  2 ...

  10. Qt 学习之路 2(7):MainWindow 简介

    Qt 学习之路 2(7):MainWindow 简介  豆子  2012年8月29日  Qt 学习之路 2  29条评论 前面一篇大致介绍了 Qt 各个模块的相关内容,目的是对 Qt 框架有一个高屋建 ...