1099. Build A Binary Search Tree (30) <BST树>

本文介绍如何根据给定的二叉树结构和一组整数键值,构造一棵二叉搜索树,并实现该树的层序遍历输出。通过排序键值并利用二叉搜索树的性质填充树节点,最终实现树的正确遍历。

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.

    Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N (<=100) which is the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the format "left_index right_index", provided that the nodes are numbered from 0 to N-1, and 0 is always the root. If one child is missing, then -1 will represent the NULL child pointer. Finally N distinct integer keys are given in the last line.

    Output Specification:

    For each test case, print in one line the level order traversal sequence of that tree. All the numbers must be separated by a space, with no extra space at the end of the line.

    Sample Input:
    9
    1 6
    2 3
    -1 -1
    -1 4
    5 -1
    -1 -1
    7 -1
    -1 8
    -1 -1
    73 45 11 58 82 25 67 38 42
    
    Sample Output:
    58 25 82 11 38 67 45 73 42
    相当于给你一个建立好的树,给你一些权值,让你输出层序遍历


    BST树:

     左结点权值小于结点权值

     右结点权值大于等于结点权值

    就相当于对这个树进行中序遍历一定是从小到大排列


    给你的权值sort一下,就是树的中序遍历

    然后对树的结点权值进行归为。最后输出层序遍历


    自己的问题:

    刚开始就使用链表做的,一直22分。

    百度搜都是用数组做的,好不容易找到一个说是因为

    假定了结点按先序遍历是按顺序编号的(当然是受样例的影响)这句话我也没看太懂。改用数组做了

  • 其实用数组做比链表还简单。。。。。。。

  • #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<queue>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    using namespace std;
    typedef struct tree{
    	int left,right;
    	int num;
    }tree;
    tree t[100001];
    int key[100001];
    int ct=0;
    void zhong(int index){
    	if(t[index].left!=-1){
    		zhong(t[index].left);
    	} 
    	t[index].num=key[ct++];
    	if(t[index].right!=-1){
    		zhong(t[index].right);
    	}
    }
    void ceng(int index){
    	queue<int> que;
    	que.push(index);
    	cout<<t[index].num;
    	while(que.size()){
    		int fz=que.front();
    		que.pop();
    		if(t[fz].left!=-1) {
    			cout<<" "<<t[t[fz].left].num;
    			que.push(t[fz].left);
    		}
    		if(t[fz].right!=-1) {
    			que.push(t[fz].right);
    			cout<<" "<<t[t[fz].right].num;
    		}
    	}
    }
    int main(){
    	int n;
    	cin>>n;
    	for(int i=0;i<n;i++){
    		scanf("%d %d",&t[i].left,&t[i].right);
    	}
    	for(int i=0;i<n;i++){
    		scanf("%d",&key[i]);
    	}
    	sort(key,key+n);
    	zhong(0);
    	ceng(0);
    	return 0;
    }
    
    另附链表做的未AC代码
  • #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<queue>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    using namespace std;
    typedef struct tree{
    	int num,fz;
    	struct tree *right,*left;
    }tree,*linktree;
    int key[10001];
    int n;
    int cnt=0;
    static linktree t[10001];
    void qian(linktree head){
    	if(head){
    		int next=head->fz;
    		qian(t[next]->left);
    		t[next]->num=key[cnt++];
    		//cout<<t[next]->num<<" ";
    		qian(t[next]->right);
    	}
    }
    int out[10001];
    int cn=0;
    void cengxu(linktree head){
    	int next=0;
    	queue<linktree> que;
    	que.push(head);
    	while(que.size()){
    		linktree l=new tree();
    		l=que.front();
    		que.pop();
    		next=l->fz;
    		out[cn++]=(l->num);
    		if(t[next]->left!=NULL){
    			que.push(t[next]->left);
    		}
    		if(t[next]->right!=NULL){
    			que.push(t[next]->right);
    		}
    	}
    }
    int main(){
    	int f[1001]={0};
    	cin>>n;
    	t[0]=new tree();
    	t[0]->fz=0;
    	f[0]=1;
    	for(int i=0;i<n;i++){
    		int a,b;
    		cin>>a>>b;
    		if(f[i]==0){
    			t[i]=new tree();
    			f[i]=1;
    		}	
    		if(a!=-1){
    		   t[a]=new tree();	
    		   f[a]=1;
    		   t[a]->fz=a;	
    		   t[i]->left=t[a];
    		}
    		else t[i]->left=NULL;
    		
    		if(b!=-1){
    		   t[b]=new tree();
    		   f[b]=1;
    		   t[b]->fz=b;	
    		   t[i]->right=t[b];	
    		}
    		else t[i]->right=NULL;
    	}
    	
    	for(int i=0;i<n;i++) scanf("%d",&key[i]);
    	
    	sort(key,key+n);
    	
    	qian(t[0]);
    	
    	cengxu(t[0]);
    	for(int i=0;i<cn;i++){
    		if(i==0) printf("%d",out[i]);
    		else printf(" %d",out[i]);
    	}
    	return 0;
    }
    



<think>嗯,用户想了解关于完整二叉搜索的概念和实现方法。首先,我需要明确什么是完整二叉搜索(Complete Binary Search Tree,CBST)。根据之前学过的知识,二叉中的完全二叉(Complete Binary Tree)是指除了最后一层外,其他层都是满的,并且最后一层的节点都尽可能靠左排列。而二叉搜索Binary Search TreeBST)的特点是每个节点的左子所有节点的值都小于该节点的值,右子则都大于。所以完整二叉搜索应该是结合了这两种特性的数据结构,即同时满足完全二叉的结构和二叉搜索的排序性质。 接下来,我需要确认完整二叉搜索的具体定义和特点。完全二叉的特性使得它可以用数组高效地存储,而不需要指针,这对于内存管理是有优势的。而二叉搜索的特性则保证了高效的查找、插入和删除操作,平均时间复杂度为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、付费专栏及课程。

余额充值