13
8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8 4 Customized Data Types 8.4 Customized Data Types 8.5 Classes and Objects 8.6 Pointers in Machine Language 8-2 Homogeneous array Heterogeneous array • List Stack Stack – Queue • Tree 8-3 8-4

LectureSlides1008 Data Abstraction.ppt [相容模式]homepage.ntu.edu.tw/~dwhuang/courses/cs/cs_08.pdf · •List – Stack – Queue ... using a linked storage system 8-30 8-31 Figure

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: LectureSlides1008 Data Abstraction.ppt [相容模式]homepage.ntu.edu.tw/~dwhuang/courses/cs/cs_08.pdf · •List – Stack – Queue ... using a linked storage system 8-30 8-31 Figure

• 8.1 Data Structure Fundamentals• 8.2 Implementing Data Structures• 8.3 A Short Case Study• 8 4 Customized Data Types• 8.4 Customized Data Types• 8.5 Classes and Objectsj• 8.6 Pointers in Machine Language

8-2

• Homogeneous array• Heterogeneous array• List

– StackStack– Queue

• Tree

8-3

8-4

Page 2: LectureSlides1008 Data Abstraction.ppt [相容模式]homepage.ntu.edu.tw/~dwhuang/courses/cs/cs_08.pdf · •List – Stack – Queue ... using a linked storage system 8-30 8-31 Figure

• List: A collection of data whose entries are arranged sequentiallyH d Th b i i f th li t• Head: The beginning of the list

• Tail: The end of the listTail: The end of the list

8-5

• Stack: A list in which entries are removed and inserted only at the headLIFO L t i fi t t• LIFO: Last-in-first-out

• Top: The head of list (stack)Top: The head of list (stack)• Bottom or base: The tail of list (stack)• Pop: To remove the entry at the top

i h• Push: To insert an entry at the top

8-6

• Queue: A list in which entries are removed at the head and are inserted at the tailFIFO Fi t i fi t t• FIFO: First-in-first-out

8-7

8-8

Page 3: LectureSlides1008 Data Abstraction.ppt [相容模式]homepage.ntu.edu.tw/~dwhuang/courses/cs/cs_08.pdf · •List – Stack – Queue ... using a linked storage system 8-30 8-31 Figure

• Tree: A collection of data whose entries have a hierarchical organizationN d A t i t• Node: An entry in a tree

• Root node: The node at the topRoot node: The node at the top• Terminal or leaf node: A node at the bottom

8-9

(continued)(continued)

• Parent: The node immediately above a specified nodeChild A d i di t l b l ifi d• Child: A node immediately below a specifiednode

• Ancestor: Parent, parent of parent, etc.• Descendent: Child, child of child, etc.• Siblings: Nodes sharing a common parent• Siblings: Nodes sharing a common parent

8-10

(continued)(continued)

• Binary tree: A tree in which every node has at most two childrenD th Th b f d i l t th• Depth: The number of nodes in longest path from root to leaf

8-11

8-12

Page 4: LectureSlides1008 Data Abstraction.ppt [相容模式]homepage.ntu.edu.tw/~dwhuang/courses/cs/cs_08.pdf · •List – Stack – Queue ... using a linked storage system 8-30 8-31 Figure

• Static Data Structures: Size and shape of data structure does not changeD i D t St t Si d h f• Dynamic Data Structures: Size and shape ofdata structure can changeg

• Pointers: Used to locate data

8-13

8-14

• Homogeneous arrays– Row-major order versus column major order– Address polynomialAddress polynomial

• Heterogeneous arrays– Components can be stored one after the other in a

contiguous block– Components can be stored in separate locations

identified by pointersidentified by pointers

8-15

8-16

Page 5: LectureSlides1008 Data Abstraction.ppt [相容模式]homepage.ntu.edu.tw/~dwhuang/courses/cs/cs_08.pdf · •List – Stack – Queue ... using a linked storage system 8-30 8-31 Figure

Figure 8.6 A two-dimensional array with four rows and five columns storedwith four rows and five columns storedin row major order

8-17

Figure 8.7 Storing the heterogeneous array Employee

8-18

• Contiguous list: List stored in a homogeneous arrayLi k d li t Li t i hi h h t i• Linked list: List in which each entries are linked by pointersy p– Head pointer: Pointer to first entry in list

NIL i t A “ i ” l d– NIL pointer: A “non-pointer” value used toindicate end of list

8-19

8-20

Page 6: LectureSlides1008 Data Abstraction.ppt [相容模式]homepage.ntu.edu.tw/~dwhuang/courses/cs/cs_08.pdf · •List – Stack – Queue ... using a linked storage system 8-30 8-31 Figure

