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<stdio.h>
#include<malloc.h>
#define Null -1
typedef struct TreeNode Tree;
typedef struct QueueNode* Queue;
struct TreeNode{
int elem;
int left;
int right;
};
struct QueueNode{
int Data[10024];
int front;
int rear;
};
void Add(Queue Q, int data){
Q->rear++;
Q->Data[Q->rear] = data;
}
int Delete(Queue Q){
Q->front++;
return Q->Data[Q->front];
}
int BuildTree (Tree* T, int* A, int l, int r){
int root;
int num;
int halfnum;
int temp, height,buttomleft, realleft, abovenum,i;
int right_tree_num = 1;
if (l > r) return Null;
if (l == r){
root = l;
T[root].elem = A[l];
T[root].left = T[root].right = Null;
return root;
}
num = r - l + 1;
temp = num;
height = 0;
while (temp){
temp/= 2;
height++;
}
abovenum = 1;
for( i = 1; i <= height - 1; i++){
abovenum*= 2;
}
abovenum -= 1;
buttomleft = 1;
for ( i = 1; i <= height -2; i++)
buttomleft *= 2;
realleft = num - abovenum;
halfnum = num/2;
while (right_tree_num <= halfnum)
right_tree_num *= 2;
right_tree_num = right_tree_num/2 - 1;
if (realleft > buttomleft)
right_tree_num += realleft - buttomleft;
root = r - right_tree_num;
T[root].elem = A[root];
T[root].left = BuildTree(T,A,l,root - 1);
T[root].right = BuildTree(T, A, root+1, r);
return root;
}
void LevelOrder(Tree* T, int root){
Queue Q;
int flag = 0;
Q = (Queue)malloc(sizeof(struct QueueNode));
Q->front = Q->rear = 0;
int temp;
Add(Q,root);
while ( Q->front != Q->rear){
temp = Delete(Q);
if(flag) printf(" ");
printf("%d",T[temp].elem);
flag = 1;
if (T[temp].left != Null)
Add(Q,T[temp].left);
if (T[temp].right != Null)
Add(Q,T[temp].right);
}
}
void HashSort(int* A, int N){
int i, j, k;
for ( i = N/2; i >= 1; i--){
for (j = 0; j < N; j++){
int temp = A[j];
for ( k = j; k >= i; k -= i){
if (A[k - i] > temp)
A[k] = A[k - i];
else
break;
}
A[k] = temp;
}
}
}
int main(){
int N;
int A[3024];
int i;
Tree T[3024];
scanf("%d",&N);
int root;
for ( i = 0 ; i < N; i++)
scanf("%d",&A[i]);
HashSort(A, N);
root = BuildTree(T,A,0, N-1);
LevelOrder(T,root);
}