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 <math.h>
#define SIZE 1000

void InorderTree(int *T,int N);
void PrintTree(int *T,int N);
void CBTBuild(int Aleft,int Aright,int Troot);
int GetLeftLength(int N);

int A[SIZE],T[SIZE];


int main()
{
    /* 初始定义两个数组,存放当前数据的数组A以及结果数组T 1.A:读入初始序列 2.A:按照大小排序 3.计算出左子树结点个数,右子树结点个数,找到当前结果完全二叉树T结点对应在A的下标并赋值 4.递归调用第三步,直到N个数均遍历完成 5.输出结果 */
    int N;
    scanf("%d",&N);
    for(int i=0;i<N;i++)
    {
        scanf("%d",&A[i]);
    }
    InorderTree(A,N);
    CBTBuild(0,N-1,0);
    PrintTree(T,N);
    return 0;
}

void InorderTree(int *TREE,int N)
{
    int swap,temp;
    for(int i=0;i<N;i++)
    {
        swap = 1;
        for(int j=0;j<N-i-1;j++)
        {
            if(TREE[j]>TREE[j+1])
            {
                swap = 0;
                temp = TREE[j];
                TREE[j] = TREE[j+1];
                TREE[j+1] = temp;
            }
        }
        if(swap) break;
    }
}

void PrintTree(int *TREE,int N)
{
    for(int i=0;i<N-1;i++){
        printf("%d ",TREE[i]);
    }
    printf("%d\n",TREE[N-1]);
}

void CBTBuild(int Aleft,int Aright,int Troot)
{
    int LeftRoot,RightRoot;
    int n = Aright - Aleft +1;
    if(n==0) return;
    int L = GetLeftLength(n);
    T[Troot] = A[Aleft+L];
    LeftRoot = Troot*2+1;  //Troot左孩子下标
    RightRoot = LeftRoot+1; ////Troot右孩子下标
    CBTBuild(Aleft,Aleft+L-1,LeftRoot);
    CBTBuild(Aleft+L+1,Aright,RightRoot);
}

int GetLeftLength(int N)
{
    int X,H,i =0;
    while(pow(2,i)-1<N)
    {
        i++;
    }
    H = i-1;
    X = N-pow(2,H)+1;
    if(X<=pow(2,H-1))
        return X+pow(2,H-1)-1;
    else
        return pow(2,H)-1;
}
全部评论

相关推荐

2025-12-02 02:15
门头沟学院
最近菊厂陆续开了,极力劝退那些拿13级的985硕士,就13级那么点儿薪资,一线城市每个月到手1.8/7/6w,租房2k还是破烂,吃饭2k还是预制菜,买个1k衣服都是聚酯纤维破塑料,稍微出去浪一浪,能留1w就是万岁,要是再有个啥都想买的对象,一线工作一年难存10w。隔壁工地混泥土,钳工,焊工一天800+,还包吃包住。读书18年到985硕士出来就为了进厂螺丝工?还不如从8岁童工开始干活,别人读书完了你工龄18+,混不上领导也是个小头头了。当然专科进来正式工,od都行,一般本科进来13级也OK,毕竟22岁年纪摆在那个地方还不需要太花钱。读硕博的基本26岁,工作两年就要结婚的,兜里没几个崽,连彩礼都要信用贷。菊厂离职的不少,毕竟正常没人受得了9116(梗:再来一次911刷6)。为啥这时候劝?因为刚下班,因为国考刚完,省考下周,就是可惜选调只有当年应届能报。现在回想能拍断大腿。应届生真实好身份,错过这一次,选调,考公,考编,当老师,进医院,研究所,高校,央国企,基本都无缘了,就连报名资格都被剥夺了,可谓是被党和国家遗弃的废材,统称“社会上的”,扔到社会去流浪,被用坏了就扔医院,长期超负载使用,零件修不好基本可以扔火里回炉重造了。体制内奉行找体制内的,都是党和国家选的人才,智力不差,样貌不丑,身材端正,收入稳定,安居房政策福利待遇也OK。因公出行都是报销,周末顺带“游山玩水“,这种体制内单身资源但凡想找对象,去社会上随便吆喝一声都排队。观察一下,基本没什么公务员在相亲,因为早就被邻里邻居抢光了。
哈哈哈,你是老六:就这不去的人大把人干呢,现在不缺人干活,你不干大把干呢,还有那个说农民工赚钱的,那个800+我估计肯定也就那一段时间,哪有这么赚钱,还是一句话,要想存下钱必须花销极低,能省的就不花钱,工资要高点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务