8-21

8-22

8-23

• Stacks usually stored as contiguous lists• Queues usually stored as Circular Queues

– Stored in a contiguous block in which the first entry is considered to follow the last entry

– Prevents a queue from crawling out of its allotted storage spacestorage space

8-24

Page 7: LectureSlides1008 Data Abstraction.ppt [相容模式]homepage.ntu.edu.tw/~dwhuang/courses/cs/cs_08.pdf · •List – Stack – Queue ... using a linked storage system 8-30 8-31 Figure

8-25

Figure 8.13 A queue implementation with head and tail pointers

8-26

• Linked structure– Each node = data cells + two child pointers

A d i i t t t d– Accessed via a pointer to root node• Contiguous array structureg y

– A[1] = root node– A[2],A[3] = children of A[1]– A[4],A[5],A[6],A[7] = children of A[2] and A[3]A[4],A[5],A[6],A[7] children of A[2] and A[3]

8-27

8-28

Page 8: LectureSlides1008 Data Abstraction.ppt [相容模式]homepage.ntu.edu.tw/~dwhuang/courses/cs/cs_08.pdf · •List – Stack – Queue ... using a linked storage system 8-30 8-31 Figure

8-29

Figure 8.16 The conceptual and actual organization of a binary treeactual organization of a binary treeusing a linked storage system

8-30

8-31

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

8-32

Page 9: LectureSlides1008 Data Abstraction.ppt [相容模式]homepage.ntu.edu.tw/~dwhuang/courses/cs/cs_08.pdf · •List – Stack – Queue ... using a linked storage system 8-30 8-31 Figure

• Ideally, a data structure should be manipulated solely by pre-defined procedures.

Example: A stack typically needs at least push– Example: A stack typically needs at least pushand pop procedures.

– The data structure along with these procedures constitutes a complete abstract tool.co st tutes a co p ete abst act too .

8-33

8-34

Problem: Construct an abstract tool consisting of a list of names in alphabetical order along with the operations search print and insertthe operations search, print, and insert.

8-35

8-36

Page 10: LectureSlides1008 Data Abstraction.ppt [相容模式]homepage.ntu.edu.tw/~dwhuang/courses/cs/cs_08.pdf · •List – Stack – Queue ... using a linked storage system 8-30 8-31 Figure

Figure 8.21 The binary search as it would appear if the list wereit would appear if the list wereimplemented as a linked binary tree

8-37

Figure 8.22 The successively smaller trees considered by the procedure in Figureconsidered by the procedure in Figure8.18 when searching for the letter J

8-38

8-39

8-40

Page 11: LectureSlides1008 Data Abstraction.ppt [相容模式]homepage.ntu.edu.tw/~dwhuang/courses/cs/cs_08.pdf · •List – Stack – Queue ... using a linked storage system 8-30 8-31 Figure

Figure 8.25 Inserting the entry M into the list B E G H J K N PM into the list B, E, G, H, J, K, N, Pstored as a tree

8-41

8-42

• A template for a heterogeneous structure• Example:define type EmployeeType to be

{char Name[25];{char Name[25];int Age;real SkillRating;}}

8-43

• A user-defined data type with procedures for access and manipulationmanipulation

• Example:define type StackType to be{int StackEntries[20];int StackPointer = 0;procedure push(value)procedure push(value)

{StackEntries[StackPointer] ← value;StackPointer ¬ StackPointer + 1;

}procedure pop . . .

}}

8-44

Page 12: LectureSlides1008 Data Abstraction.ppt [相容模式]homepage.ntu.edu.tw/~dwhuang/courses/cs/cs_08.pdf · •List – Stack – Queue ... using a linked storage system 8-30 8-31 Figure

• An abstract data type with extra features– Characteristics can be inherited

C t t b l t d– Contents can be encapsulated– Constructor methods to initialize new objectsj

8-45

8-46

• Immediate addressing: Instruction contains the data to be accessedDi t dd i I t ti t i th• Direct addressing: Instruction contains the address of the data to be accessed

• Indirect addressing: Instruction contains the l i f h dd f h d blocation of the address of the data to beaccessed

8-47

Figure 8.28 Our first attempt at expanding the machine language in Appendix C to takethe machine language in Appendix C to take advantage of pointers

8-48

Page 13: LectureSlides1008 Data Abstraction.ppt [相容模式]homepage.ntu.edu.tw/~dwhuang/courses/cs/cs_08.pdf · •List – Stack – Queue ... using a linked storage system 8-30 8-31 Figure

Figure 8.29 Loading a register from a memory cell that is located by meansmemory cell that is located by meansof a pointer stored in a register

8-49