IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 04-树6 Complete Binary Search Tree (30 分)------C语言 -> 正文阅读

[数据结构与算法]04-树6 Complete Binary Search Tree (30 分)------C语言

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的那个数肯定就是根结点。

????????那么怎么得到左子树的结点数呢,因为这又是完全二叉树,其有很多特殊性质:

1、层数为log2N(N为结点数)

3、每层的结点数最大为2^(k-1)(k为第几层)

3、前k层的总的结点数为2^k - 1

????????完全二叉树除了叶结点不满以外,其它应该都是满的,而叶结点也是从左到右排布的,中间没有空的,那么如图:

? ? ? ? ?至于思路的实现,可以链表,也可以数组,但我个人认为数组会更简单,因为涉及层次遍历。并且用一组从小到大排好的数来构造完全二叉搜索树,如果用链表,会非常麻烦。

#include<stdio.h>
#include<algorithm> ? ?//sort函数?
#include<cmath> ? ? ? ? ?//pow函数?
#include<vector> ? ? ? //sort函数,和algorithm一起才能使用sort?
#define Maxsize 2005
using namespace std;

int num[Maxsize], BST[Maxsize];
int Find_Left_Node(int N);
void Find_root(int left, int right, int root );

int main()
{
?? ?int N=0, i=0, Data=0; ??
?? ?scanf("%d", &N);
?? ?for(i=0;i<N;i++) scanf("%d", &num[i]);
?? ?sort(num,num+N);
?? ?Find_root(0, N-1, 0);
?? ?for(i=0;i<N;i++){
?? ??? ?printf("%d", BST[i]);
?? ??? ?if(i!=N-1) printf(" ");
?? ?}
?? ?
?? ?return 0;
}

int Find_Left_Node(int N)? ? ? ?//计算左子树结点个数
{
?? ?int i=0, L=0, floor=0, sum=1,tmp=0, number=0;
?? ?for(i=0;sum<=N+1;i++){
?? ??? ?sum*=2;
?? ?}
?? ?floor=i;
?? ?sum = (int) pow(2,floor-1);
?? ?number=N-(sum-1);
?? ?tmp=(int) pow(2, floor-2);
?? ?L=number<tmp?number:tmp;
?? ?L=L+(sum-2)/2;
?? ?
?? ?return L;
}

void Find_root(int left, int right, int root )? ? ? ?

//递归将大序列分成一堆子序列,分别求左子树结点个数
{
?? ?int length=right-left+1;
?? ?if(!length) return;
?? ?int L=Find_Left_Node(length);
?? ?BST[root]=num[left+L];
?? ?int leftroot=2*root+1;
?? ?int rightroot=leftroot+1;
?? ?Find_root(left,left+L-1,leftroot);
?? ?Find_root(left+L+1,right,rightroot);
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-08-03 11:27:45  更:2021-08-03 11:30:32 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/10 6:13:46-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码