54
1 数数数数 Data Structures 数数数数 数数数 Instructor: Zhenming Yuan E-mail: [email protected] Courseware and homework sets can be downloaded from http:// jsj.hznu.edu.cn/ds http://jwc.hznu.edu.cn, 数数数数

数据结构 Data Structures

Embed Size (px)

DESCRIPTION

数据结构 Data Structures. 主讲教师: 袁贞明 Instructor: Zhenming Yuan E-mail: [email protected]. Courseware and homework sets can be downloaded from http://jsj.hznu.edu.cn/ds http://jwc.hznu.edu.cn , 网络课堂. 数据结构. 数据结构是一门研究 非数值计算 的程序设计问题中计算机的操作对象以及它们之间关系和操作的学科。. 图书馆检索书签 - PowerPoint PPT Presentation

Citation preview

Page 1: 数据结构 Data Structures

1

数据结构Data Structures

主讲教师: 袁贞明 Instructor: Zhenming Yuan

E-mail: [email protected]

Courseware and homework sets can be downloaded from http://jsj.hznu.edu.cn/ds

http://jwc.hznu.edu.cn, 网络课堂

Page 2: 数据结构 Data Structures

数据结构 数据结构是一门研究非数值计算的程序设计问题中计算机的操作对象以及它们之间关系和操作的学科。 图书馆检索书签

查阅图书目录卡片:按书名编排的、按作者编排的、按分类编排的…计算机检索时,处理的对象便是这些目录卡片上的书目信息。

001

高等数学 樊映川 S01

……

002

理论力学 罗远祥 L01 ……

003

高等数学 华罗庚 S01

……

004

线性代数 栾汝书 S02

……

… … … … …

高等数学 001, 003…

理论力学 002,…

线性代数 004,…

樊映川 S01 ……

华罗庚 S01 ……

栾汝书 S02 ……

… … … 2

Page 3: 数据结构 Data Structures

人机对奕• 计算机之所以能和人对奕,策略事先存入计算机。对奕过程在

一定规则下随机进行,为使计算机灵活对奕,就必须对所有可能发生的情况以及相应的对策都考虑周全,还应能预测发展趋势,甚至最后结局。

3

Page 4: 数据结构 Data Structures

田径赛• 田径赛的时间安排问题

– 设有六个比赛项目,规定每个选手至多可参加三个项目,有五人报名参加比赛(如下表所示)设计比赛日程表,使得在尽可能短的时间内完成比赛。

姓 名 项目 1 项目 2 项目 3 丁 一 跳高 跳 远 100 米 马 二 标 枪 铅 球 张 三 标 抢 100 米 200 米 李 四 铅 球 200 米 跳 高 王 五 跳 远 200 米

4

Page 5: 数据结构 Data Structures

解决方法 ( 无向图的着色问题 )设用如下六个不同代号代表不同的项目:跳高 跳远 标枪 铅球 100 米 200 米A B C D E F用顶点代表比赛项目不能同时进行比赛的项目之间连上一条边。某选手比赛的项目必定有边相连(不能同时比赛)。

F B王五 A F D李四 F E C张三 D C马二 E B A丁一

项目3

项目2

项目1

姓名

A

E

B

F

D C F4

E3

B, D2

A , C1

比赛项目比赛时间

5

Page 6: 数据结构 Data Structures

课程主要内容• 主要内容包括

– 数组、链接表、栈和队列、递归、树与森林、图、堆与优先队列、集合与搜索结构、排序与散列等。

• 课程基本要求– 掌握数据结构的概念、使用方法及实现技术;– 理解算法分析方法(时间代价、空间代价)

• 学习方法– 预习 + 复习– 上课跟着幻灯片– 充分利用实验课– 课后必须自己练!

6

Page 7: 数据结构 Data Structures

7

课程目标

• 让同学们真正能– 设计合理的数据结构– 进一步提高编程能力

• 方法– 实践– 实践– 实践 Practice makes perfect

Page 8: 数据结构 Data Structures

8

教材 (Text Book)

Fundamentals of Data Structures in C

(2nd Edition)

E. Horowitz, S. Sahni

http://www.cise.ufl.edu/~sahni/

Page 9: 数据结构 Data Structures

9

历史教材 (Text Book)

