题目
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
要求
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
题意理解
Complete Binary Search Tree,涉及到两个概念:完全二叉树和二叉搜索树。
- 二叉搜索树
不一定是平衡的,可能全部左斜,可能全部右斜。
- 完全二叉树
题目要求将输入的数据填到一棵完全二叉树的结构中去,同时这棵完全二叉树还应满足二叉搜索树的性质,左边的子树结点的键值比根结点的键值要小,右边子树结点的键值比根结点要大,而且左右子树也分别满足二叉搜索树递归的定义。
就输入“1 2 3 4 5 6 7 8 9 0”而言,构建的完全二叉搜索树应该是:
最后要求输出完全二叉搜索树的层序遍历结果,即:6 3 8 1 5 7 9 0 2 4。
树的表示法:链表 vs. 数组
- 需要的操作
- 填写数字(某种遍历)
- 层序遍历
- 完全二叉树,不浪费空间 --- 所以用数组比较合算
- 层序遍历==直接顺序输出 ---如果用链表会有队列等操作
- 数组完胜!
核心算法
根据二叉搜索树的性质,如果得知根结点的左子树的结点个数,那么就知道根结点应该放置什么元素。而左子树结点的个数可以依靠完全二叉树的性质(确定了结点个数,完全二叉树的结构就一定了)。
于是,先将输入的序列进行排序。然后输入序列一共有10个元素,那么完全二叉树的根结点的左子树一定含有6个结点,即根结点是从小到大排第7位的那个数字(也就是6),同时也可以确定后面的三个数字(7 8 9)一定是属于根结点的右子树的。
输入序列:1 2 3 4 5 6 7 8 9 0
排好序:0 1 2 3 4 5 6 7 8 9
可知,6是根结点,同理递归地填左子树和右子树(左子树和右子树也是完全二叉树)。先左子树部分,因为是完全二叉树,所以左子树的根结点的左边一定有3个结点,右边有两个结点,即是说:0 1 2 3 4 5 6 7 8 9,3是左子树的根结点,以此类推。
填充数字时是先序遍历的应用。
假设调用核心算法前,输入序列已经放到了数组A中,并对数组A做了一个从小到大的排序,即排序后的输入序列是放在数组A中的。
结果生成的树也存在一个数组里,叫做T。TRoot对应的是当前我们要考虑的这棵子树的根结点所在的位置。
solve函数要完成的任务:从A段中选出正确的数字,填到T[TRoot]中。
排序
库函数qsort。
#include <stdlib.h>
int compare(const void* a, const void *b)
{
return *(int*)a - *(int*)b;
}
int main()
{
......
//A:待排序序列的首元素的位置
//N:待排序列的总长度
//sizeof(int):要排的元素的大小
//compare:函数名称,可以叫做其他名称,功能是比较两个元素的大小
qsort(A, N, sizeof(int), compare);
......
}
计算左子树的规模
要计算左子树的规模,当有H层的时候,结点总数为,那么计算左子树的规模的时候,因为也是完全二叉树,少了一层,结点总个数就是。
最小X = 0
最大X = (根据上面的递推公式,当h = H时,这一层的结点总个数:,那么再往下一层就是 = ,那么左子树的结点个数就是
所以加的X到底应该加多少呢?先根据计算出X,然后比较X和,加上比较小的那个数。
综合来说:
1.先根据输入的N计算出H,公式
2. 计算出X的值,公式
3.比较X和的大小,确定
4.计算出左子树的规模,利用公式:
代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
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;
//printf("GetLeftLength:%d\n", L);
return L;
}
void solve(int ALeft, int ARight, int TRoot)
{
/*初始调用为solve(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 LeftTRoot = TRoot * 2 + 1; //因为数组T中是从第0个位置开始存储的元素,所以根结点为TRoot,那么左孩子的下标就为TRoot*2+1
int RightTRoot = LeftTRoot + 1;
solve(ALeft, ALeft + L - 1, LeftTRoot);
solve(ALeft + L + 1, ARight, RightTRoot);
}
int compare(const void *a, const void *b)
{
return *(int*)a - *(int*)b;
}
int main()
{
int N;
scanf("%d\n", &N);
A = (int *)malloc(sizeof(int) * N);
T = (int *)malloc(sizeof(int) * N);
int i;
for(i = 0; i < N; i++) {
scanf("%d", &A[i]);
}
qsort(A, N, sizeof(int), compare);//将A数组排好序
solve(0, N-1, 0);
for(i = 0; i < N - 1; i++) {
printf("%d ", T[i]);
}
printf("%d", T[N-1]);
free(A);
free(T);
return 0;
}
运行结果: