Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2...

56
Chapter 8 Data Abstractions

Transcript of Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2...

Page 1: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

Chapter 8

Data Abstractions

2

Chapter 8 Data Abstractions 81 Data Structure Fundamentals 82 Implementing Data Structures 83 A Short Case Study 84 Customized Data Types 85 Classes and Objects 86 Pointers in Machine Language

3

Basic Data Structures Homogeneous array Heterogeneous array List

Stack Queue

Tree

4

Figure 81 Lists stacks and queues

5

Terminology for lists List = collection of data whose

entries are arranged sequentially Head = beginning of the list Tail = end of the list

6

Terminology for stacks Stack = a list in which entries are

removed and inserted only at the head

LIFO = last-in-first-out Top = head of list Bottom or base = tail of list Pop = remove entry from the top Push = insert entry at the top

7

Terminology for queues Queue = a list in which entries are

removed at the head and are inserted at the tail

FIFO = first-in-first-out

8

Terminology for a tree Tree = a collection of data whose

entries have a hierarchical organization

Node = an entry in a tree Binary tree = tree in which every

node has at most two children Depth = number of nodes in

longest path from root to leaf

9

Terminology for a tree (continued)

Root node = the node at the top Terminal or leaf node = a node at the

bottom Parent of a node = the node immediately

above the specified node Child of a node = a node immediately

below the specified node Ancestor = parent parent of parent etc Descendent = child child of child etc Siblings = nodes sharing a common

parent

10

Figure 81 An example of an organization chart

11

Figure 82 Tree terminology

12

Basic data structures Homogeneous array

is an array of entries of the same type List The unique feature of arrays is that

their size and shape are constant1048708 For the group with dynamic population

(members joinleave) lists structure employs the concept of a pointer to accommodate such variations on membership

13

Nature of data structures Static size of data structure does not

change Dynamic size of data structure can change PointerMain memory location of an data item A is

identified by its numeric address which value can be saved in another memory location P1048708

Such P is called a pointer to the data item A1048708 The program counter is indeed the instruction

pointer1048708 URLs(Uniform resource Locator) used to link

hypertext contents are also like the pointers

14

Figure 83 Novels arranged by title but linked according to authorship

15

1Give examples of each of the following structures in daily lifeListStackQueueTree2Suppose a tree has four nodes ABCand D If A and C are siblings and Drsquos parent is A which nodes are leafNodes Which is root node

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 2: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

2

Chapter 8 Data Abstractions 81 Data Structure Fundamentals 82 Implementing Data Structures 83 A Short Case Study 84 Customized Data Types 85 Classes and Objects 86 Pointers in Machine Language

3

Basic Data Structures Homogeneous array Heterogeneous array List

Stack Queue

Tree

4

Figure 81 Lists stacks and queues

5

Terminology for lists List = collection of data whose

entries are arranged sequentially Head = beginning of the list Tail = end of the list

6

Terminology for stacks Stack = a list in which entries are

removed and inserted only at the head

LIFO = last-in-first-out Top = head of list Bottom or base = tail of list Pop = remove entry from the top Push = insert entry at the top

7

Terminology for queues Queue = a list in which entries are

removed at the head and are inserted at the tail

FIFO = first-in-first-out

8

Terminology for a tree Tree = a collection of data whose

entries have a hierarchical organization

Node = an entry in a tree Binary tree = tree in which every

node has at most two children Depth = number of nodes in

longest path from root to leaf

9

Terminology for a tree (continued)

Root node = the node at the top Terminal or leaf node = a node at the

bottom Parent of a node = the node immediately

above the specified node Child of a node = a node immediately

below the specified node Ancestor = parent parent of parent etc Descendent = child child of child etc Siblings = nodes sharing a common

parent

10

Figure 81 An example of an organization chart

11

Figure 82 Tree terminology

12

Basic data structures Homogeneous array

is an array of entries of the same type List The unique feature of arrays is that

their size and shape are constant1048708 For the group with dynamic population

(members joinleave) lists structure employs the concept of a pointer to accommodate such variations on membership

13

Nature of data structures Static size of data structure does not

change Dynamic size of data structure can change PointerMain memory location of an data item A is

identified by its numeric address which value can be saved in another memory location P1048708

Such P is called a pointer to the data item A1048708 The program counter is indeed the instruction

pointer1048708 URLs(Uniform resource Locator) used to link

hypertext contents are also like the pointers

14

Figure 83 Novels arranged by title but linked according to authorship

15

1Give examples of each of the following structures in daily lifeListStackQueueTree2Suppose a tree has four nodes ABCand D If A and C are siblings and Drsquos parent is A which nodes are leafNodes Which is root node

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 3: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

3

Basic Data Structures Homogeneous array Heterogeneous array List

Stack Queue

Tree

4

Figure 81 Lists stacks and queues

5

Terminology for lists List = collection of data whose

entries are arranged sequentially Head = beginning of the list Tail = end of the list

6

Terminology for stacks Stack = a list in which entries are

removed and inserted only at the head

LIFO = last-in-first-out Top = head of list Bottom or base = tail of list Pop = remove entry from the top Push = insert entry at the top

7

Terminology for queues Queue = a list in which entries are

removed at the head and are inserted at the tail

FIFO = first-in-first-out

8

Terminology for a tree Tree = a collection of data whose

entries have a hierarchical organization

Node = an entry in a tree Binary tree = tree in which every

node has at most two children Depth = number of nodes in

longest path from root to leaf

9

Terminology for a tree (continued)

Root node = the node at the top Terminal or leaf node = a node at the

bottom Parent of a node = the node immediately

above the specified node Child of a node = a node immediately

below the specified node Ancestor = parent parent of parent etc Descendent = child child of child etc Siblings = nodes sharing a common

parent

10

Figure 81 An example of an organization chart

11

Figure 82 Tree terminology

12

Basic data structures Homogeneous array

is an array of entries of the same type List The unique feature of arrays is that

their size and shape are constant1048708 For the group with dynamic population

(members joinleave) lists structure employs the concept of a pointer to accommodate such variations on membership

13

Nature of data structures Static size of data structure does not

change Dynamic size of data structure can change PointerMain memory location of an data item A is

identified by its numeric address which value can be saved in another memory location P1048708

Such P is called a pointer to the data item A1048708 The program counter is indeed the instruction

pointer1048708 URLs(Uniform resource Locator) used to link

hypertext contents are also like the pointers

14

Figure 83 Novels arranged by title but linked according to authorship

15

1Give examples of each of the following structures in daily lifeListStackQueueTree2Suppose a tree has four nodes ABCand D If A and C are siblings and Drsquos parent is A which nodes are leafNodes Which is root node

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 4: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

4

Figure 81 Lists stacks and queues

5

Terminology for lists List = collection of data whose

entries are arranged sequentially Head = beginning of the list Tail = end of the list

6

Terminology for stacks Stack = a list in which entries are

removed and inserted only at the head

LIFO = last-in-first-out Top = head of list Bottom or base = tail of list Pop = remove entry from the top Push = insert entry at the top

7

Terminology for queues Queue = a list in which entries are

removed at the head and are inserted at the tail

FIFO = first-in-first-out

8

Terminology for a tree Tree = a collection of data whose

entries have a hierarchical organization

Node = an entry in a tree Binary tree = tree in which every

node has at most two children Depth = number of nodes in

longest path from root to leaf

9

Terminology for a tree (continued)

Root node = the node at the top Terminal or leaf node = a node at the

bottom Parent of a node = the node immediately

above the specified node Child of a node = a node immediately

below the specified node Ancestor = parent parent of parent etc Descendent = child child of child etc Siblings = nodes sharing a common

parent

10

Figure 81 An example of an organization chart

11

Figure 82 Tree terminology

12

Basic data structures Homogeneous array

is an array of entries of the same type List The unique feature of arrays is that

their size and shape are constant1048708 For the group with dynamic population

(members joinleave) lists structure employs the concept of a pointer to accommodate such variations on membership

13

Nature of data structures Static size of data structure does not

change Dynamic size of data structure can change PointerMain memory location of an data item A is

identified by its numeric address which value can be saved in another memory location P1048708

Such P is called a pointer to the data item A1048708 The program counter is indeed the instruction

pointer1048708 URLs(Uniform resource Locator) used to link

hypertext contents are also like the pointers

14

Figure 83 Novels arranged by title but linked according to authorship

15

1Give examples of each of the following structures in daily lifeListStackQueueTree2Suppose a tree has four nodes ABCand D If A and C are siblings and Drsquos parent is A which nodes are leafNodes Which is root node

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 5: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

5

Terminology for lists List = collection of data whose

entries are arranged sequentially Head = beginning of the list Tail = end of the list