~02 级 02级 ~03 级

04级 ~09 级 10级 ~

Page 10: 数据结构 Data Structures

10

参考书目 (Reference)

( 中译本 )E.Horowitz, S.Sahni, S.Anderson-Freed著 , 朱仲涛译 . 数据结构基础 (C 语言版 )(第 2版 ). 清华大学出版社 , 2009

M. A. Weiss著 , 冯舜玺译 . 数据结构与算法分析 -C 语言描述 . 机械工业出版社 , 2004.1

严蔚敏 , 吴伟民编著 , 数据结构 (C 语言版 ). 清华大学出版社 , 1997

严蔚敏 , 吴伟民编著 , 数据结构题集 (C 语言版 ). 清华大学出版社 , 1999

Page 11: 数据结构 Data Structures

11

课程评分方法 (Grading Policies)

总评成绩 (100) = 平时成绩 (30, 点名 , 提问 , 作业 )

期中成绩 (20)

期末成绩 (50)

实验总评成绩 (100) = 平时成绩 (10, 点名 , 提问 )

Projects(50)

期末上机考试 (40)

Page 12: 数据结构 Data Structures

12

预备知识

• 基本编程知识– 编程工具: Visual C++ 2005 英文版

• C 语言– 重点复习指针和结构体部分。

• 学习方法– 以上课课件为主线把问题弄清楚,重要的地

方要翻译– 边学习边实践

• 喜欢编程

Page 13: 数据结构 Data Structures

Patterns of teaching• Schedule of teaching

– 以 《 Fundamentals of Data Structures in C 》的顺序讲解,但内容不限于该书!

– 在网上有每次课的幻灯片可下载 , 请适当记笔记 !• Methods of teaching

– 原版教材 , 英语讲稿– http://jsj.hznu.edu.cn/ds 网上课堂进行作业上交,答疑– 可以在 Wiki 上讨论 !

• Programming Experiment– 3 节上课, 2 节上机– 所有程序均由C 语言编写,环境: Visual C++– 作业

• 平时作业 Homework• 上机作业 Programming

13

Page 14: 数据结构 Data Structures

Time Demand

[The time of Programming Experiment in Lab ]

•Deadline of Experiment – get 100% score

•A week later – get 30% score

•More than a week later – 0 score

[The time of Homework]

•Deadline of Homework – get 100% score

•A week later – get 50% score

•Two weeks later – 0 score

14

Page 15: 数据结构 Data Structures

15

作业 (Homework Assignments)

课后作业: 4-5次 , 每次指定时间收一次; 课堂作业:上课布置后马上交 做则有分,不计对错。 作业上交后一周公布参考答案。 独立完成 相互帮助 绝不允许抄袭!

Page 16: 数据结构 Data Structures

[The format of Programming Report]

• Experimental Title

• Experimental Demand

• Data Structure Describe (ADT in C)

• Algorithms Describe (Flow chart is the better)

• Key codes (Functions in C)

• Debugging records

• Results

• Comments (Summarize and suggestions)

• Source code files

16

Page 17: 数据结构 Data Structures

17

Course Website

http://jsj.hznu.edu.cn/ds

http://jwc.hznu.edu.cn, 网络课堂

Page 18: 数据结构 Data Structures

18

Resources

• Codeguru: http://www.codeguru.com/ • Codeproject: http://www.codeproject.com/ • VC 知识库: http://www.vckbase.com/ • MSDN 中文网站: http://www.microsoft.com/

china/msdn/default.aspx

Page 19: 数据结构 Data Structures

19

Q&A

Page 20: 数据结构 Data Structures

08/ 9 / 10 杭州师范大学信息科学与工程学院 袁贞明

Chapter 1 Introduction

Page 21: 数据结构 Data Structures

1. System life cycle

• The system development process

requirement

analysis

design

coding

verification

the purpose of the project(input, output )

break the problem down into manageable pieces

Bottom - up (unstructed)

Top - down (structed)

data object + operations <---language independance

abstract data type algorithm specification

choose concrete representation for data objects

and write algorithm for each operatios on them

developing correctness for programstesting the program with variety of input dataremoving errors*correctness running time

p1--p4

21

Page 22: 数据结构 Data Structures

22

