1064 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

题目大意:给出 N 个非负的整数,将这个整数序列构建为一棵完全二叉搜索树,输出它的层序遍历序列。

分析:先将给出的整数序列从小到大排序。由于是一棵完全二叉搜索树,之前排好序的序列就是这棵树的中序遍历。可以新建一个大小为 N 的数组,对其进行中序遍历。每次中序遍历根的时候,标记一个值,这个值代表这个根节点是第几个被遍历到的。完成中序遍历后,再顺序遍历这个新数组,输出对应下标的整数即可得到层序遍历值。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0x3fffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
#define NUMBER_OF_THREADS   10
using namespace std;

//int cnt=1;

void mid_order(int bst[],int n,int pos)
{
    static int cnt=1;
    if(pos>n)return;

    mid_order(bst,n,pos*2);
    bst[pos]=cnt++;
    mid_order(bst,n,pos*2+1);

}

int main(void)
{
    #ifdef test
    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    clock_t start=clock();
    #endif //test

    int n;scanf("%d",&n);
    int num[n*2+5]={0},bst[n*2+5]={0};
    for(int i=0;i<n;++i)
        scanf("%d",&num[i]);
    sort(num,num+n);
    mid_order(bst,n,1);

    for(int i=1;i<=n;++i)
        if(i==1)printf("%d",num[bst[i]-1]);
        else printf(" %d",num[bst[i]-1]);
    printf("\n");


    #ifdef test
    clockid_t end=clock();
    double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    printf("\n\n\n\n\n");
    cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位
    cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    #endif //test
    return 0;
}

构造一个完整的二叉搜索树(Complete Binary Search Tree, CBST)涉及两个关键方面:一是确保该树是一个完全二叉树,二是满足二叉搜索树的性质。 ### 完全二叉树的特点 完全二叉树是指除最后一层外,其余每一层都被完全填充,并且所有节点都必须尽可能靠左排列。这一特征使得我们可以有效地通过数组来表示完全二叉树,其中对于任意索引i处的节点: - 其左孩子位于位置2*i+1; - 右孩子则处于位置2*i+2; ### 构建步骤概述 给定一组数值元素如列表`[7, 4, 9, 1, 5, 8, 10]`, 我们希望将其组织成一颗CBST: #### 步骤一: 对输入数据排序 首先应对原始序列进行升序排序得到 `[1, 4, 5, 7, 8, 9, 10]` #### 步骤二: 利用分治法递归生成CBST 采用类似堆排序的方式,找到中间点作为根节点(root),左侧部分构成左子树,右侧形成右子树。 例如选取上述排好序后的中间值 `7` 设为root,则左边 `[1, 4, 5]` 成为其左子树,右边 `[8, 9, 10]` 继续按同样规则处理直至每个叶节点均创建完毕为止。 ```python class TreeNode: def __init__(self, val=0): self.val = val self.left = None self.right = None def sortedArrayToBST(nums): if not nums: return None mid = len(nums) // 2 #取整数向下找中心位置 root = TreeNode(nums[mid]) root.left = sortedArrayToBST(nums[:mid]) #递归建立左半边树 root.right = sortedArrayToBST(nums[mid+1:]) #递归建立右半边树 return root ``` 这个Python示例程序展示了如何把已排序好的数组转换成为一棵平衡CBST的基本逻辑框架。 --- 如果您还有更多疑问或需要深入探讨某些细节,请随时提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值