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
题目解析
题目的目的是输入一串数字,然后构建完全二叉搜索树,搜索树的特点是左子树比根小,右子树比根大,完全二叉树的特点是层次遍历是没有中断点的,一行一行满满当当,可以用数组实现比较不会太浪费内存,最后也是要按照层次遍历输出结果,数组实现比较容易.
解题思路:
1. 中序遍历法
- 输入长度n,然后输入n个数都存到数列里面
- 想办法给函数排个序
- 建立一个数组用来存放树的结点
- 通过中序遍历(中序遍历在搜索树中是从小到大的遍历顺序)挨个在树里插入
2. 计算根结点(函数法)
- 输入长度n,然后输入n个数都存到数列里面
- 想办法给函数排个序
- 建立一个数组用来存放树的结点
- 设计函数来计算根的位置(根据完全二叉树的定义),然后再挨个在树里插入
代码示例(函数法):
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAXSIZE 1001
/* 定义全局变量比较容易操作,用指针传递不好看,动态数组节约空间,但是写题目没必要 */
int Sequence[MAXSIZE],Tree[MAXSIZE];
void Sort(int *Sequence,int n);
int compare(const void*a,const void*b);
int GetRoot(int n);
void BuildBST(int SLeft,int SRight,int TRoot);
int main(){
int i,n,Root;
scanf("%d",&n);//输入长度,一开始先写了,老套路了
Sort(Sequence,n);
BuildBST( 0, n - 1, 0);
for( i = 0; i < n; i++){
if(i == 0){
printf("%d",Tree[i]);
}else{
printf(" %d",Tree[i]);
}
}
}
int compare(const void*a,const void*b)
{
return *(int *)a - *(int *)b;
}
void Sort(int *Sequence,int n)
{
int i = 0;
for( ; i < n; i++){
scanf("%d",&Sequence[i]);
}
qsort(Sequence,n,sizeof(int),compare);
}
void BuildBST(int SLeft,int SRight,int TRoot)
{
int n,Root,LeftRoot,RightRoot;
n = SRight - SLeft + 1;//计算数列长度
if(n <= 0) return;//长度为0就返回
Root = GetRoot(n);//找到数列里面要操作的根,然后把下标返回,输入进树里
Tree[TRoot] = Sequence[SLeft + Root - 1];
//事实上面这里用函数找根插入然后递归比起中序遍历的算法要难理解
LeftRoot = TRoot * 2 + 1;//+1是因为下标从0开始
RightRoot = TRoot * 2 + 2;
BuildBST( SLeft, SLeft + Root - 2, LeftRoot);
BuildBST( SLeft + Root, SRight, RightRoot);
}
int GetRoot(int n)
{
int h,x,Root;
h = log(n + 1) / log(2);//log()是以e为底取对数,简单的换底公式
x = n + 1 - pow( 2, h);
x = ( x >= 0 && x <= pow(2,h-1) ) ? x : pow(2,h-1);
Root = pow(2,h-1) + x;
return Root;
}
代码示例(中序遍历法):
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 1001
/* Sort是不变的,BuildBST变了*/
int compare(const void *a, const void *b);
void BuildBST(int *Tree, int *Sequence, int index, int *i);
void Sort(int *Sequence,int n);
int Sequence[MAXSIZE],Tree[MAXSIZE];
int main()
{
int n, index, i;
scanf("%d", &n);
Tree[0] = n;//用下标0来存长度
index = 1, i = 0;
Sort(Sequence,n);
BuildBST(Tree, Sequence, index, &i);
for (i = 1; i <= Tree[0]; ++i) {//注意从1开始
if(i == 1){
printf("%d",Tree[i]);
}else{
printf(" %d",Tree[i]);
}
}
return 0;
}
int compare(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
void Sort(int *Sequence,int n)
{
int i = 0;
for( ; i < n; i++){
scanf("%d",&Sequence[i]);
}
qsort(Sequence,n,sizeof(int),compare);
}
/* 经典中序遍历 */
void BuildBST(int *Tree, int *Sequence, int index, int *i)
{
// 越界或者index超过最大值返回
if (index < 1 || index > Tree[0] || (*i) == Tree[0]) return;
else {
BuildBST(Tree, Sequence, index * 2, i);
Tree[index] = Sequence[(*i)++];
BuildBST(Tree, Sequence, index * 2 + 1, i);
}
}