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:
1 2 |
10 1 2 3 4 5 6 7 8 9 0 |
Sample Output:
1 |
6 3 8 1 5 7 9 0 2 4 |
注意 Complete Binary Search Tree 的特性即可
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
#include<stdio.h> #include<stdlib.h> typedef struct tree_ { int number; struct tree_ *left; struct tree_ *right; } tree; typedef struct queue_ { int size; int capacity; int front; int rear; void **data; } queue; #define is_queue_empty(X) (!((X)->size)) queue *init_queue(int n) { queue *temp; temp=(queue *)malloc(sizeof(queue)); temp->size=0; temp->capacity=n; temp->front=0; temp->rear=-1; temp->data=(void **)malloc(n*sizeof(void *)); return temp; } int enqueue(queue *q,void *data) { if(q->size==q->capacity) { return 1; } q->size++; q->rear=(q->rear+1)%q->capacity; q->data[q->rear]=data; return 0; } int dequeue(queue *q,void **data) { if(q->size==0) { *data=NULL; return 1; } q->size--; *data=q->data[q->front]; q->front=(q->front+1)%q->capacity; return 0; } int delete_queue(queue *q) { free(q->data); free(q); return 0; } void level_order(tree *t,int n) { queue *q; tree *temp; int flag=0; q=init_queue(n); enqueue(q,t); while(!is_queue_empty(q)) { dequeue(q,(void **)&temp); if(flag==0) { printf("%d",temp->number); flag=1; } else { printf(" %d",temp->number); } if(temp->left!=NULL) { enqueue(q,temp->left); } if(temp->right!=NULL) { enqueue(q,temp->right); } } printf("\n"); } int compare(const void *a,const void *b) { return *(int *)a-*(int *)b; } tree *build_cbst(int *sequence,int n) { tree *root; int edge,bottom,count,temp; if(n<=0) { return NULL; } root=(tree *)malloc(sizeof(tree)); if(n==1) { root->left=NULL; root->right=NULL; root->number=sequence[0]; return root; } temp=2; count=1; while(temp-1<n) { count++; temp=temp<<1; } temp=temp>>1; bottom=temp; if(n-temp+1>bottom/2) { edge=(temp-1-1)/2+bottom/2; } else { edge=(temp-1-1)/2+n-temp+1; } root->number=sequence[edge]; root->left=build_cbst(sequence,edge); root->right=build_cbst(sequence+edge+1,n-1-edge); return root; } int main() { int n,i,buf[1000]; tree *root; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",buf+i); } qsort(buf,n,sizeof(int),compare); root=build_cbst(buf,n); level_order(root,n); } |