2. 什么是数据结构Niklaus Wirth

《 Algorithm+Data Structures = Programming 》

程序设计:为计算机处理问题编制的一组指令集;

算 法: 处理问题的策略;数据结构:问题的数学模型。

Page 23: 数据结构 Data Structures

23

2. 1 基本概念和术语

数据:所有能被输入到计算机中,且被计算 机处理的符号的集合,计算机操作对象的总称。

是计算机处理的信息的某种特定的符号表示形式。

Page 24: 数据结构 Data Structures

24

数据元素:数据中的一个“个体”,数据结构中讨论的基本单位。

数据项:数据结构中讨论的最小单位

数据元素是数据项的集合

例如:图书馆中的书(数据元素)

书名 作者号 分类号 出版社 出版时间 …

数据结构 003 TP311.13 清华大学出版社

1997

… … … … …

Page 25: 数据结构 Data Structures

25

数据结构:带结构的数据元素的集合。

例如,一个含 12 位数的十进制数可以用三个4 位的十进制数表示。

3214,6587,9345---a1(3214),a2(6587),a3(9345)

在 a1、 a2和 a3之间存在“次序”关系<a1,a2>、 <a2,a3>

3214,6587,9345

a1 a2 a3

6587,3214,9345

a2 a1 a3

<a2,a1>、 <a1,a3>

Page 26: 数据结构 Data Structures

26

数据结构的形式定义为:

数据结构是一个二元组

Data_Structures=(D,S)

其中: D 是数据元素的有限集, S是 D

上关系的有限集。 这种关系描述的是数据元素之间的逻辑关系,因此又称为数据的逻辑结构。

Page 27: 数据结构 Data Structures

27

数据结构描述现实世界实体的数据对象 ( 非数值计算)及其上的操作在计算机中的表示和实现。

概括地说,

Page 28: 数据结构 Data Structures

2.2 数据结构关心的三方面内容• 逻辑结构

– 数据元素间抽象化的相互关系(简称为数据结构)。 与数据的存储无关,独立于计算机,它是从具体问题抽象出来的数学模型。

存储结构(物理结构)– 数据元素及其关系在计算机存储器中的存储方式。 是逻辑结构用计算机语言的实现 它依赖于计算机语言。

• 算法– 对于一种数据结构,常见的运算有:

• 建立数据结构• 清除数据结构• 插入一个新的数据元素• 删除某个数据元素• 修改某个数据元素• 排序• 检索

28

Page 29: 数据结构 Data Structures

29

3. 逻辑结构

数据的逻辑结构可归结为以下四类:

2.树形结构

1. 线性结构

3. 图状结构

4. 集合结构

Page 30: 数据结构 Data Structures

3.1 线性结构

• 线性结构 Linear structure– 仅有一个开始元素,仅有一个最后元素;其余都是内部元素,且都有且仅有一个直接前驱和一个直接后继。

– 线性表 B={K, R}• K={k0, k1,…, kn-1}

• R={r}, r={<ki-1,ki>|ki K,1<∈ i<n}

30

Page 31: 数据结构 Data Structures

3.2 非线性结构

• 非线性结构 Nonlinear structure – 各个数据成员不再保持在一个线性序列中。

每个数据成员可能与零个或多个其他数据成员发生联系。可分为层次结构和群结构。

– 层次结构• 按层次划分的数据元素的集合,指定层次上的元素,可以有零个或多个处于下一层次上的,直接的所属元素。例如:树结构(树、二叉树、堆)

– 群结构• 所有元素之间无顺序关系。例:集合就是一种群

结构。图结构也属于群结构。

31

Page 32: 数据结构 Data Structures

32

抽象数据类型( Abstract Data Type简称 AD

T)

是一个数据类型 ,其数据对象和对象上的操作的规格说明独立于对象的存储表示和对象上操作的实现 .

3.3 怎样描述逻辑结构?p18—p21

Definition: An abstract data type (ADT) is a data type that is organized in

such a way that the specification of the objects and the specification of

the operations on the objects is separated from the representation of

the objects and the implementation of the operations.

Page 33: 数据结构 Data Structures

33

抽象数据类型的描述方法

抽象数据类型可用(D,S,P)三元组表示

其中, D 是数据元素集,

