04-树6完整的二叉查找树 分数 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
    题意:

    二叉查找树(BST)被递归地定义为二叉树,该二叉树具有以下属性:

  • 节点的左子树只包含键小于该节点键的节点。
  • 节点右边的子树只包含键大于或等于节点键的节点。
  • 左右子树也必须是二分搜索法树。

    完全二叉树(CBT)是一种完全填充的树,可能除了底层是从左到右填充的。

    现在给定一系列不同的非负整数键,如果要求树也必须是CBT,则可以构造唯一的BST。你应该输出这个BST的层次顺序遍历序列。

    输入规格:

    每个输入文件包含一个测试用例。对于每种情况,第一行包含一个正整数N (≤1000).然后N下一行给出了不同的非负整数键。一行中的所有数字由空格分隔,并且不大于2000。

    输出规格:

    对于每个测试用例,在一行中打印相应的完整二叉查找树的层次顺序遍历序列。一行中的所有数字必须用空格隔开,并且行尾不能有多余的空格。

思路:

将读入的n个数排序,然后计算当前根的左子树和右子树分别有多少节点,在当前[l,r]区间中,左子树是[l,l+lt-1],右子树是[r-rt+1,r],lt、rt是左、右子树节点数,根为mid = l+lt = r-rt。

#include <bits/stdc++.h>
using namespace std;
int n,flag = 0;
int a[1005];
int h[11] = {0,1,3,7,15,31,63,127,255,511,1023};
queue<int>q;
int f(int l,int r){
	if(l == r)return l;
	else{
		int cnt = 1;
		for(int i = 1;i<11;i++){
			if(r-l+1==h[i]){//求当前树的层高 
				cnt = i;
				break;
			}else if(r-l+1>h[i]){
				cnt++;
			}else break;
		}
		int len = r-l+1 - h[cnt-1];//求最下面一层的节点数 
		int llen,rlen;			//最下面一层节点中属于左子树的数量和属于右子树的数量 
		if(len<=h[cnt-2]+1){	
			llen = len;
			rlen = 0;
		}else{
			llen = h[cnt-2]+1;
			rlen = len - llen;
		}
		int lt = llen + (r-l-len)/2;		//求得左右子树的节点个树 
		int rt = rlen + (r-l-len)/2;
		int mid = l + lt;
		if(mid-1>=l){
			q.push(l);
			q.push(mid-1);
		}
		if(r>=mid+1){
			q.push(mid+1);
			q.push(r);
		}
		return mid;
	}
}
int main()
{
	cin>>n;
	for(int i = 1;i<=n;i++){
		cin>>a[i];
	}
	sort(a+1,a+1+n);
	q.push(1);
	q.push(n);
	while(!q.empty()){
		int l,r;
		l = q.front();
		q.pop();
		r = q.front();
		q.pop();
		int mid = f(l,r);
		if(flag)cout<<" ";
		else flag = 1;
		cout<<a[mid];
	}
	return 0;
}

### 关于二叉排序的实现与应用 #### 二叉排序的概念 二叉排序(Binary Search Tree, BST)是一种特殊的二叉,其中每个节点都满足如下性质:对于任意节点X,如果Y是X左子中的一个节点,则Y的关键字小于等于X的关键字;如果Z是X右子的一个节点,则Z的关键字大于X的关键字[^1]。 #### 二叉排序的主要操作 ##### 查找操作 在执行查找时,从根节点开始比较目标值key和当前节点关键字。若相等则找到该元素;如果不相等就继续向相应的子深入直到发现匹配项或到达叶子位置为止[^2]。 ##### 插入操作 当要插入一个新的键k到BST中去的时候,按照上述提到的方法定位应该放置新结点的位置,并创建新的叶节点完成加入过程。 ##### 删除操作 删除分为三种情况处理: - 如果待删节点无子女,则直接移除; - 若只有一个孩子,则让此孩子的父指针指向它的孙子代替自己; - 对有两个孩子的节点来说,需要用前驱/后继替代被删除者并调整链接关系以保持原有特性不变。 ```cpp struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; // Insert function example TreeNode* insertIntoBST(TreeNode* root, int val) { if (!root) return new TreeNode(val); if (val < root->val) root->left = insertIntoBST(root->left, val); else if (val > root->val) root->right = insertIntoBST(root->right, val); return root; } ``` #### 应用实例 一种典型应用场景是在构建高效的个人通讯录系统里利用二叉排序来管理联系人信息。通过这种方式可以快速地按姓名字母顺序排列以及高效检索特定条目,因为每次查询平均只需访问log(n)级别的节点数即可得到结果[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值