您的位置:首页 > 其它

04-树5. Complete Binary Search Tree (30)

2015-04-11 23:48 337 查看
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

苦觅一天没想出特别贴题的解决办法,用个数学方法装个逼凑合凑合

题目的要求是给定一堆数,构建一棵完全二叉搜索树。观察可以发现,对所有元素进行排序之后, 树的最后一排是从最小的数开始隔一个数升序排列的。读取完最后一排之后,新的最后一排在去掉最后刚才那排之后的所有元素的大小顺序中也符合上述规则。程序的时间复杂度为O(NlogN)

const int MAXSIZE = 10000;

#include <stdio.h>
#include <malloc.h>
#include <cmath>

int main(void)
{
    int n = 0;
    int* tempInput = (int*) malloc(sizeof(int) * MAXSIZE);
    // 所有元素全部置-1,为下面的排序做准备
    for (int i = 0; i < MAXSIZE; i++)
    {
        tempInput[i] = -1;
    }
    scanf("%d", &n);
    int* orderNode = (int*) malloc(sizeof(int) * n);
    // 键值对排序
    for (int i = 0; i < n; i++)
    {
        int temp = 0;
        scanf("%d", &temp);
        tempInput[temp] = 0;
    }
    for (int i = 0; i < MAXSIZE; i++)
    {
        static int j = 0;
        if (tempInput[i] == 0)
        {
            orderNode[j++] = i;
        }
    }
    free(tempInput);
    // 整排的排数
    const int count_Column = log(n + 1) / log(2);
    // 最后一排的元素个数
    const int restNode = n - (pow(2, count_Column) - 1);

    int* output = (int*) malloc(sizeof(int) * n);
    // 读取过的元素置为-1
    for (int i = 0; i < restNode; i++)
    {
        output[n - restNode + i] = orderNode[i * 2];
        orderNode[i * 2] = -1;
    }
    for (int i = count_Column; i > 0; i--)
    {
            int originalIndex = 0;
            int judge = 0;
            int outIndex = 0;
            while (originalIndex != n)
            {
                if (orderNode[originalIndex] != -1)
                {
                    if (judge % 2 == 0)
                    {
                        output[int(pow(2, i - 1) - 1) + outIndex] = orderNode[originalIndex];
                        orderNode[originalIndex] = -1;
                        outIndex++;
                    }
                    judge++;
                }
                originalIndex++;
            }
    }
    // 输出
    for (int i = 0; i < n; i++)
    {
        if (i != n - 1)
        {
            printf("%d ", output[i]);
        }
        else
        {
            printf("%d", output[i]);
        }
    }

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: