浙大数据结构第四周之04-树6 Complete Binary Search Tree

这篇具有很好参考价值的文章主要介绍了浙大数据结构第四周之04-树6 Complete Binary Search Tree。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

题目详情:

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. 因为二叉搜索树的中序满足:是一组序列的从小到大排列,所以只需将所给序列排序即可得到中序数组in
2. 假设把树按从左到右、从上到下的顺序依次编号,根节点为0,则从根结点root = 0开始中序遍历,root结点的左孩子下标是root*2+1,右孩子下标是root*2+2

递归的终止条件是下标比节点数目大

递归时也按中序遍历

单层处理逻辑是将已知的中序输出结果数组里对应位置的数字赋给通过从0~n -1下标中序遍历的level数组
因为是中序遍历,所以遍历结果与中序数组in中的值从0开始依次递增的结果相同,即in[t++](t从0开始),将in[t++]赋值给level[root]数组
3. 因为树是按从左到右、从上到下的顺序依次编号的,所以level数组从0到n-1的值即所求的层序遍历的值,输出level数组即可

如下图解释

浙大数据结构第四周之04-树6 Complete Binary Search Tree,数据结构,数据结构

 文章来源地址https://www.toymoban.com/news/detail-591601.html

代码实现:

/*
将读入的数据按从小到大排序,构成中序遍历结果
利用规律递归
递归返回值返回到层序遍历数组中
*/
#include <stdio.h>  
#define MAX_SIZE 1005
int Inorder[MAX_SIZE];
int Levelorder[MAX_SIZE];
int N;
int Ptr = 0;
/*从小到大排序*/
void Sort(int nodeNum) {
    for(int i = 0; i < nodeNum; i++) {
        for(int j = i; j < nodeNum; j++) {
            if(Inorder[i] > Inorder[j]) {
                int tmp = Inorder[i];
                Inorder[i] = Inorder[j];
                Inorder[j] = tmp;
            }
        }
    }
    return;
}
/*递归建树*/
void BuildTree(int root) {
    if(root >= N) {
        return;
    }

    BuildTree(root * 2 + 1);
    Levelorder[root] = Inorder[Ptr++];
    BuildTree(root * 2 + 2);
    return;
}
/*主函数*/
void MainFunction() {
    scanf("%d", &N);
    for(int i = 0; i < N; i++) {
        scanf("%d", &(Inorder[i]));
    }
    Sort(N);
    BuildTree(0);
    for(int i = 0; i < N; i++) {
        if(i != N - 1) {
            printf("%d ", Levelorder[i]);
        }
        else {
            printf("%d", Levelorder[i]);
        }
    }
    return;
}
int main() {
    MainFunction();
    return 0;
}

到了这里,关于浙大数据结构第四周之04-树6 Complete Binary Search Tree的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 浙大数据结构第二周之02-线性结构3 Reversing Linked List

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6. Input Specification: Each input file contains one test case. For each case, the first line

    2024年02月15日
    浏览(42)
  • 浙大数据结构第一周01-复杂度3 二分查找

    本题要求实现二分查找算法。 函数接口定义: 其中 List 结构定义如下: L 是用户传入的一个线性表,其中 ElementType 元素可以通过、==、进行比较,并且题目保证传入的数据是递增有序的。函数 BinarySearch 要查找 X 在 Data 中的位置,即数组下标(注意:元素从下标1开始存储)

    2024年02月12日
    浏览(31)
  • 浙大数据结构第三周之03-树2 List Leaves

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corr

    2024年02月16日
    浏览(30)
  • 浙大数据结构第八周之08-图7 公路村村通

    现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本。 输入格式: 输入数据包括城镇数目正整数N(≤1000)和候选道路数目M(≤3N);随后的M行对应M条道路,每行给出3个正整数,分别是该条道路

    2024年02月11日
    浏览(27)
  • 浙大数据结构第五周之05-树9 Huffman Codes

    In 1953, David A. Huffman published his paper \\\"A Method for the Construction of Minimum-Redundancy Codes\\\", and hence printed his name in the history of computer science. As a professor who gives the final exam problem on Huffman codes, I am encountering a big problem: the Huffman codes are NOT unique. For example, given a string \\\"aaaxuaxz\\\", we can observe th

    2024年02月15日
    浏览(38)
  • 浙大数据结构第二周02-线性结构2 一元多项式的乘法与加法运算

    设计函数分别求两个一元多项式的乘积与和。 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。 输出格式: 输出分2行,分别以指数递降方式输出乘积多项式

    2024年02月13日
    浏览(36)
  • (浙大陈越版)数据结构 第三章 树(上) 3.3 二叉树的遍历

    目录 3.3.1 遍历(先中后) 二叉树的遍历 先序遍历: 中序遍历 后序遍历 tips: 3.3.2 中序非递归遍历 非递归算法实现的基本思路:使用堆栈 中序遍历的非递归算法具体实现方法为: 3.3.3 层序遍历 难点 解决方法: 队列实现 思路 有如下二叉树作为例子: 遍历过程:(出队即

    2024年02月06日
    浏览(34)
  • 数据结构与算法 | 二叉树(Binary Tree)

    二叉树(Binary Tree)是一种树形数据结构,由节点构成,每个节点最多有两个子节点:一个左子节点和一个右子节点。 \\\"二叉树\\\"(Binary Tree)这个名称的由来是因为二叉树的每个节点最多有两个子节点,一个左子节点和一个右子节点。其中,“二叉”指的是两个,因此“二叉树

    2024年02月08日
    浏览(34)
  • (浙大陈越版)数据结构 第三章 树(中) 二叉搜索树和平衡二叉树

    目录 4.1.1 二叉搜索树及查找 什么是二叉搜索树 定义 二叉搜索树特殊函数集: 查找操作:Find 算法思想 代码实现 补:查找最大和最小元素 4.1.2 二叉搜索树的插入 插入操作:Insert 算法思想 代码实现 例题 4.1.3 二叉搜索树的删除 删除操作:delete 算法思想 情况1:删除叶节点

    2024年02月08日
    浏览(39)
  • Java 【数据结构】 二叉树(Binary_Tree)【神装】

        登神长阶  第五神装 二叉树 Binary-Tree 目录  🎷一.树形结构 🪗1.概念 🎸2.具体应用 🎹 二.二叉树(Binary Tree) 🎺1.概念  🎻2.表现形式 🪕3.特殊类型 🥁3.1完全二叉树(Complete Binary Tree) 🪘3.2满二叉树(Full Binary Tree) 🔋4.性质  🪫5.二叉树的遍历 💿5.1前中后序遍历

    2024年04月27日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包