数据结构与算法分析 C++版
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
输入两个实数分别作为实部和虚部来构造一个
3-8i i
抽象数据类型(ADT)是未指明具体实现的一组 值的集合及其相关操作(A set of data values
and associated operations that are specified independent of any particular implementation. )
第一部分
预备知识
第1章 Chapter 1
数据结构与算法 (Data Structures and Algorithms)
1
2
What is a data structure?
最广义地说, 数据结构是指数据的表示及其相 关操作(In the most general, a data structure is any y data representation p and its associated operations.) 更典型地讲, 数据结构是指一组数据项的组织 或结构(More typically, a data structure is meant to be an organization or structuring for a collection of data items.)
5 6
1
Choosing your Data Structure
1.
Choosing your Data Structure
You should follow these steps
分析问题, 确定解必须满足的资源限制 (Analyze your problem to determine the resource constraints that any solution must meet) 确定必须支持的基本操作, 对每一个操作的资源限 制进行量化(Determine the basic operations that must be supported and quantify the resource constraints for each operation) 选择能最好地满足要求的数据结构(Select the data structure that best meets these requirements)
例:整数类型
(或、与、非)的统一体; 整数类型就是整数值与若干算术运算(加、减、 乘、除、取模)的统一体; 复数类型就是复数值与若干复数运算的统一体; 线性表(List)类型就是列表与插入、删除、清 空、查找等操作的统一体 13
在使用 int i 或 long i 时我们通常只关心 i 是整数,可以对它加减 乘除,并未考虑其实现问题
17
3
抽象数据类型(Abstract Data Type) (cont)
Grader的ADT
抽象数据类型(Abstract Data Type) (cont)
String的ADT
Operations s.append(str) s.compare( (str t) s.erase(index, length) s.find(str) s.rfind(str) s.insert(index, str) s.length() or s.size() s.replace(index, len, str) s.substr(start, length) or s.substr(start)
数据类型(data type):一组值的集合及其 相关操作(A set of data values and associated operations )
逻辑类型就是逻辑值(是、否)与 逻辑类型就是逻辑值(是 否)与3种逻辑运算
数据类型的逻辑概念(使用者所关心的)和它的物 理实现(生产者所关心的)之间是有区别的(There is a distinction between the logical concept of a data type and its physical implementation)
These techniques also allow you to judge the merits of new data structures that you or others might invent.(评判新数据结构的价值)
12
11
2
数据类型(Data Type)
数据类型(Data Type)
课程目标(Goals of this Course)
1.
Reinforce the concept that costs and benefits exist for every data structure. Learn the commonly used data structures.
2.
用4个字节存放一个整数?用8个字节? 进行加法时,用二进制如何进行?
例:线性表(Lists)可以通过链式存储也可通过数组的方
式来实现
14
一个线性表
10010 970161 曾小波 90 10020 970166 杨雪辉 89 10030 970164 胡 涛 95 10040 970159 李启明 91 10050 970160 刘 华 78 10060 970157 李小青 83 10070 970168 梅 玲 65 10080 970163 王 伟 65
10010 10020 10030 10040 10050 10060 10070 10080 10090 Head
970159 李启明 91 100B0 970164 胡 涛 95 10010 970166 杨雪辉 89 10030 970157 李小青 83 10090 970163 王 伟 65 NULL 970168 梅 玲 65 10080 970160 刘 华 78 10060
4
Efficiency(效率) and Cost(代价)
Efficiency and Cost (cont)
A solution to a problem is efficient(有效 率的) if it solves the problem within the required resource constraints(资源限制)
解的代价是指其消耗的资源量的总和(The cost of a solution is the amount of resources that the solution consumes.)
Often
the cost is measured in terms of one key resource
3
Organizing Data
一组记录的任何一种组织(结构)都可能被检索、 按任意顺序被处理或被修改(Any organization for a collection of records can be searched, processed d in i any order, d or modified.) difi d ) 对数据结构和算法的不同选择会造成程序运行时 间的很大不同: 几秒钟或者很多天(The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days.)
9
for each data item it stores, to perform each basic operation, programming effort.
10
Data Structure Philosophy (cont)
每个问题都有可用空间和时间的限制. (Each problem has constraints on available space and time.) Only after a careful analysis of problem characteristics can we know the best data structure for the task.
These form a programmer‘s programmer s basic data structure “toolkit”.(基本的数据结构工具箱)
3.
Understand how to measure the cost of a data structure or program.
16
相关操作:插入、删 除、查找记录
10090 100A0
15
100A0 970161 曾小波 90 10040 100B0
顺序存储 100B0
链式存储
Βιβλιοθήκη Baidu抽象数据类型(Abstract Data Type)
抽象数据类型(Abstract Data Type) (cont) -i
复数ADT:
数据项: 基本操作:
Space Time
(total space to store data) (time allowed to perform)
A solution can also be efficient if it requires fewer resources than other known solutions
e1(实部),e2(虚部)
ADT不需要关心数据类型是如何实现的
封装(Encapsulation):
复数
得到一个复数的实部值 得到一个复数的虚部值 两个复数相加 两个复数相乘
18
Hide implementation details.
每一个ADT上的操作由其输入和输出来定义 (Each ADT operation is defined by its inputs and outputs.)
每一个数据结构都有其代价和效益(Each data structure has costs and benefits.) 一个数据结构几乎不可能在任何情况下都比 另 个要好 (Rarely is one data structure 另一个要好 better than another in all situations.) A data structure requires:
UillhtY Description add text to the end of a string return t -1, 1 0 0, or 1 d depending di on relative l ti ordering d i delete text from a string starting at given index first or last index where the start of str appears in this string (returns string::npos if not found) add text into a string at a given index number of characters in this string replaces len chars at given index with new text the next length characters beginning at start (inclusive); if length omitted, grabs till end of string
7
Bank example:
Start account: a few minutes Transactions: a few seconds Close
2.
account: overnight
3.
8
Some Questions to Ask
Data Structure Philosophy 关于数据结构的基本观念
space time
When choosing data structures, here are some questions to ask.
是不是所有的数据都在一开始时就插入数据结构,
还是插 入操作是和其它操作夹杂在 起进行?(Are all data items 入操作是和其它操作夹杂在一起进行 inserted into the data structure at the beginning, or are the insertions interspersed with other operations?) 数据能否被删除? (Can data items be deleted?) 是否所有数据都需按一个定义好的顺序进行访问, 还是允 许随机存取?(Are all data processed in some welldefined order, or is random access allowed?)
3-8i i
抽象数据类型(ADT)是未指明具体实现的一组 值的集合及其相关操作(A set of data values
and associated operations that are specified independent of any particular implementation. )
第一部分
预备知识
第1章 Chapter 1
数据结构与算法 (Data Structures and Algorithms)
1
2
What is a data structure?
最广义地说, 数据结构是指数据的表示及其相 关操作(In the most general, a data structure is any y data representation p and its associated operations.) 更典型地讲, 数据结构是指一组数据项的组织 或结构(More typically, a data structure is meant to be an organization or structuring for a collection of data items.)
5 6
1
Choosing your Data Structure
1.
Choosing your Data Structure
You should follow these steps
分析问题, 确定解必须满足的资源限制 (Analyze your problem to determine the resource constraints that any solution must meet) 确定必须支持的基本操作, 对每一个操作的资源限 制进行量化(Determine the basic operations that must be supported and quantify the resource constraints for each operation) 选择能最好地满足要求的数据结构(Select the data structure that best meets these requirements)
例:整数类型
(或、与、非)的统一体; 整数类型就是整数值与若干算术运算(加、减、 乘、除、取模)的统一体; 复数类型就是复数值与若干复数运算的统一体; 线性表(List)类型就是列表与插入、删除、清 空、查找等操作的统一体 13
在使用 int i 或 long i 时我们通常只关心 i 是整数,可以对它加减 乘除,并未考虑其实现问题
17
3
抽象数据类型(Abstract Data Type) (cont)
Grader的ADT
抽象数据类型(Abstract Data Type) (cont)
String的ADT
Operations s.append(str) s.compare( (str t) s.erase(index, length) s.find(str) s.rfind(str) s.insert(index, str) s.length() or s.size() s.replace(index, len, str) s.substr(start, length) or s.substr(start)
数据类型(data type):一组值的集合及其 相关操作(A set of data values and associated operations )
逻辑类型就是逻辑值(是、否)与 逻辑类型就是逻辑值(是 否)与3种逻辑运算
数据类型的逻辑概念(使用者所关心的)和它的物 理实现(生产者所关心的)之间是有区别的(There is a distinction between the logical concept of a data type and its physical implementation)
These techniques also allow you to judge the merits of new data structures that you or others might invent.(评判新数据结构的价值)
12
11
2
数据类型(Data Type)
数据类型(Data Type)
课程目标(Goals of this Course)
1.
Reinforce the concept that costs and benefits exist for every data structure. Learn the commonly used data structures.
2.
用4个字节存放一个整数?用8个字节? 进行加法时,用二进制如何进行?
例:线性表(Lists)可以通过链式存储也可通过数组的方
式来实现
14
一个线性表
10010 970161 曾小波 90 10020 970166 杨雪辉 89 10030 970164 胡 涛 95 10040 970159 李启明 91 10050 970160 刘 华 78 10060 970157 李小青 83 10070 970168 梅 玲 65 10080 970163 王 伟 65
10010 10020 10030 10040 10050 10060 10070 10080 10090 Head
970159 李启明 91 100B0 970164 胡 涛 95 10010 970166 杨雪辉 89 10030 970157 李小青 83 10090 970163 王 伟 65 NULL 970168 梅 玲 65 10080 970160 刘 华 78 10060
4
Efficiency(效率) and Cost(代价)
Efficiency and Cost (cont)
A solution to a problem is efficient(有效 率的) if it solves the problem within the required resource constraints(资源限制)
解的代价是指其消耗的资源量的总和(The cost of a solution is the amount of resources that the solution consumes.)
Often
the cost is measured in terms of one key resource
3
Organizing Data
一组记录的任何一种组织(结构)都可能被检索、 按任意顺序被处理或被修改(Any organization for a collection of records can be searched, processed d in i any order, d or modified.) difi d ) 对数据结构和算法的不同选择会造成程序运行时 间的很大不同: 几秒钟或者很多天(The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days.)
9
for each data item it stores, to perform each basic operation, programming effort.
10
Data Structure Philosophy (cont)
每个问题都有可用空间和时间的限制. (Each problem has constraints on available space and time.) Only after a careful analysis of problem characteristics can we know the best data structure for the task.
These form a programmer‘s programmer s basic data structure “toolkit”.(基本的数据结构工具箱)
3.
Understand how to measure the cost of a data structure or program.
16
相关操作:插入、删 除、查找记录
10090 100A0
15
100A0 970161 曾小波 90 10040 100B0
顺序存储 100B0
链式存储
Βιβλιοθήκη Baidu抽象数据类型(Abstract Data Type)
抽象数据类型(Abstract Data Type) (cont) -i
复数ADT:
数据项: 基本操作:
Space Time
(total space to store data) (time allowed to perform)
A solution can also be efficient if it requires fewer resources than other known solutions
e1(实部),e2(虚部)
ADT不需要关心数据类型是如何实现的
封装(Encapsulation):
复数
得到一个复数的实部值 得到一个复数的虚部值 两个复数相加 两个复数相乘
18
Hide implementation details.
每一个ADT上的操作由其输入和输出来定义 (Each ADT operation is defined by its inputs and outputs.)
每一个数据结构都有其代价和效益(Each data structure has costs and benefits.) 一个数据结构几乎不可能在任何情况下都比 另 个要好 (Rarely is one data structure 另一个要好 better than another in all situations.) A data structure requires:
UillhtY Description add text to the end of a string return t -1, 1 0 0, or 1 d depending di on relative l ti ordering d i delete text from a string starting at given index first or last index where the start of str appears in this string (returns string::npos if not found) add text into a string at a given index number of characters in this string replaces len chars at given index with new text the next length characters beginning at start (inclusive); if length omitted, grabs till end of string
7
Bank example:
Start account: a few minutes Transactions: a few seconds Close
2.
account: overnight
3.
8
Some Questions to Ask
Data Structure Philosophy 关于数据结构的基本观念
space time
When choosing data structures, here are some questions to ask.
是不是所有的数据都在一开始时就插入数据结构,
还是插 入操作是和其它操作夹杂在 起进行?(Are all data items 入操作是和其它操作夹杂在一起进行 inserted into the data structure at the beginning, or are the insertions interspersed with other operations?) 数据能否被删除? (Can data items be deleted?) 是否所有数据都需按一个定义好的顺序进行访问, 还是允 许随机存取?(Are all data processed in some welldefined order, or is random access allowed?)