6

Terminology for stacks Stack = a list in which entries are

removed and inserted only at the head

LIFO = last-in-first-out Top = head of list Bottom or base = tail of list Pop = remove entry from the top Push = insert entry at the top

7

Terminology for queues Queue = a list in which entries are

removed at the head and are inserted at the tail

FIFO = first-in-first-out

8

Terminology for a tree Tree = a collection of data whose

entries have a hierarchical organization

Node = an entry in a tree Binary tree = tree in which every

node has at most two children Depth = number of nodes in

longest path from root to leaf

9

Terminology for a tree (continued)

Root node = the node at the top Terminal or leaf node = a node at the

bottom Parent of a node = the node immediately

above the specified node Child of a node = a node immediately

below the specified node Ancestor = parent parent of parent etc Descendent = child child of child etc Siblings = nodes sharing a common

parent

10

Figure 81 An example of an organization chart

11

Figure 82 Tree terminology

12

Basic data structures Homogeneous array

is an array of entries of the same type List The unique feature of arrays is that

their size and shape are constant1048708 For the group with dynamic population

(members joinleave) lists structure employs the concept of a pointer to accommodate such variations on membership

13

Nature of data structures Static size of data structure does not

change Dynamic size of data structure can change PointerMain memory location of an data item A is

identified by its numeric address which value can be saved in another memory location P1048708

Such P is called a pointer to the data item A1048708 The program counter is indeed the instruction

pointer1048708 URLs(Uniform resource Locator) used to link

hypertext contents are also like the pointers

14

Figure 83 Novels arranged by title but linked according to authorship

15

1Give examples of each of the following structures in daily lifeListStackQueueTree2Suppose a tree has four nodes ABCand D If A and C are siblings and Drsquos parent is A which nodes are leafNodes Which is root node

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 6: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

6

Terminology for stacks Stack = a list in which entries are

removed and inserted only at the head

LIFO = last-in-first-out Top = head of list Bottom or base = tail of list Pop = remove entry from the top Push = insert entry at the top

7

Terminology for queues Queue = a list in which entries are

removed at the head and are inserted at the tail

FIFO = first-in-first-out

8

Terminology for a tree Tree = a collection of data whose

entries have a hierarchical organization

Node = an entry in a tree Binary tree = tree in which every

node has at most two children Depth = number of nodes in

longest path from root to leaf

9

Terminology for a tree (continued)

Root node = the node at the top Terminal or leaf node = a node at the

bottom Parent of a node = the node immediately

above the specified node Child of a node = a node immediately

below the specified node Ancestor = parent parent of parent etc Descendent = child child of child etc Siblings = nodes sharing a common

parent

10

Figure 81 An example of an organization chart

11

Figure 82 Tree terminology

12

Basic data structures Homogeneous array

is an array of entries of the same type List The unique feature of arrays is that

their size and shape are constant1048708 For the group with dynamic population

(members joinleave) lists structure employs the concept of a pointer to accommodate such variations on membership

13

Nature of data structures Static size of data structure does not

change Dynamic size of data structure can change PointerMain memory location of an data item A is

identified by its numeric address which value can be saved in another memory location P1048708

Such P is called a pointer to the data item A1048708 The program counter is indeed the instruction

pointer1048708 URLs(Uniform resource Locator) used to link

hypertext contents are also like the pointers

14

Figure 83 Novels arranged by title but linked according to authorship

15

1Give examples of each of the following structures in daily lifeListStackQueueTree2Suppose a tree has four nodes ABCand D If A and C are siblings and Drsquos parent is A which nodes are leafNodes Which is root node

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 7: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

7

Terminology for queues Queue = a list in which entries are

removed at the head and are inserted at the tail

FIFO = first-in-first-out

8

Terminology for a tree Tree = a collection of data whose

entries have a hierarchical organization

Node = an entry in a tree Binary tree = tree in which every

node has at most two children Depth = number of nodes in

longest path from root to leaf

9

Terminology for a tree (continued)

Root node = the node at the top Terminal or leaf node = a node at the

bottom Parent of a node = the node immediately

above the specified node Child of a node = a node immediately

below the specified node Ancestor = parent parent of parent etc Descendent = child child of child etc Siblings = nodes sharing a common

parent

10

Figure 81 An example of an organization chart

11

Figure 82 Tree terminology

12

Basic data structures Homogeneous array

is an array of entries of the same type List The unique feature of arrays is that

their size and shape are constant1048708 For the group with dynamic population

(members joinleave) lists structure employs the concept of a pointer to accommodate such variations on membership

13

Nature of data structures Static size of data structure does not

change Dynamic size of data structure can change PointerMain memory location of an data item A is

identified by its numeric address which value can be saved in another memory location P1048708

Such P is called a pointer to the data item A1048708 The program counter is indeed the instruction

pointer1048708 URLs(Uniform resource Locator) used to link

hypertext contents are also like the pointers

14

Figure 83 Novels arranged by title but linked according to authorship

15

1Give examples of each of the following structures in daily lifeListStackQueueTree2Suppose a tree has four nodes ABCand D If A and C are siblings and Drsquos parent is A which nodes are leafNodes Which is root node

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 8: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

8

Terminology for a tree Tree = a collection of data whose

entries have a hierarchical organization

Node = an entry in a tree Binary tree = tree in which every

node has at most two children Depth = number of nodes in

longest path from root to leaf

9

Terminology for a tree (continued)

Root node = the node at the top Terminal or leaf node = a node at the

bottom Parent of a node = the node immediately

above the specified node Child of a node = a node immediately

below the specified node Ancestor = parent parent of parent etc Descendent = child child of child etc Siblings = nodes sharing a common

parent

10

Figure 81 An example of an organization chart

11

Figure 82 Tree terminology

12

Basic data structures Homogeneous array

is an array of entries of the same type List The unique feature of arrays is that

their size and shape are constant1048708 For the group with dynamic population

(members joinleave) lists structure employs the concept of a pointer to accommodate such variations on membership

13

Nature of data structures Static size of data structure does not

change Dynamic size of data structure can change PointerMain memory location of an data item A is

identified by its numeric address which value can be saved in another memory location P1048708

Such P is called a pointer to the data item A1048708 The program counter is indeed the instruction

pointer1048708 URLs(Uniform resource Locator) used to link

hypertext contents are also like the pointers

14

Figure 83 Novels arranged by title but linked according to authorship

15

1Give examples of each of the following structures in daily lifeListStackQueueTree2Suppose a tree has four nodes ABCand D If A and C are siblings and Drsquos parent is A which nodes are leafNodes Which is root node

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 9: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

9

Terminology for a tree (continued)

Root node = the node at the top Terminal or leaf node = a node at the

bottom Parent of a node = the node immediately

above the specified node Child of a node = a node immediately

below the specified node Ancestor = parent parent of parent etc Descendent = child child of child etc Siblings = nodes sharing a common

parent

10

Figure 81 An example of an organization chart

11

Figure 82 Tree terminology

12

Basic data structures Homogeneous array

is an array of entries of the same type List The unique feature of arrays is that

their size and shape are constant1048708 For the group with dynamic population

(members joinleave) lists structure employs the concept of a pointer to accommodate such variations on membership

13

Nature of data structures Static size of data structure does not

change Dynamic size of data structure can change PointerMain memory location of an data item A is

identified by its numeric address which value can be saved in another memory location P1048708

Such P is called a pointer to the data item A1048708 The program counter is indeed the instruction

pointer1048708 URLs(Uniform resource Locator) used to link

hypertext contents are also like the pointers

14

Figure 83 Novels arranged by title but linked according to authorship

15

1Give examples of each of the following structures in daily lifeListStackQueueTree2Suppose a tree has four nodes ABCand D If A and C are siblings and Drsquos parent is A which nodes are leafNodes Which is root node

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 10: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

10

Figure 81 An example of an organization chart

11

Figure 82 Tree terminology

12

Basic data structures Homogeneous array

is an array of entries of the same type List The unique feature of arrays is that

their size and shape are constant1048708 For the group with dynamic population

(members joinleave) lists structure employs the concept of a pointer to accommodate such variations on membership

13

Nature of data structures Static size of data structure does not

change Dynamic size of data structure can change PointerMain memory location of an data item A is

identified by its numeric address which value can be saved in another memory location P1048708

Such P is called a pointer to the data item A1048708 The program counter is indeed the instruction

pointer1048708 URLs(Uniform resource Locator) used to link

hypertext contents are also like the pointers

14

Figure 83 Novels arranged by title but linked according to authorship

15

1Give examples of each of the following structures in daily lifeListStackQueueTree2Suppose a tree has four nodes ABCand D If A and C are siblings and Drsquos parent is A which nodes are leafNodes Which is root node

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 11: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

