04-树6 Complete Binary Search Tree (30 分)
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<stdlib.h>
#include<stdbool.h>
typedef struct TNode* Position;
struct TNode {
int Data;
Position Left;
Position Right;
};
typedef struct TNode* BST;
BST CreateCBST(int N);
void ChooseSort(int* a, int N);
int Cnt = 0;
void MidTraverse(BST Node, int* Vertex);
typedef BST ElementType;
struct QNode {
ElementType* Data;
int Head;
int Rear;
int Capacity;
};
typedef struct QNode* Queue;
bool IsEmpty(Queue Q);
ElementType OutQueue(Queue Q);
bool InsertQueue(Queue Q, ElementType Item);
bool IsFull(Queue Q);
Queue CreateQueue(int N);
void LevelTraverse(BST Root, Queue Q);
int main()
{
int N;//节点个数
scanf("%d", &N);
int* Vertex = (int*)malloc(sizeof(int) * N);
for (int i = 0; i < N; i++)
{
scanf("%d", Vertex + i);
}
ChooseSort(Vertex, N);
BST Root = CreateCBST(N);
MidTraverse(Root, Vertex);
Queue Q = CreateQueue(N+1);
LevelTraverse(Root, Q);
}
BST CreateCBST(int N)
{
BST* Arrary = (BST*)malloc(sizeof(BST) * N+1);
for (int i = 0; i <=N; i++)
{
Arrary[i] = (BST)malloc(sizeof(struct TNode));
Arrary[i]->Data = i;
Arrary[i]->Left = NULL;
Arrary[i]->Right = NULL;
}
for (int i =1; i <= N; i++)
{
if (i * 2 <= N)
{
Arrary[i]->Left = Arrary[i * 2];
if (i * 2 + 1 <= N)
Arrary[i]->Right = Arrary[i * 2 + 1];
}
}
return Arrary[1];
}
void ChooseSort(int* a, int N)
{
int Tmp;
int MinIndex;
for (int i = 0; i < N - 1; i++)
{
MinIndex = i;
Tmp = a[i];
for (int j = i + 1; j < N; j++)
{
if (a[MinIndex] > a[j])
{
MinIndex = j;
}
}
a[i] = a[MinIndex];
a[MinIndex] = Tmp;
}
}
void MidTraverse(BST Node,int*Vertex)
{
if (Node)
{
MidTraverse(Node->Left,Vertex);
Node->Data = Vertex[Cnt++];
MidTraverse(Node->Right, Vertex);
}
}
Queue CreateQueue(int N)
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->Data = (ElementType*)malloc(sizeof(ElementType) * (N));//Head指向的不放
Q->Capacity = N;//实际为N-1
Q->Head = 0;
Q->Rear = 0;
return Q;
}
bool IsFull(Queue Q)
{
return((Q->Rear + 1)%Q->Capacity == Q->Head);
}
bool InsertQueue(Queue Q,ElementType Item)
{
if (!IsFull(Q))
{
Q->Rear = (Q->Rear + 1) % Q->Capacity;
Q->Data[Q->Rear] = Item;
return true;
}
else
return false;
}
bool IsEmpty(Queue Q)
{
return (Q->Head == Q->Rear);
}
ElementType OutQueue(Queue Q)
{
if (!IsEmpty(Q))
{
Q->Head = (Q->Head + 1) % Q->Capacity;
return (Q->Data[Q->Head]);
}
else return NULL;
}
void LevelTraverse(BST Root,Queue Q)
{
int n = 0;
BST Node;
InsertQueue(Q, Root);
while (!IsEmpty(Q))
{
Node = OutQueue(Q);
n++;
if(n<Q->Capacity-1)
printf("%d ", Node->Data);
else
printf("%d", Node->Data);
if (Node->Left)
InsertQueue(Q, Node->Left);
if (Node->Right)
InsertQueue(Q,Node->Right);
}
}
很妙的地方在于中序遍历顺序为二叉搜索树从小到大