Exercises 14.2-5: ⋆
We wish to augment red-black trees with an operation RB-ENUMERATE(x, a, b) that outputs all the keys k such that a ≤ k ≤ b in a red-black tree rooted at x. Describe how RB-ENUMERATE can be implemented in Θ(m +lg n) time, where m is the number of keys that are output and n is the number of internal nodes in the tree. (Hint: There is no need to add new fields to the red-black tree.)
解:
RB-ENUMERATE(x, a, b)
1 start TREE-SEARCH(x, a) //该方法返回不大于a的最紧下解,时间复杂度O(lgn)
2 if start < a
3 then start SUCC[start]
4 while start< b //O(m) 次循环
5 print start
6 start SUCC[start] //时间复杂度O(1),每个节点必须存储Succ这个域,指向下一个节点的位置.
总的时间复杂度O(m+lgn)
We can also answer this question without resorting to augmenting the data
structure: we perform an inorder tree walk, except we ignore (do not recurse
on) the left children of nodes with key < a and the right children of nodes with
key > b. Thus, if we consider both children of a node we will output at least one
of them, therefore, all nodes not counted by m will lie on two paths from the root
to a leaf in the tree. This yields a total running time of (m + lg n) as before.
RB-ENUMERATE(x, a, b)
{
-
if( a<=x<=b)
-
print (x)
-
RB-ENUMERATE(left[x], a, b)
-
RB-ENUMERATE(right[x], a, b)
-
else if(x<a)
-
RB-ENUMERATE(right[x], a, b)
-
else if(x>b)
-
RB-ENUMERATE(left[x], a, b)
}
这个时间复杂度会是O(m+lgn)吗?不解.
分享到:
相关推荐
《算法导论》是计算机科学领域的一本经典著作,它深入浅出地介绍了各种重要的算法,并提供了详尽的分析和实现。这本书对于学习算法、提升编程能力以及理解复杂度分析至关重要。"Solutions to selected exercises and...
中文名: 算法导论 原名: Introduction to Algorithms 作者: Thomas H. Cormen Ronald L. Rivest Charles E. Leiserson Clifford Stein 资源格式: PDF 版本: 文字版 出版社: The MIT Press书号: 978-0262033848发行...
Exercises 1-4.zip
- 练习题(Exercises):用于帮助学生掌握课程内容,无需提交。 - 问题(Problems):需提交并评分。 #### 三、作业格式与要求 - **作业格式**: - 作业开头应包含学生姓名、课程编号、问题编号、讨论班编号、...
算法导论 CLRS Mit Press - Introduction To Algorithms 2Nd Edition Incl Exercises Edition.chm
这本书涵盖了排序、搜索、图算法、动态规划、数据结构等核心主题,每个主题下都有大量的练习题,旨在帮助读者加深理解,锻炼解决问题的能力。这些答案通常会提供详细的步骤解释和分析,以及必要的伪代码或实际代码...
### Python Workout: 50 Essential Exercises (2020) #### Overview The "Python Workout: 50 Essential Exercises" is a comprehensive guide designed to help beginners and intermediate Python programmers ...
麻省理工算法导论 Table of Contents Introduction to Algorithms, Second Edition Preface Part I - Foundations Chapter 1 - The Role of Algorithms in Computing Chapter 2 - Getting Started ...
Both exercises and problems should be solved, but only the problems should be turned in. Exercises are intended to help you master the course material. Even though you should not turn in the exercise ...
【标题】"exercises-js-master.rar" 暗示这是一个JavaScript练习项目的压缩文件,很可能包含一系列用于提升JavaScript编程技能的代码挑战或实例。这个标题可能指的是一个开源项目,旨在帮助开发者通过实践来学习和...
《算法导论》习题解答.rar Solutions for Introduction to algorithms second edition Philip Bille The author of this document takes absolutely no responsibility for the contents. This is merely a vague ...
Exercises-and-Tests-源码.rar
TypeScript 练习题 series - 了解 TypeScript 基础知识 在本文中,我们将通过一系列的练习题来深入了解 TypeScript 的基础知识。这些练习题涵盖了 TypeScript 的类型系统、泛型、配置文件、与 React、Vue、Webpack ...
注:下载后,评价时给5星,还你11分 Table of Contents Introduction to Algorithms, Second Edition Preface Part I - Foundations Chapter 1 - The Role of Algorithms in Computing Chapter 2 - ...
Chapter 5 - Probabilistic Analysis and Randomized Algorithms Part II - Sorting and Order Statistics Chapter 6 - Heapsort Chapter 7 - Quicksort Chapter 8 - Sorting in Linear Time Chapter 9 -...
通过这些练习,你将逐步熟悉Pandas的常用功能,并能将其应用于实际的数据分析项目。记得在实践中不断尝试,多思考,理论结合实践,才能真正掌握Pandas的精髓。祝你在数据分析的道路上越走越远!
Exercism_exercises_in_Ruby._ruby.zip Exercism_exercises_in_Ruby._ruby.zip Exercism_exercises_in_Ruby._ruby.zip Exercism_exercises_in_Ruby._ruby.zip Exercism_exercises_in_Ruby._ruby.zip Exercism_...
The only way to master matrix algebra is by working through exercises. Most texts have exercises, but few offer solutions. Harville's main text is great because it offers proofs for most theorems. ...
在这个“Java Exercises --- JetBrains”项目中,我们可能找到了一系列针对Java学习者的实践题目,旨在提升他们的编程技能和对Java的理解。 描述中提到的"JetBrains Academy"是一个在线学习平台,它提供了一系列的...
ACT4E_exercises库的具体功能和用途可能包括各种编程练习、算法实现、教学辅助工具等,但详细内容需参考"使用说明.txt"以获取更多信息。 总结起来,这个压缩包是一个Python Wheel格式的库,名为ACT4E_exercises,...