11

Figure 82 Tree terminology

12

Basic data structures Homogeneous array

is an array of entries of the same type List The unique feature of arrays is that

their size and shape are constant1048708 For the group with dynamic population

(members joinleave) lists structure employs the concept of a pointer to accommodate such variations on membership

13

Nature of data structures Static size of data structure does not

change Dynamic size of data structure can change PointerMain memory location of an data item A is

identified by its numeric address which value can be saved in another memory location P1048708

Such P is called a pointer to the data item A1048708 The program counter is indeed the instruction

pointer1048708 URLs(Uniform resource Locator) used to link

hypertext contents are also like the pointers

14

Figure 83 Novels arranged by title but linked according to authorship

15

1Give examples of each of the following structures in daily lifeListStackQueueTree2Suppose a tree has four nodes ABCand D If A and C are siblings and Drsquos parent is A which nodes are leafNodes Which is root node

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 12: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

12

Basic data structures Homogeneous array

is an array of entries of the same type List The unique feature of arrays is that

their size and shape are constant1048708 For the group with dynamic population

(members joinleave) lists structure employs the concept of a pointer to accommodate such variations on membership

13

Nature of data structures Static size of data structure does not

change Dynamic size of data structure can change PointerMain memory location of an data item A is

identified by its numeric address which value can be saved in another memory location P1048708

Such P is called a pointer to the data item A1048708 The program counter is indeed the instruction

pointer1048708 URLs(Uniform resource Locator) used to link

hypertext contents are also like the pointers

14

Figure 83 Novels arranged by title but linked according to authorship

15

1Give examples of each of the following structures in daily lifeListStackQueueTree2Suppose a tree has four nodes ABCand D If A and C are siblings and Drsquos parent is A which nodes are leafNodes Which is root node

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 13: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

13

Nature of data structures Static size of data structure does not

change Dynamic size of data structure can change PointerMain memory location of an data item A is

identified by its numeric address which value can be saved in another memory location P1048708

Such P is called a pointer to the data item A1048708 The program counter is indeed the instruction

pointer1048708 URLs(Uniform resource Locator) used to link

hypertext contents are also like the pointers

14

Figure 83 Novels arranged by title but linked according to authorship

15

1Give examples of each of the following structures in daily lifeListStackQueueTree2Suppose a tree has four nodes ABCand D If A and C are siblings and Drsquos parent is A which nodes are leafNodes Which is root node

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 14: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

14

Figure 83 Novels arranged by title but linked according to authorship

15

1Give examples of each of the following structures in daily lifeListStackQueueTree2Suppose a tree has four nodes ABCand D If A and C are siblings and Drsquos parent is A which nodes are leafNodes Which is root node

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 15: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

15

1Give examples of each of the following structures in daily lifeListStackQueueTree2Suppose a tree has four nodes ABCand D If A and C are siblings and Drsquos parent is A which nodes are leafNodes Which is root node

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 16: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

16

Storing homogeneous arrays

Requires enough contiguous memory to hold all data

Int readings [24] reading[4] lt- 67 Two dimensional arrays

Row-major order each row is stored together

Address polynomial A[rc] = (r-1)( columns) + (c-1)

Column-major order each column is stored together

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 17: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

17

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

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 18: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

18

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

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 19: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

19

Figure 87 Storing the heterogeneous array Employee

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 20: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

20

Storing lists

Contiguous list = list stored in a homogeneous array

Linked list = list in which each node points to the next one Head pointer = pointer to first entry

in list NIL pointer = non-pointer value

used to indicate end of list

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 21: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

21

Figure 86 Names stored in memory as a contiguous list

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 22: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

22

Figure 87 The structure of a linked list

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 23: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

23

Figure 88 Deleting an entry from a linked list

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 24: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

24

Figure 89 Inserting an entry into a linked list

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 25: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

25

Storing stacks and queues Can use same mechanisms as for

lists Circular queue = homogeneous

array where the first array entry is treated as immediately following the last array entry Prevents a queue from crawling out of

its allotted storage space

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 26: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

26

Figure 810 A stack in memory

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 27: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

27

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

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 28: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

28

Problems with Pointers

Pointers in Queue may grow out of the region of the specified memory region

In Java to advance pointerredirect Next to the next list entryIn CAssign Next the value Next +1What is the problem in C

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 29: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

29

