pat04-树8. Complete Binary Search Tree (30)
04-树8. 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 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)的更多相关文章
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- .Net Core Api 使用版本控制
1,安装Microsoft.AspNetCore.Mvc.Versioning NET Core Mvc中,微软官方提供了一个可用的Api版本控制库Microsoft.AspNetCore.Mvc.V ...
- ABP框架应用-MySQL数据库集成
1. 框架以外依赖包引入 1.1. Pomelo.EntityFrameworkCore.MySql 1.2. Pomelo.EntityFrameworkCore.MySql.Design 2 ...
- html5 canvas绘制矩形和圆形
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- AtCoder Grand Contest 011D(思维,规律,异或)
#include<bits/stdc++.h>using namespace std;char s[200007];int ans[200007];int main(){ int n ...
- django 基础框架学习 (二)
Django框架基础-02 Django缓存cookie 1.说明 当我们服务器在响应数据的同时,希望写⼊⼀些缓存数据到客户端 我们可以选择在响应的同时,将要写⼊到客户端的 ...
- tftp简单文件传输协议搭建
TFTP 简单文件传输协议 安装 sudo apt-get install tftp tftpd openbsd-inetd 需要tftp tftpd openbsd-ine ...
- PAT天梯赛 L1-049 天梯赛座位分配
题目链接:点击打开链接 天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所学校参赛,第 i 所学校有 M[i] ...
- POJ1049 Microprocessor Simulation
题目来源:http://poj.org/problem?id=1049 题目大意: 一种小型的微处理器有以下特性: 1. 每个字长4bit. 2. 地址用2个字进行编码.先高位字后低位字,即高位字大的 ...
- Kibana6.x.x源码分析--启动时basePath
每次启动Kibana时,发现URL都会随机的生成三位字母,比如:[http://localhost:5601/abc/][http://localhost:5601/dzf/]等等,那么这些随机的字符 ...
- 使用Entity Framwork 保存数据时,提示不能在对象中插入重复键,违反了PRIMARY_KEY约束
这种情况,大多发生在有外键存在的情况下,解决方法是: 把dataContext.Set<T>().Add(model)修改成dataContext.Models.Add(model);