04-树6 Complete Binary Search Tree
admin
2024-04-08 02:29:35
0

04-树6 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

代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
C++ (g++)

思路:

根据二叉搜索树的性质,如果知道根结点左子树的结点个数,那么就知道根结点应该放置什么元素。而左子树结点的个数可以依靠完全二叉树的性质得到。(确定了结点个数,完全二叉树的结构就一定了)。
所以,先将输入的序列进行排序。然后输入序列一共有10个元素,那么完全二叉树的根结点的左子树一定含有6个结点(4层,3层为满的,共1+2+4=7个 左子树还剩3 个 3+1+2=6个结点),即根结点是从小到大排第七位的那个数字,也就是6,同时也可以确定后面的三个数字(7,8,9)一定是属于根节点的右子树的。
输入序列:1234567890
排好序:0123456789
可知,6是根结点,同理递归地填左子树和右子树(左子树和右子树也是完全二叉树)。先左子树部分,因为是完全二叉树,所以左子树的根结点的左边一定有3个结点,右边有两个结点,即是说0 1 2 3 4 5 6 7 8 9,3是左子树的根结点,依次类推。
填充数字时是先序遍历的应用。
假设调用核心算法之前,输入序列已经放到了数组A中,并对数组A做了一个从小到大的排列。
结果生成的树也存在一个数组里,叫做T。TRoot对应的是当前我们要考虑的这棵子树的根结点所在的位置。

solve函数要完成:从A中选出正确的数字,填到T[TRoot]中。

排序: qsort(A, N, sizeof(int), compare);
A 待排序序列的首元素地址
N 待排序序列的长度
sizeof(int) 要排序的元素的大小
compare 函数名称,可以叫做其他名称,功能是比较两个元素的大小

计算左子树的规模
H=log(N+1)
2^H-1+x=N
x要在x和pow(2,H-1)之间取最小
要计算左子树的规模。当有H层的时候,节点总数为2^H-1,那么计算左子树的规模的时候,因为也是完全二叉树,少了一层,节点个数就是2 ^(H-1)-1。
L=pow(2,H-1)-1+x

AC代码:

#include;
using namespace std;int *A;
int *T;int min(int a,int b)
{return a>b?b:a;
}int GetLeftLength(int n)
{int h=log2(n+1);int x=n+1-pow(2,h);x=min(x,pow(2,h-1));int L=pow(2,h-1)-1+x;return L;
}void solve(int ALeft,int ARight,int TRoot)
{//初始调用为(0,N-1,0)int n=ARight-ALeft+1;//数组A中一段数字的个数if(n==0) return;int  L=GetLeftLength(n);//计算出n个结点的树及其左子树有多少个结点T[TRoot]=A[ALeft+L];int LeftRoot=TRoot*2+1;//因为数组T中是从第0个位置开始存储的元素,所以根结点为TRoot,那么左孩子的下标就为TRoot*2+1int RightRoot=LeftRoot+1;solve(ALeft,ALeft+L-1,LeftRoot);solve(ALeft+L+1,ARight,RightRoot);
}int compare(const void *a,const void *b)
{return *(int*)a-*(int*)b;
}int main()
{int N;cin>>N;A=(int *)malloc(sizeof(int)*N);T=(int *)malloc(sizeof(int)*N);for(int i=0;i>A[i];qsort(A,N,sizeof(int),compare);//将A数组排好顺序solve(0,N-1,0);for(int  i=0;i

相关内容

热门资讯

【MySQL】锁 锁 文章目录锁全局锁表级锁表锁元数据锁(MDL)意向锁AUTO-INC锁...
【内网安全】 隧道搭建穿透上线... 文章目录内网穿透-Ngrok-入门-上线1、服务端配置:2、客户端连接服务端ÿ...
GCN的几种模型复现笔记 引言 本篇笔记紧接上文,主要是上一篇看写了快2w字,再去接入代码感觉有点...
数据分页展示逻辑 import java.util.Arrays;import java.util.List;impo...
Redis为什么选择单线程?R... 目录专栏导读一、Redis版本迭代二、Redis4.0之前为什么一直采用单线程?三、R...
【已解决】ERROR: Cou... 正确指令: pip install pyyaml
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
Lock 接口解读 前置知识点Synchronized synchronized 是 Java 中的关键字,...
Win7 专业版安装中文包、汉... 参考资料:http://www.metsky.com/archives/350.htm...
3 ROS1通讯编程提高(1) 3 ROS1通讯编程提高3.1 使用VS Code编译ROS13.1.1 VS Code的安装和配置...
大模型未来趋势 大模型是人工智能领域的重要发展趋势之一,未来有着广阔的应用前景和发展空间。以下是大模型未来的趋势和展...
python实战应用讲解-【n... 目录 如何在Python中计算残余的平方和 方法1:使用其Base公式 方法2:使用statsmod...
学习u-boot 需要了解的m... 一、常用函数 1. origin 函数 origin 函数的返回值就是变量来源。使用格式如下...
常用python爬虫库介绍与简... 通用 urllib -网络库(stdlib)。 requests -网络库。 grab – 网络库&...
药品批准文号查询|药融云-中国... 药品批文是国家食品药品监督管理局(NMPA)对药品的审评和批准的证明文件...
【2023-03-22】SRS... 【2023-03-22】SRS推流搭配FFmpeg实现目标检测 说明: 外侧测试使用SRS播放器测...
有限元三角形单元的等效节点力 文章目录前言一、重新复习一下有限元三角形单元的理论1、三角形单元的形函数(Nÿ...
初级算法-哈希表 主要记录算法和数据结构学习笔记,新的一年更上一层楼! 初级算法-哈希表...
进程间通信【Linux】 1. 进程间通信 1.1 什么是进程间通信 在 Linux 系统中,进程间通信...
【Docker】P3 Dock... Docker数据卷、宿主机与挂载数据卷的概念及作用挂载宿主机配置数据卷挂载操作示例一个容器挂载多个目...