Storing a binary tree Linked structure

Each node = data cell + two child pointers Accessed through a pointer to root node

Mapped to a contiguous array 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] hellip

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 30: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

30

Figure 812 The structure of a node in a binary tree

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 31: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

31

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

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 32: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

32

Figure 814 A tree stored without pointers

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 33: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

33

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

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 34: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

34

Manipulating data structures

Ideally a data structure should be manipulated solely by pre-defined procedures Example A stack typically needs at

least push and pop procedures The data structure along with these

procedures constitutes a complete abstract tool

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 35: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

35

Figure 816 A procedure for printing a linked list

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 36: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

36

What condition indicates a linked list is emptyWhat about Stack and Queue

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 37: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

37

A Case Study

Store the letters in a binary tree1 The middle of the entry the root node2 The middle of the remaining first half of the list the rootrsquos left child3 The middle of the remaining second half of the list the rootrsquos right childABCDEFGHIJKLMThen 3 operations to be performedSearch for the presence of an entryPrint the list in alphabetical orderInsert a new entry

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 38: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

38

Figure 817 The letters A through M arranged in an ordered tree

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 39: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

39

Figure 818 The binary search as it would appear if the list were implemented as a linked binary tree

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 40: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

40

Figure 819 The successively smaller trees considered by the procedure in Figure 818 when searching for the letter J

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 41: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

41

Figure 820 Printing a search tree in alphabetical order

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 42: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

42

Figure 821 A procedure for printing the data in a binary tree

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 43: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

43

Figure 822 Inserting the entry M into the list B E G H J K N P stored as a tree

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 44: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

44

Figure 823 A procedure for inserting a new entry in a list stored as a binary tree

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 45: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

45

Non-recursive way for serachprocedure BinarySearch (Tree TargetValue)

CurrentPointer root pointer of TreeThe node pointed to by CurrentPointer will be called the current node

Found falsewhile (Found is false and CurrentPointer is not NIL) do

(Perform the activity associated with the appropriate

case below

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 46: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

46

Non-recursive way for serachcase 1 TargetValue = current node(Found true)case 2 TargetValue lt current node(CurrentPointer current nodes left child pointer)

case 3 TargetValue gt current node(CurrentPointer current nodes right child pointer)

)if (Found = false) then (declare the search a failure)

else (declare the search a success)

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 47: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

47

Customized data types User-defined data type = template

for a heterogeneous structure define type EmployeeType to be char Name[8] Int Age real SkillRating EmployeeType DistManager DistManagername=John DistManagerage=30

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 48: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

48

Customized data types Abstract data type = user-defined

data type with methods for access and manipulation

define type StackType to be int StackEntries[20] int StackPointer = 0 Procedure push(value) StackEnties[StackPointer]=value Stackpointer++ Procedure pophellip StackType Stackone Stackonepush(25)

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 49: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

49

Customized data types What should be considered when building an

abstract data type for a list 1 insert delete find the entry 2 Ways of implementing a listmdash Contiguous or linked list 3 The choice of either way is invisible to users

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 50: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

50

Customized data types Class and Objects Class = abstract data type with

extra features Characteristics can be inherited Contents can be encapsulated Constructor methods to initialize new

objects

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 51: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

51

Figure 824 A stack of integers implemented in Java and C

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 52: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

52

Pointers in machine language Immediate addressing

instruction contains the data to be accessed

Direct addressing instruction contains the address of the data to be accessed

Indirect addressing instruction contains the location of the address of the data to be accessed

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 53: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

53

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

DRXYmdashload register R with contents of the memory cells whose address is found at address XYNeed to change Stack pointer(load to register-1then load)

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 54: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

54

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register S

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 55: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

55

Figure 826 Loading a register from a memory cell that is located by means of a pointer stored in a register

DR0Smdashload register R with the contents of the memory cells Pointed by register SWrite a complete machine language routine to perform a Pop operationAssume stack pointer is in register FAnd Top of the stack is to be pooped into register 5

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer

Page 56: Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.

56

Push operation

ER0Smdashstore the contents of register R in the memory cells Pointed by register S

DRXSload register R with the data pointed by the value in register S plus the value XEX If register F contained 0x04 then DE2F willLoad register E with the content of the memory cell at address The value of register F remains What advantage of this instruction has when used in linked list data structureEach entry has two memory cells(data+pointer)DR0S can be used to retrieve the dataDR1S can be used to retrieve the pointer