PAT甲级-1064. Complete Binary Search Tree (30)

本文介绍如何通过给定的一组非负整数构造一个既为二叉搜索树又为完全二叉树的数据结构,并输出该树的层次遍历序列。文章详细解释了使用的数据结构和算法,包括vector容器的操作、sort排序函数的应用以及二叉搜索树的递归构建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> v, tree;
int k = 0, n;
void build(int i)//构建二叉搜索树
{
	if (i <= n)
	{
		build(2 * i);
		tree[i] = v[k++];
		build(2 * i + 1);
	}
}
int main(/*int argc, char const *argv[]*/)
{
	cin >> n;
	v.resize(n);
	tree.resize(n + 1);
	for (int i = 0; i < n; ++i) cin >> v[i];
	sort(v.begin(), v.end());
	build(1);
	for (int i = 1; i < tree.size(); ++i)
		i == tree.size() - 1 ? cout << tree[i] : cout << tree[i] << " ";
	system("pause");
	return 0;
}

1.vector容器

作用:它能够像容器一样存放各种类型的对象,简单的说,vector是一个能够存放任意类型的动态数组,能够增加和压缩内容。

vec.begin()//指向迭代器中第一个元素。   
vec.end()//指向迭代器中末端元素的下一个,指向一个不存在元素。          
vec.push_back(elem)     //在尾部加入一个数据。  
vec.pop_back()          //删除最后一个数据。  
vec.capacity()  //vector可用空间的大小。  
vec.size()//返回容器中数据个数。  
vec.empty() //判断容器是否为空。  
vec.front()     //传回第一个数据。  
vec.back()  //传回最后一个数据,不检查这个数据是否存在。  
vec.at(index)   //传回索引idx所指的数据,如果idx越界,抛出out_of_range。  
vec.clear() //移除容器中所有数据。  
vec.erase(iterator) //删除pos位置的数据,传回下一个数据的位置。  
vec.erase(begin,end)    //删除[beg,end)区间的数据,传回下一个数据的位置。注意:begin和end为iterator  
vec.insert(position,elem)   //在pos位置插入一个elem拷贝,传回新数据位置。  
vec.insert(position,n,elem) //在pos位置插入n个elem数据,无返回值。  
vec.insert(position,begin,end)  //在pos位置插入在[beg,end)区间的数据,无返回值。  

assign函数原型及功能:  
void assign(const_iterator first,const_iterator last); //功能:将区间[first,last)的元素赋值到当前的vector中,当前vector会清除掉容器中之前的内容。  
void assign(size_type n,const T& x = T()); //功能:赋n个值为x的元素到当前vector中,当前vector会清除掉容器中之前的内容。  

vec.rbegin()//传回一个vector的最后一个数据的指针。  
vec.rend()// 传回一个vector的第一个数据前一个位置的指针。

vec.resize(num)//重新指定vector的长度。  
vec.resize(num,value)//重新指定vector的长度。并设定新增的元素的值  


2.resize()和reverse()

1、resize(n) 
调整容器的长度大小,使其能容纳n个元素。
如果n小于容器的当前的size,则删除多出来的元素。
否则,添加采用值初始化的元素。
2、 resize(n,t)
多一个参数t,将所有新添加的元素初始化为t。


而reserver()的用法只有一种

reserve(n)

预分配n个元素的存储空间。


了解这两个函数的区别,首先要搞清楚容器的capacity(容量)与size(长度)的区别。
size指容器当前拥有的元素个数;
而capacity则指容器在必须分配新存储空间之前可以存储的元素总数。
也可以说是预分配存储空间的大小。
resize()函数和容器的size息息相关。调用resize(n)后,容器的size即为n。
至于是否影响capacity,取决于调整后的容器的size是否大于capacity。
reserve()函数和容器的capacity息息相关。
调用reserve(n)后,若容器的capacity<n,则重新分配内存空间,从而使得capacity等于n。
如果capacity>=n呢?capacity无变化。
从两个函数的用途可以发现,容器调用resize()函数后,所有的空间都已经初始化了,所以可以直接访问。
而reserve()函数预分配出的空间没有被初始化,所以不可访问。

3.sort

STL中就自带了排序函数sort。
sort 对给定区间所有元素进行排序  要使用此函数只需用#include <algorithm> sort即可使用,语法描述为:
sort(begin,end),表示一个范围,例子:#include <algorithm>int main()
{
 int a[20]={2,4,1,23,5,76,0,43,24,65},i;
 for(i=0;i<20;i++)
  cout<<a[i]<<endl;
 sort(a,a+20);
 for(i=0;i<20;i++)
 cout<<a[i]<<endl;
 return 0;
}

输出结果将是把数组a按升序排序,说到这里可能就有人会问怎么样用它降序排列呢?这就是下一个讨论的内容.一种是自己编写一个比较函数来实现,接着调用三个参数的sort:sort(begin,end,compare)就成了。对于list容器,这个方法也适用,把compare作为sort的参数就可以了,即:sort(compare).1)自己编写compare函数:
sort(begin,end),表示一个范围,例子:#include <algorithm>int main()
{
 int a[20]={2,4,1,23,5,76,0,43,24,65},i;
 for(i=0;i<20;i++)
  cout<<a[i]<<endl;
 sort(a,a+20);
 for(i=0;i<20;i++)
 cout<<a[i]<<endl;
 return 0;
}

4.二叉排序树的递归实现(BST)

void build(int i)//构建二叉搜索树
{
	if (i <= n)
	{
		build(2 * i);
		tree[i] = v[k++];
		build(2 * i + 1);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值