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:

  1. 10
  2. 1 2 3 4 5 6 7 8 9 0

Sample Output:

  1. 6 3 8 1 5 7 9 0 2 4

提交代码

解法一:

数组做法:

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<iostream>
  4. #include<cstring>
  5. #include<queue>
  6. #include<vector>
  7. #include<cmath>
  8. #include<string>
  9. using namespace std;
  10. int tree[],endtree[];
  11. int GetLeftLength(int n){
  12. int h=log(n+)/log();//除去最后一排的元素
  13. int x=n+-pow(,h);
  14. if(x>=pow(,h-)){
  15. x=pow(,h-);
  16. }
  17. return x+pow(,h-)-;
  18. }
  19. void solve(int left,int right,int root){
  20. int n=right-left+;
  21. if(!n) return;
  22. int l=GetLeftLength(n);
  23. endtree[root]=tree[left+l];
  24. solve(left,left+l-,root*+);
  25. solve(left+l+,right,root*+);
  26. }
  27. int main(){
  28. //freopen("D:\\INPUT.txt","r",stdin);
  29. int n;
  30. scanf("%d",&n);
  31. int i;
  32. for(i=;i<n;i++){
  33. scanf("%d",&tree[i]);
  34. }
  35. sort(tree,tree+n);
  36. solve(,n-,);
  37. printf("%d",endtree[]);
  38. for(i=;i<n;i++){
  39. printf(" %d",endtree[i]);
  40. }
  41. printf("\n");
  42. return ;
  43. }

方法二:

链表做法:

递归建树的思想:想找出当前的中间大的树,作为当前树根,然后遍历左子树,右子树,最后对所建的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开始)。

  1. #include<cstdio>
  2. #include<functional>
  3. #include<queue>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<iostream>
  7. using namespace std;
  8. int mem[];
  9. struct node{
  10. int v;
  11. node *l,*r;
  12. node(){
  13. l=r=NULL;
  14. }
  15. };
  16. void CBTBuild(int *mem,int n,int mid,node *&p){
  17. //q.push(mem[mid]);
  18. p=new node();
  19. p->v=mem[mid];
  20. //cout<<mem[mid]<<endl;
  21.  
  22. if(mid->=){//left tree
  23. int nn=mid;
  24. int k=ceil(log(nn+)/log());
  25. if(nn>=*pow(,k-)){//超过一半
  26. CBTBuild(mem,nn,pow(,k-)-,p->l);
  27. }
  28. else{//未超过一半
  29. CBTBuild(mem,nn,nn-pow(,k-),p->l);
  30. }
  31. }
  32. if(mid+<n){//right tree
  33. int nn=n-(mid+);
  34. int k=ceil(log(nn+)/log());
  35. if(nn>=*pow(,k-)){//超过一半
  36. CBTBuild(mem+mid+,nn,pow(,k-)-,p->r);
  37. }
  38. else{//未超过一半
  39. CBTBuild(mem+mid+,nn,nn-pow(,k-),p->r);
  40. }
  41. }
  42. }
  43. int main(){
  44. //freopen("D:\\INPUT.txt","r",stdin);
  45. int n,i,j,k;
  46. queue<node> q;
  47. scanf("%d",&n);
  48. for(i=;i<n;i++){
  49. scanf("%d",&mem[i]);
  50. }
  51. sort(mem,mem+n);
  52.  
  53. /*for(i=0;i<n;i++){
  54. cout<<mem[i]<<endl;
  55. }*/
  56. node *h;
  57. k=ceil(log(n+)/log());
  58. if(n>=*pow(,k-)){//超过一半
  59. CBTBuild(mem,n,pow(,k-)-,h);
  60.  
  61. //cout<<mem[int(pow(2,k-1)-1)]<<endl;
  62.  
  63. }
  64. else{//未超过一半
  65. CBTBuild(mem,n,n-pow(,k-),h);
  66.  
  67. //cout<<mem[int(n-pow(2,k-2))]<<endl;
  68.  
  69. }
  70. int top;
  71. node p=*h;
  72. q.push(p);
  73. printf("%d",p.v);
  74. while(!q.empty()){
  75. p=q.front();
  76. q.pop();
  77. if(p.l!=NULL){
  78. q.push(*(p.l));
  79. printf(" %d",p.l->v);
  80. }
  81. if(p.r!=NULL){
  82. q.push(*(p.r));
  83. printf(" %d",p.r->v);
  84. }
  85. }
  86. printf("\n");
  87. return ;
  88. }

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. C# 写 LeetCode easy #14 Longest Common Prefix

    14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  2. 【bzoj4939】【YNOI2016】掉进兔子洞(莫队)

    题目描述 您正在打galgame,然后突然发现您今天太颓了,于是想写个数据结构题练练手: 一个长为 n 的序列 a. 有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个 ...

  3. 缩点【洛谷P1262】 间谍网络

    [洛谷P1262] 间谍网络 题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他 ...

  4. 浅谈python web框架django2.x

    1.Django简介 Python下有多款不同的 Web 框架,Django是最有代表性的一种.许多成功的网站和APP都基于Django. Django是一个开源的Web应用框架,由Python写成. ...

  5. Leetcode 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)

    题目描述 已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益 测试样例 Input: [7, 1, 5, 3, 6, 4] Output: 5 最大收 ...

  6. CF1101C Division and Union 线段相交问题

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #i ...

  7. 去除IDEA报黄色/灰色的重复代码的下划波浪线

    解决方法: File---->Settings

  8. java Response 设置中文编码

    response.setHeader("Content-type", "text/html;charset=UTF-8"); response.setChara ...

  9. 数据恢复(Data recovery)

    定义数据恢复: 当存储介质出现损伤或由于人员误操作.操作系统故障本身故障所造成的数据不可见,无法读取.丢失. 工程师通过特殊的手段读取却在正常状态下不可见,不可读,无法读的数据. 数据恢复(Data ...

  10. Install ElasticSearch plugin for head

    git clone git://github.com/mobz/elasticsearch-head.git yum install git npm cd elasticsearch-head npm ...