S 是 D 上的关系集,

P 是对 D 的基本操作集。

Page 34: 数据结构 Data Structures

34

structure 抽象数据类型名 is

objects : < 数据对象的定义 > functions:< 基本操作的定义 >

end 抽象数据类型名

其中:数据对象的定义用伪码描述,基本操作的定义格式为

基本操作名 ( 参数表 )::=

if (初始条件 ) return 操作结果 ;

else return 错误 ;

Page 35: 数据结构 Data Structures

35

“初始条件”描述了操作执行之前数据结构和参数应满足的条件,若不满足,则操作失败,并返回相应出错信息。若初始条件为空,则省略之。

“ 操作结果”说明了操作正常完成之后,数据结构的变化状况和应返回的结果。

Page 36: 数据结构 Data Structures

36

Structure Natural_Number is Objects: 整数的有序子序列 , 该子序列从 0 开始 ,到计算机上的最大整数 (INT_MAX) 结束 . Functions: 对所有 x,y ∈ Natural_Number; TRUE, FALSE ∈ Boolean且其中 +,-,< 和 == 是通常所用的整数操作 . Nat_No Zero() ::= 0 Boolean Is_Zero(x) ::= if (x) return FALSE Else return TRUE Nat_No Add(x,y) ::= if ((x+y)<=INT_MAX) return x +y Else return INT_MAX Boolean Equal(x,y) ::= if (x==y) return TRUE Else return FALSE; Nat_No Successor(x) ::= if (x == INT_MAX) return x Else return x+1; Nat_No Substract(x,y) ::= if (x < y) return 0; Else return x – y; End Natural_Number

ADT Natural_Number

Page 37: 数据结构 Data Structures

37

4. 数据的存储结构

逻辑结构在存储器中的表示。四种基本的存储结构•顺序存储方法(结构) Sequential access •链接存储方法(链式存储结构) Linked access•索引存储方法 Indexing access•散列存储方法 Hash table

Page 38: 数据结构 Data Structures

38

4.1 顺序存储结构 (Arrays)

以存储位置的相邻表示后继关系。 y 的存储位置和 x 的存储位置之间差一个常量 C 。

而 C 是一个隐含值,因此,整个存储结构中只含数据元素本身的信息。

… x y …

Page 39: 数据结构 Data Structures

39

元素 n

……..

元素 i

……..

元素 2

元素 1Lo

Lo+c

Lo+(i-1)*c

Lo+( n-1)*c

存储地址 存储内容

Loc( 元素 i)=L0+(i-1)*c

顺序存储结构C 语言中如何表示顺序存储结构?

C 语言如何用指针访问数组元素?

&*

p4—p5

Page 40: 数据结构 Data Structures

40

1536元素 21400元素 1 1346元素 3 ∧元素 4

h

4.2 链式存储结构 (Linked Lists) 以附加信息(指针)表示后继。

Dynamic Memory Allocation

p5—p6

Page 41: 数据结构 Data Structures

41

4.3 索引结构

Page 42: 数据结构 Data Structures

42

例如:线性结构的元素存取方式顺序存取结构:不用访问元素前驱和后继即可访问该元素,如数组。

链式存取结构:只能从表的第一个元素按顺序访问,如链表。

词典结构(广义索引):字典与数组有相同之处,数组通过下标访问,字典通过关键码( key) 进行索引访问。

同一种逻辑结构可采用不同的存储方法(以上四种之一或组合),这主要考虑的是运算方便及算法的时空要求。

Page 43: 数据结构 Data Structures

43

5. Algorithm ( 算法 )

【Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task, In addition, all algorithms must satisfy the following criteria:

(1) Input. There are zero or more quantities that are externally applied.

(2) Output. At least one quantity is produced.(3) Definiteness. Each instruction is clear and unambiguous.(4) Finiteness. If we trace out the instructions of an

algorithm. then for all cases, the algorithm terminates after finite number of steps.

(5) Effectiveness. Every instruction must be basic enough to be carried out, in principle, by a person using only pencil and paper, It is not enough that each operation be definite as in (3); it also must be feasible.

The distinguishes between an algorithm and a program: Program----- does not have to satisfy(4) (operation system) a programming language Algorithm---- may was natural language、 graphic、 a programming language

p8

