77
Implementing Data Structures 資資資資資資

Implementing Data Structures

Embed Size (px)

DESCRIPTION

Implementing Data Structures. 資料結構實做. Storing Arrays. 儲存陣列 有點類似「表格」概念,每一格儲存我們要的資訊. Homogeneous Arrays. 同質陣列 長方形的資料區塊每一項有相同性質. Homogeneous Arrays. Row-major order versus column major order Address polynomial for each of them is continuous 由 列與行 來儲存的,例如 :4 列* 4 行 = 16 個儲存空間,並且 編號是連續 的。. - PowerPoint PPT Presentation

Citation preview

Page 1: Implementing Data Structures

Implementing Data Structures資料結構實做

Page 2: Implementing Data Structures

Storing Arrays• 儲存陣列 有點類似「表格」概念,每一格儲

存我們要的資訊

Page 3: Implementing Data Structures

Homogeneous Arrays

• 同質陣列長方形的資料區塊每一項有相同性質

Page 4: Implementing Data Structures

Homogeneous Arrays

• Row-major order versus column major order• Address polynomial for each of them is continuous 由列與行來儲存的,例如 :4 列 *4 行 = 16 個

儲存空間,並且編號是連續的。

Page 5: Implementing Data Structures

Figure 8.5• int Readings[24]; // 宣告一個可存 24 個整數的空間(陣列)

The array of temperature readings stored in memory starting at address x

記憶單位

位址

Page 6: Implementing Data Structures

Figure 8.5• int Readings[24]; // 宣告一個可存 24 個整數的空間(陣列)

• Readings[4]67;

The array of temperature readings stored in memory starting at address x

67記憶單位

位址

Page 7: Implementing Data Structures

多維陣列

Page 8: Implementing Data Structures

多維陣列• int a[5]; 一維陣列

Page 9: Implementing Data Structures

多維陣列• int a[5]; 一維陣列• int b[5][5]; 二維陣列

Page 10: Implementing Data Structures

多維陣列• int a[5]; 一維陣列• int b[5][5]; 二維陣列• int c[5][5][5]; 三維陣列

Page 11: Implementing Data Structures

多維陣列• int a[5]; 一維陣列• int b[5][5]; 二維陣列• int c[5][5][5]; 三維陣列• int d[5][5][5][5];• int e[5][5][5][5][5];• ......

那這些多維的你能想像嗎! ?

Page 12: Implementing Data Structures

• Row-major (row by row)

• Column major (column by column) 個整數的空間(陣列)

Page 13: Implementing Data Structures

• Row-major (row by row)

• Column major (column by column) 個整數的空間(陣列)

Page 14: Implementing Data Structures

• Row-major (row by row)

• Column major (column by column) 個整數的空間(陣列)

ex:Pascal, C/C++, Java, C#

ex:FORTRAN

Page 15: Implementing Data Structures

ex:Pascal, C/C++, Java, C#

ex:FORTRAN

• Row-major (row by row)

• Column major (column by column)

x x+1 x+2 x+3

x+4 x+5 x+6 x+7

x+8 x+9 x+10 x+11

x+12 x+13 x+14 x+15

x x+4 x+8 x+12

x+1 x+5 x+9 x+13

x+2 x+6 x+10 x+14

x+3 X+7 x+11 x+15

0 1 2 3

1 2 3 4

0 1 2

1 2 3

Page 16: Implementing Data Structures

16

Address polynomial• int a[r][c]

// 宣告一個 r(row 行 ) *c(column 列 ) 的二維陣列 1 ...... c

1 a[1][1] ...... a[1][c]2 a[2][1] ...... a[2][c]3 a[3][1] ...... a[3][c]

......

...... ...... ......

r a[r][1] ...... a[r][c]

Page 17: Implementing Data Structures

17

Address polynomial• int a[r][c]

// 宣告一個 r(row 行 ) *c(column 列 ) 的二維陣列 1 ...... c

1 a[1][1] ...... a[1][c]2 a[2][1] ...... a[2][c]3 a[3][1] ...... a[3][c]

......

...... ...... ......

r a[r][1] ...... a[r][c]

我的位址是 x

Page 18: Implementing Data Structures

18

Address polynomial• int a[r][c]

// 宣告一個 r(row 行 ) *c(column 列 ) 的二維陣列 1 ...... c

1 a[1][1] ...... a[1][c]2 a[2][1] ...... a[2][c]3 a[3][1] ...... a[3][m]

......

...... a[i][j] ......

r a[r][1] ...... a[r][c]

我的位址是 x

那我呢 ???

Page 19: Implementing Data Structures

19

Address polynomial• int a[r][c]

// 宣告一個 r(row 行 ) *c(column 列 ) 的二維陣列 1 ...... c

1 a[1][1] ...... a[1][c]2 a[2][1] ...... a[2][c]3 a[3][1] ...... a[3][c]

......

...... a[i][j] ......

r a[r][1] ...... a[r][c]

x (row major order)

a[i][j] 的位址 : x+c*(i-1)+(j-1)

Page 20: Implementing Data Structures

20

Address polynomial• int a[r][c]

// 宣告一個 r(row 行 ) *c(column 列 ) 的二維陣列 1 ...... c

1 a[1][1] ...... a[1][c]2 a[2][1] ...... a[2][c]3 a[3][1] ...... a[3][c]

......

...... a[i][j] ......

r a[r][1] ...... a[r][c]

x (row major order)

a[i][j] 的位址 : x+c*(i-1)+(j-1)

Address polynomial位址多項式

Page 21: Implementing Data Structures

Example• int a[3][4]

1 2 3 4

1

2 target

3

x

Page 22: Implementing Data Structures

Example• int a[3][4]

1 2 3 4

1

2 target

3

x

• Row-major order x+4*(2-1)+(3-1)=x+6

Page 23: Implementing Data Structures

Example• int a[3][4]

1 2 3 4

1

2 target

3

x

• Row-major order x+4*(2-1)+(3-1)=x+6• Column-major order x+3*(3-1)+(2-1) =x+7

Page 24: Implementing Data Structures

Figure 8.6• int Readings[4][5]; // 宣告一個可存 4*5 個整數的空間(陣

列)

A two-dimensional array with four rows and five columns stored in row major order

機器之記憶體

第 3 列第 4 行之值

概念性陣列

Page 25: Implementing Data Structures

Figure 8.6• int Readings[4][5]; // 宣告一個可存 4*5 個整數的空間(陣

列)

A two-dimensional array with four rows and five columns stored in row major order

機器之記憶體

第 3 列第 4 行之值

概念性陣列x+1 x+2 x+3 x+4 x+5

x+6 x+7 x+8 x+9 x+10

x+11 x+12 x+13 x+14 x+15

x+16 x+17 x+18 x+19 x+20

位址

Page 26: Implementing Data Structures

Heterogeneous arrays

• 異質陣列 每一項可能不同類型稱作文件

Page 27: Implementing Data Structures

Heterogeneous arrays

• Components can be stored one after the other in a contiguous block

• Components can be stored in separate locationsidentified by pointers

它儲存在連續的空間 , 他也可以用指標分開來儲存在不同的位置,應用在 struct/class

Page 28: Implementing Data Structures

Figure 8.7

• Storing the heterogeneous array Employee

a. 陣列儲存在連續區間

b. 陣列組件儲存在分開區間

Page 29: Implementing Data Structures

Figure 8.7

• Storing the heterogeneous array Employee

a. 陣列儲存在連續區間

b. 陣列組件儲存在分開區間

個別項目通常被稱為元件( components )

Page 30: Implementing Data Structures

Storing Lists• 儲存列表

例如儲存一串名字的 list 到電腦主記憶體之中

Page 31: Implementing Data Structures

Contiguous list

連續串列• 優點:靜態時為便利的儲存結構 • 缺點:動態下極存不便

Page 32: Implementing Data Structures

Figure 8.8•

Names stored in memory as a contiguous list

記憶單位連續區塊

第一名稱儲存區塊

Page 33: Implementing Data Structures

linked list

鏈結串列• 簡單說:將各 list 的入口用 pointer 連結起來 • 儲存在分散的記憶單元裡

- 頭指標( head pointer ):指標指到串列的開端

- 空指標( NLT pointer ):最後一項指標中放NIL

Page 34: Implementing Data Structures

linked list

鏈結串列• 簡單說:將各 list 的入口用 pointer 連結起來 • 儲存在分散的記憶單元裡

- 頭指標( head pointer ):指標指到串列的開端

- 空指標( NLT pointer ):最後一項指標中放NIL

NIL

Page 35: Implementing Data Structures

Figure 8.9•

The structure of a linked list

頭指標

名稱 指標

空指標

Page 36: Implementing Data Structures

• linked list 可說是一系列 node (節點)連結

• 每個節點至少包含– data ( 資料 )–指到下一個節點的指標

A

Head

B C

A

data pointer

node

Page 37: Implementing Data Structures

Figure 8.10•

Deleting an entry from a linked list

頭指標

名稱 指標

舊指標

新指標 空指標

刪除的項目

Page 38: Implementing Data Structures

• 將 A 節點的連結指標指向 C

BA C

BA C

Page 39: Implementing Data Structures

Figure 8.11•

Inserting an entry into a linked list

頭指標

名稱 指標

舊指標

新指標 新指標

空指標

新增的項目

Page 40: Implementing Data Structures

• 將 B 節點的連結指標指向 C ( node.next )

• 將 A ( node )節點的連結指標指向 B

A AC C

B B

Page 41: Implementing Data Structures

• 將 B 節點的連結指標指向 C ( node.next )

• 將 A ( node )節點的連結指標指向 B

A AC C

B B

步驟不能交換 !!! Why?

Page 42: Implementing Data Structures

Storing Stacks and Queues• 儲存堆疊及佇列

Page 43: Implementing Data Structures

Stack

堆疊• FILO : ( First-In,Last-Out )先進後出

Page 44: Implementing Data Structures

Stack

堆疊• FILO : ( First-In,Last-Out )先進後出

• 記憶體需保留些連續區塊以供成長及縮小• 頂端位置在保留記憶體區塊中前後移動,其單

元稱 stack pointer

Page 45: Implementing Data Structures

Figure 8.12•

A stack in memory

堆積的基底

保留區塊的記憶單元

堆積項目

堆積指標

供成長的空間

Page 46: Implementing Data Structures

Example

Page 47: Implementing Data Structures

Example

Push 紅球

Page 48: Implementing Data Structures

Example

Push 綠球

Page 49: Implementing Data Structures

Example

Push 藍球

Page 50: Implementing Data Structures

Example

Pop

Page 51: Implementing Data Structures

Example

Page 52: Implementing Data Structures

Example

Pop

Page 53: Implementing Data Structures

Example

Page 54: Implementing Data Structures

Example

Pop

Page 55: Implementing Data Structures

Example

Page 56: Implementing Data Structures

Queue

佇列• FIFO : ( First-In,First-Out ) 先進後

Page 57: Implementing Data Structures

Queue

佇列• FIFO : ( First-In,First-Out ) 先進後出

• 以連續記憶體區塊做保留二單元做指標 ( head pointer 頭指標 & tail pointer

尾指標)

Page 58: Implementing Data Structures

Figure 8.13

•A queue implementation with head and tail pointers. Note how the queue crawls through memory as entries are inserted and removed.

a. 空佇列 b. 插入項目 A,B,C 之後

c. 移除項目 A 插入 D 之後 d. 移除項目 B 插入 E 之後

Page 59: Implementing Data Structures

缺點• queue 若不作控制,記憶體緩慢移動,且

經過地方所存的資料被摧毀 • 改善方法 環狀序列( circular queue )

Page 60: Implementing Data Structures

Figure 8.14•

A circular queue containing the letters P through V

概念性的 queue 儲存結構(最後一個單元連到第一個單元)

實際的 queue 儲存結構

Page 61: Implementing Data Structures

Figure 8.14•

A circular queue containing the letters P through V

概念性的 queue 儲存結構(最後一個單元連到第一個單元)

實際的 queue 儲存結構

A

Head

B C

Page 62: Implementing Data Structures

Storing Binary Trees儲存二元樹

Page 63: Implementing Data Structures

Storing Binary Trees儲存二元樹

Page 64: Implementing Data Structures

Storing Binary Trees儲存二元樹

• 每個節點 = 資料 + 左右孩子指標

Left child pointer Right child pointer

Page 65: Implementing Data Structures

Storing Binary Trees儲存二元樹

• 每個節點 = 資料 + 左右孩子指標• 通過根節點指標來做存取

Page 66: Implementing Data Structures

Figure 8.15 The structure of a node in a binary tree

包含資料的記憶單元 左子指標 右子指標

Page 67: Implementing Data Structures

Figure 8.16

•The conceptual and actual organization of a binary tree using a linked storage system

概念性的樹

實際的儲存結構

Page 68: Implementing Data Structures

Figure 8.16

•The conceptual and actual organization of a binary tree using a linked storage system

概念性的樹

實際的儲存結構根節點

Page 69: Implementing Data Structures

Figure 8.16

A

B C

D E F

NIL NIL NIL NIL NIL

NIL

Page 70: Implementing Data Structures

Figure 8.17•

A tree stored without pointers

概念性的樹

實際的儲存結構

根節點樹第二層中的節點 樹第三層中的節點

Page 71: Implementing Data Structures

Figure 8.18•

A sparse, unbalanced tree shown in its conceptual form and as it would be stored without pointers

概念性的樹

實際的儲存結構

根 第二層 第三層 第四層

Page 72: Implementing Data Structures

Figure 8.18•

A sparse, unbalanced tree shown in its conceptual form and as it would be stored without pointers

概念性的樹

實際的儲存結構

根 第二層 第三層 第四層

太浪費記憶體空間了 !!!

Page 73: Implementing Data Structures

靜態 vs 動態結構• 靜態結構比動態結構易於管理,

且可直接存取(動態須由指標一個指一個)

• 靜態加入、刪除及合併時,必須做大量資料的移動;動態則只須要改變指標即可

Page 74: Implementing Data Structures

Manipulating Data Structures資料結構的操作

• 藉由事先已定義的程序( procedure )來完成資料結構的操作

• Example : stack 堆疊最少需要 push (堆入)和 pop (移除)兩個 procedures 來完成操作

Page 75: Implementing Data Structures

Figure 8.19•

A procedure for printing a linked list

Page 76: Implementing Data Structures

Figure 8.19•

A procedure for printing a linked list

可以將鏈結串列的名單列出

Page 77: Implementing Data Structures

The end