Problem Description:
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 N N ( ≤ 1000 \leq 1000 ≤1000). Then N N 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
Problem Analysis:
前置知识:
满二叉树:
1、第 i i i 层的节点数目为 2 i 2^i 2i。
2、节点总数和深度的关系: n = ∑ i = 0 k 2 i = 2 k + 1 − 1 n = \sum\limits_{i=0}^k 2^i = 2^{k + 1} - 1 n=i=0∑k2i=2k+1−1
完全二叉树:
1、具有 n n n 个节点的完全二叉树的深度(根节点的深度定义为 0 0 0)为 k = ⌊ log 2 n ⌋ k = \lfloor \log_2 n \rfloor k=⌊log2n⌋。
2、最后一层的节点数为: n − ( 2 k − 1 ) = n + 1 − 2 k n - (2^k - 1) = n + 1 - 2^k n−(2k−1)=n+1−2k
3、左子树的节点数为(总结点个数为 n n n):
l ( n ) = { n − 2 k − 1 , n + 1 − 2 k ≤ 2 k − 1 2 k − 1 , n + 1 − 2 k > 2 k − 1 l(n) = \begin{cases} n - 2^{k-1}, & n + 1 - 2^k\leq 2^{k-1} \\\ 2^k - 1, & n + 1 - 2^k > 2^{k-1} \end{cases} l(n)={n−2k−1, 2k−1,n+1−2k≤2k−1n+1−2k>2k−1
因为最后一层全部都在左子树,右子树为满二叉树,高度为 k − 1 k-1 k−1。
因为左子树为满二叉树,高度为 k − 1 k - 1 k−1。
4、右子树: r ( n ) = n − l ( n ) r(n) = n - l(n) r(n)=n−l(n)。
二叉搜索树有一个比较重要的性质,那就是其节点权值的中序遍历是一个升序序列,又因为这是一颗完全二叉树,所以可以直接建树:
unordered_map<int, int> l, r;
for (int i = 1; i <= n; i ++ )
{
if (2 * i <= n) l[i] = i << 1;
if (2 * i + 1 <= n) r[i] = i << 1 | 1;
}
然后进行 DFS 中序遍历,根节点为 1 号节点,在这个过程中将权值从小到大依次填入每个节点:
void dfs(int u)
{
int left = l[u], right = r[u];
if (left) dfs(left);
w[u] = weight[cnt ++ ];
if (right) dfs(right);
}
由于建树时才用二叉树的性质进行建树,因此节点编号按从小到大的顺序排列恰好就是这棵树的层序遍历。
Code
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int N = 1010;
int n, k; // n节点总数,k为最深的深度(根节点深度为0)
unordered_map<int, int> l, r;
int weight[N], cnt;
int w[N];
void dfs(int u) // 当前搜索到的节点编号,当前节点所在的深度
{
// 二叉搜索树中序遍历是所有权值从小到大排列
int left = l[u], right = r[u];
if (left) dfs(left);
w[u] = weight[cnt ++ ]; // 填每个节点的权值
if (right) dfs(right);
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++ ) // 建立完全二叉树
{
if (2 * i <= n) l[i] = i << 1;
if (2 * i + 1 <= n) r[i] = i << 1 | 1;
}
for (int i = 0; i < n; i ++ ) cin >> weight[i];
sort(weight, weight + n);
dfs(1);
for (int i = 1; i <= n; i ++ )
{
cout << w[i];
if (i != n) cout << ' ';
}
return 0;
}