pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)

本文探讨了如何从一组非负整数键中构建一个既为二叉搜索树又为完全二叉树的数据结构。通过递归过程,文章详细解释了如何确保树的左子树仅包含小于节点键的值,右子树则包含大于等于节点键的值,并且树的底部层级从左到右填充。最后,给出了一个具体示例,展示了构建的完全二叉搜索树的层次遍历序列。

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

重点在递归的过程建树

详见代码:

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int a[1005];
int cnt[12]={-1,1,3,7,15,31,63,127,255,511,1023};
struct node
{
    int v;
    node *left=NULL,*right=NULL;
};
int mypow(int a,int b)
{
    int res=1;
    while(b--)res*=a;
    return res;
}
node* build(int l,int r,int num)
{
    if(l>r){return NULL;}
    if(l==r)
    {
        node *tt=new node();tt->v=a[l];
        return tt;
    }
    if(num<=0)return NULL;
    node *thi;
    int k1=0;
    while(cnt[k1+1]<num)k1++;
    int k2=k1+1;
    int rr=num-(cnt[k1]);
    int tmp=mypow(2,k2-2);
    if(rr>=tmp)
    {
        int lnum=cnt[k1];
        int rnum=num-lnum-1;
        int id=l+lnum;
        thi=new node();
        thi->v=a[id];
        thi->left=build(l,id-1,lnum);
        thi->right=build(id+1,r,rnum);
    }
    else {
        int lnum=num-1-cnt[k1-1];
        int rnum=cnt[k1-1];
        int id=l+lnum;
        thi=new node();thi->v=a[id];
        thi->left=build(l,id-1,lnum);
        thi->right=build(id+1,r,rnum);
    }
    return thi;
}
int main()
{
    //freopen("in.txt","r",stdin);
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+1+n);
    int l=1;int r=n;
    node *head=build(l,r,n);
    queue< node* >q;
    while(!q.empty())q.pop();
    q.push(head);
    vector<int>ans;ans.clear();
    node *now;
    while(!q.empty())
    {
        now=q.front();q.pop();
        ans.push_back(now->v);
        if(now->left!=NULL)q.push(now->left);
        if(now->right!=NULL)q.push(now->right);
    }
    for(int i=0;i<ans.size();i++)
    {
        cout<<ans[i];if(i!=ans.size()-1)cout<<" ";
    }
    cout<<endl;
    return 0;
}

 

<think>嗯,用户想了解关于完整二叉搜索树的概念和实现方法。首先,我需要明确什么是完整二叉搜索树(Complete Binary Search Tree,CBST)。根据之前学过的知识,二叉树中的完全二叉树(Complete Binary Tree)是指除了最后一层外,其他层都是满的,并且最后一层的节点都尽可能靠左排列。而二叉搜索树(Binary Search Tree,BST)的特点是每个节点的左子树所有节点的值都小于该节点的值,右子树则都大于。所以完整二叉搜索树应该是结合了这两种特性的数据结构,即同时满足完全二叉树的结构和二叉搜索树的排序性质。 接下来,我需要确认完整二叉搜索树的具体定义和特点。完全二叉树的特性使得它可以用数组高效地存储,而不需要指针,这对于内存管理是有优势的。而二叉搜索树的特性则保证了高效的查找、插入和删除操作,平均时间复杂度为O(log n)。结合起来,CBST可能在保持高效操作的同时,优化存储结构。不过,用户可能想知道如何实现这样的数据结构,或者它的具体应用场景。 然后,实现方法方面,可能需要考虑如何构建这样的树。由于是完全二叉树,节点的位置可以通过数组的索引来确定,比如对于节点i,左子节点在2i+1,右子节点在2i+2。同时,作为二叉搜索树,必须满足左子树的值小于根节点,右子树的值大于根节点。因此,构建CBST可能需要将给定的有序数组按照完全二叉树的结构进行排列。例如,对于有序数组,中序遍历完全二叉树的结构应该能得到有序序列,因此可能需要找到中间节点作为根,递归构建左子树和右子树。 另外,用户可能关心插入和删除操作在CBST中的处理。因为完全二叉树的结构要求最后一层尽可能左对齐,插入和删除可能需要调整树的结构以维持完全性,这可能会影响二叉搜索树的性质。这时候可能需要特定的算法来保持两者的平衡,或者在某些情况下,CBST可能更适合静态数据,而不是频繁变动的数据,因为维护结构可能代价较高。 还需要考虑CBST与其他类型二叉搜索树的区别,比如平衡二叉搜索树(如AVL树、红黑树)。CBST可能在存储上更紧凑,但可能在动态操作上不如平衡树高效。因此,用户可能想知道在什么情况下选择CBST更合适,比如数据静态且需要快速查找的情况下。 此外,用户的问题中提到了“实现方法”,可能需要给出具体的代码示例,比如如何从有序数组构建CBST,或者如何插入节点。例如,可以通过递归方法,每次选择中间元素作为根节点,然后递归构建左右子树,确保完全二叉树的结构。这部分需要结合算法步骤和代码实现来说明。 最后,需要验证自己的理解是否正确,是否有遗漏或错误的地方。例如,是否混淆了完全二叉树和满二叉树的概念,或者是否正确处理了二叉搜索树的排序性质与完全二叉树结构之间的冲突。比如,在构建过程中,如何确保节点位置既满足完全性,又满足左小右大的顺序,可能需要特定的构建策略,如中序遍历填充。</think>### 完整二叉搜索树(Complete Binary Search Tree)概念解析 完整二叉搜索树是**完全二叉树**和**二叉搜索树**的结合体,需同时满足以下特性: 1. **完全二叉树结构**:所有层除最后一层外均为满层,最后一层节点左对齐排列[^1] 2. **二叉搜索树性质**:任意节点的左子树值均小于该节点,右子树值均大于该节点 $$ \text{设节点索引为}i,\ \text{左子节点索引}=2i+1,\ \text{右子节点索引}=2i+2 $$ ### 核心实现步骤(以数组存储为例) 1. **数据预处理**:将输入数据排序为升序序列 2. **递归构建**: ```python def build_cbst(sorted_arr, tree, root_index=0): if not sorted_arr: return n = len(sorted_arr) mid = n // 2 # 计算中间索引 tree[root_index] = sorted_arr[mid] # 递归构建左子树(左半部分) build_cbst(sorted_arr[:mid], tree, 2*root_index+1) # 递归构建右子树(右半部分) build_cbst(sorted_arr[mid+1:], tree, 2*root_index+2) ``` 3. **空间分配**:根据完全二叉树特性预分配数组空间 $$ \text{数组长度}=2^h-1\ \text{(h为树的高度)} $$ ### 关键特性对比 | 特性 | 普通BST | 完整BST | |---------------------|---------|--------------| | 空间利用率 | 动态 | 100%(数组) | | 查找时间复杂度 | O(h) | O(log n) | | 插入/删除复杂度 | O(h) | O(n) | | 适合场景 | 动态数据| 静态数据 | ### 应用场景 1. 预先生成的静态数据集快速查询(如字典数据) 2. 需要固定存储空间的嵌入式系统 3. 需要快速序列化/反序列化的场景[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值