PAT 甲级 1064 Complete Binary Search Tree (完全二叉树+二叉排序树)(c++版)(附代码注释)

1064 Complete Binary Search Tree (30 分)

原文链接:http://kakazai.cn/index.php/Kaka/Pat/query/id/154

题目

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568

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

题意分析

  • 题意

二叉排序树的递归定义:1)左子树的所有结点的值都小于根结点的值;2)右子树的所有结点的值都大于或等于根结点的值;3)左右子树也是一棵二叉排序树

完全二叉树的定义:满二叉树在最底层的最右边去掉一部分叶子结点就成了完全二叉树

给定一个序列,序列中元素都是不重复的,非负的。用这个序列来构造一棵树,既要是二叉排序树叶也要是完全二叉树。并且输出它的层次遍历。

范围:元素的值的范围[0,2000],元素的个数范围[1,1000]

  • 分析

二叉排序树的特点是中序遍历是升序的,因此将序列升序排列就可以得到该树的中序遍历。

完全二叉树的特点是,将它的层次遍历存储在一维数组中,父结点的数组下标若是i,则左孩子的下标是2i+1,右子结点的下标是2i+2。

结合这两个特点,根据父结点取孩子节点,对该树进行中序遍历,得出的序列结果必然是升序的。

知识点与坑点

  • 知识点

1)二叉排序树的中序遍历,完全二叉树的层次遍历

  • 坑点

1)

拟合法

算法思路

1 对输入序列进行升序,得到中序遍历的结果

2 假设树的层次遍历存在一维数组cbt[]中,根据父结点与孩子结点的下标关系,对树进行中序遍历

3 中序遍历时,将第一步的中序结果镶嵌进去

4 遍历cbt[]数组,即得到层次遍历结果

代码-c++版
#include <cstdio>
#include <algorithm> 
using namespace std;

/*数值范围*/ 
const int maxn = 1001;	//最多有1000个结点 

/*所有变量*/ 
int in[maxn];	//中序遍历 
int level[maxn];  //层次遍历  
int n;	//结点数目

/* 中序遍历 */ 
int c = 0;
void inorder(int root) {
    if (root >= n)return;
    inorder(root*2+1);
    level[root] = in[c]; //拟合中序遍历结果和过程
    c++;
    inorder(root * 2 + 2);
}

int main(){
	
scanf("%d", &n);
for (int i = 0; i < n; i++) {
	scanf("%d", &in[i]);
}
sort(in+0, in + n);	//对数组下标为[0,n)排序 
inorder(0);	//中序遍历 

/* 按要求输出层次遍历 */
for (int i = 0; i < n; i++){
	if(i!=0)printf(" ");
	printf("%d",level[i]);
} 

return 0;

}
代码-python版

平衡二叉二叉排序树和平衡二叉排序树都是数据结构中用于存储有序元素的特殊类型的二叉,它们在结构和性能上有一些关键区别: 1. **二叉排序树Binary Search Tree, BST)**: - 它是一个二叉,其中每个节点的左子包含的值都小于该节点的值,右子包含的值都大于该节点的值。 - 保证了查找、插入和删除操作的时间复杂度通常为 O(log n)(假设是完全平衡的),但在最坏情况下(退化成链表)可能会退化为 O(n)。 - 平衡性不是其固有属性,如果插入或删除操作导致严重不平衡,性能会下降。 2. **平衡二叉(Balanced Binary Tree)**: - 这是一个更宽泛的概念,包括但不限于AVL、红黑、B等,这些的设计目的是在每次插入和删除后自动调整以保持的高度尽可能均衡。 - 它们都有自我修复的能力,即使在插入或删除操作后也能快速恢复平衡,避免极端情况下的性能退化。 - 不同的平衡二叉在具体实现上有差异,如AVL是高度严格平衡的,而红黑则相对宽松一些,但总体上保证了O(log n)的操作时间。 3. **平衡二叉排序树(Balanced Binary Search Tree, BBST)**: - 这实际上是平衡二叉二叉排序树的结合,它保持了二叉排序树的排序性,同时具有平衡二叉的特性。 - 当插入或删除后,BBST会进行适当的旋转操作来维持平衡,确保查找、插入和删除始终能在O(log n)时间内完成。 相关问题: 1. 什么是BST的平衡性? 2. AVL和红黑是如何保持平衡的? 3. BBST在实际应用中的优缺点是什么?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值