MAX-HEAPIFY(2/3n的疑惑)
Q:
In CLRS, third Edition, on page 155, it is given that in MAX-HEAPIFY,
The children’s subtrees each have size at most 2n/3—the worst case occurs when the bottom level of the tree is exactly half full.
I understand why it is worst when the bottom level of the tree is exactly half full. And it is also answered in this question worst case in MAX-HEAPIFY
My question is how to get 2n/3?
Why if the bottom level is half full, then the size of the child tree is up to 2n/3?
How to calculate that?
A:
For a complete binary tree of height h
, number of nodes is f(h) = 2^h - 1
. In above case we have nearly complete binary tree with bottom half full. We can visualize this as collection of root + left complete tree + right complete tree
. If height of original tree is h
, then height of left is h - 1
and right is h - 2
. So equation becomes
n = 1 + f(h-1) + f(h-2)
(1)
We want to solve above for f(h-1)
expressed as in terms of n
f(h-2) = 2^(h-2) - 1 = (2^(h-1)-1+1)/2 - 1 = (f(h-1) - 1)/2
(2)
Using above in (1) we have
n = 1 + f(h-1) + (f(h-1) - 1)/2 = 1/2 + 3*f(h-1)/2
=> f(h-1) = 2*(n-1/2)/3
Hence O(2n/3)
MAX-HEAPIFY(2/3n的疑惑)的更多相关文章
- Heap和Heapify
最近复习数据结构,又回去再看塞神的课件,看到PriorityQueue的实现.自己也根据塞神的代码写一写. 下面使用Binary Heap实现了一个简单的 Max-oriented PriorityQ ...
- 堆排序(heap sort)
参考博客:http://bubkoo.com/2014/01/14/sort-algorithm/heap-sort/ 1.二叉树 二叉树的第 i 层至多有 2i-1 个结点:深度为 k 的二叉树至多 ...
- 常见排序算法总结 -- java实现
常见排序算法总结 -- java实现 排序算法可以分为两大类: 非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此称为非线性时间比较类排序. 线性时间 ...
- Python中多使用迭代器
英文原文出处:Use More Iterators 本文介绍将代码转换为使用迭代器的原因和实用技巧. 我最喜欢的Python语言的特色之一是生成器,它们是非常有用的,然而当阅读开源代码时,我很少遇到它 ...
- [Swift]八大排序算法(四):堆排序
排序分为内部排序和外部排序. 内部排序:是指待排序列完全存放在内存中所进行的排序过程,适合不太大的元素序列. 外部排序:指的是大文件的排序,即待排序的记录存储在外存储器上,待排序的文件无法一次装入内存 ...
- C++面试笔记--排序
这里我们开始复习排序的一些面试题. 首先我们来看一下各个排序方法的时间复杂度和稳定性的比较,见下面表格: 排序法 平均时间 最差情形 稳定度 额外空间 备注 冒泡 O(n2) O(n2) 稳定 ...
- PHP面试:说下什么是堆和堆排序?
堆是什么? 堆是基于树抽象数据类型的一种特殊的数据结构,用于许多算法和数据结构中.一个常见的例子就是优先队列,还有排序算法之一的堆排序.这篇文章我们将讨论堆的属性.不同类型的堆以及堆的常见操作.另外我 ...
- JavaScript--数据结构与算法之排序
排序总结————常见的排序 常见的9中排序(冒泡,选择,插入(二分插入,希尔),归并,快速,堆,计数,基数,桶排序)可分为两类 比较排序:冒泡,选择,插入(二分插入,希尔),归并,堆,快速 非比较排序 ...
- 七种常见经典排序算法总结(C++实现)
排序算法是非常常见也非常基础的算法,以至于大部分情况下它们都被集成到了语言的辅助库中.排序算法虽然已经可以很方便的使用,但是理解排序算法可以帮助我们找到解题的方向. 1. 冒泡排序 (Bubble S ...
随机推荐
- input属性disabled和readonly的区别
两种属性的写法如下: 1.<input type="text" name="name" value="xxx" disabled=&q ...
- gson小练习之嵌套复杂数据解析
package com.zf.demo; import java.util.List; import com.google.gson.Gson; public class JGson { /** * ...
- slider jq小插件
html代码 <div class="r_list r_1" style="display:block;"> <div class=" ...
- phpcms v9 自定义伪静态的分页函数
因为.这个页面还没做好..等做好了..再给大家演示...... 调用方法:$page_attr=pages_open($num[0]['cun'],$get_page,$max_page,'http: ...
- 手把手教你写LKM rookit! 之 第一个lkm程序及模块隐藏(一)
唉,一开始在纠结起个什么名字,感觉名字常常的很装逼,于是起了个这<手把手教你写LKM rookit> 我觉得: 你们觉得:...... 开始之前,我们先来理解一句话:一切的操作都是系统调用 ...
- There is a legend about a bird
There is a legend about a bird which sings just once in its life, more sweetly than any other ...
- Sugarcrm Email Integration
Introdurce http://www.sugarcrm.com/feature/email-integration Tutor http://www.youtube.com/watch?v=BQ ...
- linux whereis which
whereis 命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s). [root@localhost ~]# whereis svn svn ...
- python学习笔记6(字典)
映射:键值对的关系,键(key)映射值(value) 字典是Python唯一的映射类型 >>> phonebook = {'} >>> phonebook {'} ...
- 在SpringMVC利用MockMvc进行单元测试
spring在线文档:https://docs.spring.io/spring/docs/current/javadoc-api/index.html?index-files/index-13.ht ...