Page 44: 数据结构 Data Structures

44

Example 1.1 [Selection Sort]: Suppose we must devise a program that sorts a set of n≥1 integers.

From those integers that are currently unsorted, find the smallest and place it next in the sorted list.

Solution 1:

Program 1.2: Selection sort algorithm

for ( i=0; i < n; i ++) { Examine list[i] to list[n-1] and suppose that the smallest integer is at list[min]; Interchange list[i] and list[min];}

p9—p11

Page 45: 数据结构 Data Structures

45

Page 46: 数据结构 Data Structures

46

How to interchange two integers?

Program 1.3 Swap function

Macro version

Page 47: 数据结构 Data Structures

47

How to find the smallest integer in the unsorted list?

void sort(int list[], int n){ int i, j, min, temp;

}

Page 48: 数据结构 Data Structures

48

Example 1.2 [Binary Search]: Assume that we have n≥1 distinct integers that are already sorted and stored in the array list. That is, list[0]≤list[1] ≤ … ≤ list[n-1]. We must figure out if an integer searchnum is in this list. If it is we should return an index, i, such that list[i]=searchnum. If searchnum is not present, we should return -1.

left(0) middle right(n-1)

list[middle]searchnum ~

= >

iright=middle -1 lefit=middle +1

<

An algorithm is O( log N) if it takes constant (O(1)) time to cut the problem size by a fraction (which is usually 1/2)

p10—p13

Page 49: 数据结构 Data Structures

49

Program 1.5: Searching a sorted list

while ( there are more integers to check ) { middle = (left +right )/2; if ( searchnum < list[middle] ) right = middle - 1; else if ( searchnum == list[middle]) return middle; else left = middle + 1;}

Program 1.6: Comparison of two integers

#define COMPARE(x,y) ((x) < (y)? -1:((x) == (y)) ? 0:1)

Page 50: 数据结构 Data Structures

50

Program 1.7: Searching an ordered list

int binsearch ( int list [ ], int searchnum , int left, int right ){ /* search list[0] <= list[1] <= ··· <= list[n-1] for searchnum. Return its position if found . Otherwise return -1*/ int middle ; while ( left <= right ) { middle = ( left + right ) / 2; switch ( COMPARE ( list [ middle ] , searchnum )) { case -1 : left = middle + 1 ; break; case 0 : return middle; case 1 : right = middle - 1; } } return -1;}

Page 51: 数据结构 Data Structures

51

6. Recursive algorithms【 Definition 】 function that is defined in terms of itself is called recursive. (1) Direct recursion: the functions call themselves.(2) Indirect recursion: the functions call other functions that invoke the calling function again.

Factorials Fibonacci numbers

FUNCTION F1 F2 Fn

direct recursion indirect recursion

---

Page 52: 数据结构 Data Structures

52

Two fundamental rules of recursion

(1) Base cases: establish boundary conditions that terminate the recursive calls,

(2) Making progress: implement the recursive calls so that each call brings us one step closer to a solution.

Example 1#include <stdio.h> int F( int X ){/*1*/   if( X == 0 )/*2*/    return 0;    else/*3*/   return 2*F( X-1)+X*X; }main( ){ printf( "F(5) = %d\n", F( 5 ) ); return 0;}

Example 2#include <stdio.h> int B ( unsigned int N ){/*1*/   if( N == 0 )/*2*/    return 0;     else/*3*/    return B(N/3+1)+N-1;}main( ){   printf("B is infinite recursion\n" );   return 0;}

Page 53: 数据结构 Data Structures

53

Program 1.8: recursive version of binary search

int binsearch(int list[], int searchnum, int left, int right){ int middle; if (left <= right){ middle = (left + right) / 2; switch ( COMPARE(list[middle], searchnum)) { case –1: return binsearch(list, searchnum,middle + 1, right); case 0: return middle; case 1: return binsearch(list, searchnum, left, middle – 1); } } return –1;}

边界条件:1 )找到,则返回找到的数( middle )2 )找不到,则 left > right

Page 54: 数据结构 Data Structures

54

Homework

1. Translate #1.2.3(on page 7 to page 8) and Example 1.4(on page 15 to page 16) into Chinese.

2. Exercises on page 17, #4, #6, #7, #8