chapter 8: data abstractions senem kumova metin. 8-2 chapter 8: data abstractions 8.1 basic data...

38
Chapter 8: Chapter 8: Data Data Abstractions Abstractions Senem Kumova Metin

Upload: irene-sutton

Post on 12-Jan-2016

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

Chapter 8:Chapter 8:Data AbstractionsData Abstractions

Senem Kumova Metin

Page 2: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-2

Chapter 8: Data Abstractions• 8.1 Basic Data Structures– Arrays– Lists, Stacks, Queues– Trees

• 8.2 Related Concepts– Static versus dynamic structures– Pointers

• 8.3 Implementing Data Structures

Page 3: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-3

Data Structures• A data structure is a particular way of storing and

organizing data in a computer so that it can be used efficiently

• Basic Data Structures– Homogeneous array– Heterogeneous array– List• Linked list• Stack• Queue

– Tree

Page 4: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-4

Basic Data Structures : Arrays• Homogeneous array: The block of data items whose entries

are of the same type

– int array1[6]; // includes 6 integers– float * array2 =new float [6]; // includes 6 floating-point numbers

• Heterogeneous array: The block of data items that might be of different types. (The items within block are usually called components)

– class Employee { public : int ID; string name;};Employee array[8];

Page 5: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

Basic Data Structures : Lists• List : A collection whose entries are arranged sequentially.

– The beginning of the list is head.– The other end of the list is tail.

8-5

data next data next data next NULL

head

class Linked_list { int data; Linked_list*next; };

Linked_list * head;

tail

Page 6: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

Basic Data Structures : Stacks• Stack : A list in which entries are inserted or removed only from head.

– The head of the stack is called top of the stack– Bottom or base: The tail of list (stack)– Inserting a new entry is called pushing.– Deleting an entry is called popping

– Last in first out (LIFO) : The last entry placed on a stack will always be the first entry to be removed

8-6

top

Page 7: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

Basic Data Structures : Queue• Queue : A list in which entries are inserted only at the tail and removed only from head.

– First in first out (FIFO) : The first entry placed on a queue will always be the first entry to be removed

8-7

Page 8: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-8

Figure 8.1 Lists, stacks, and queues

Page 9: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-9

Figure 8.2 An example of an organization chart

Page 10: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-10

Basic Data Structures : Tree• Tree: A collection of data whose entries have a

hierarchical organization

– Node: An entry in a tree– Root node: The node at the top– Terminal or leaf node: A node at the bottom

Root

Leaf nodes

Page 11: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-11

Basic Data Structures : Tree (cont.)– Parent: The node immediately above a

specified node ( 3 is the parent of 1 and 6 )

– Child: A node immediately below a specified node ( 14 is a child of 10)

– Ancestor: Parent, parent of parent, etc. (10 is ancestor of 14 and 13)

– Descendent: Child, child of child, etc.(13 is a decendent of 14,10 and 7)

– Siblings: Nodes sharing a common parent (4 and 7 are siblings)

Page 12: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-12

Basic Data Structures : Tree (cont.)• Binary tree: A tree in which every node has at most two

children

• Depth (The depth of tree): The number of nodes in longest path from root to leaf

• Subtree:The node (except root node) with the nodes below it

A subtree

Page 13: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

Basic Data Structures : Tree (cont.)• The size of a binary tree is the

number of nodes in it– This tree has size 9

• The depth of a node is its distance from the root– a is at depth zero– e is at depth 2

• The depth of a binary tree is the depth of its deepest node– This tree has depth 3

a

b c

d e f g

h ı

Page 14: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-14

Figure 8.3 Tree terminology

Page 15: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-15

Related Concepts

• Static Data Structures: Size and shape of data structure does not change (compile time)

• Dynamic Data Structures: Size and shape of data structure can change (run time)

• Pointers: Used to locate data

Page 16: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-16

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

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

a contiguous block– Components can be stored in separate locations

identified by pointers

Page 17: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-17

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

(Homogenous Array)

Page 18: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-18

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

Page 19: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

19

Storage mapping function : ROW MAJOR ORDER

• the mapping b/w pointer values and array indices • EXAMPLE:

int d [3];d[i]*(&d[0]+i) *(d+i)

• EXAMPLE: int a [3] [4];a[i][j] *(&a[0][0]+4*i+j)

*(a+4*i+j) // address polynomial

• If indexing starts from 1 then x+ c*(i-1)+(j-1)

a[0][0]a[0][1]a[0][2]a[0][3]a[1][0]a[1][1]a[1][2]a[1][3]a[2][0]a[2][1]a[2][2]a[2][3]

Page 20: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-20

Figure 8.7 Storing the heterogeneous array Employee

Page 21: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-21

Storing Lists

• Contiguous list: List stored in a homogeneous array (Example: int a [10])

• Linked list: List in which each entries are linked by pointers– Head pointer: Pointer to first entry in list– NIL pointer: A “non-pointer” value used to

indicate end of list

Page 22: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-22

Figure 8.8 Names stored in memory as a contiguous list

Page 23: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-23

Figure 8.9 The structure of a linked list

Page 24: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-24

Figure 8.10 Deleting an entry from a linked list

Page 25: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-25

Figure 8.11 Inserting an entry into a linked list

Page 26: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-26

Figure 8.19 A procedure for printing a linked list

Page 27: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-27

Exercise : Question 8 (pg.428) Linked list

• The table represents a position in memory. Each entry in the list consists of two cells: The first contains a letter of alphabet; the second contains a pointer to the next list entry.

• Alter the pointers so that the letter N is no longer in the list then replace the letter N with the letter G and alter the pointers so that the new letter appears int he list in its proper place in alphabetical order

Page 28: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-28

Storing Stacks and Queues

• Stacks usually stored as contiguous lists (arrays) but also may be implemented using linked lists

• Queues usually stored as Circular Queues– Stored in a contiguous block in which the first

entry is considered to follow the last entry– Prevents a queue from crawling out of its allotted

storage space

Page 29: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-29

Figure 8.12 A stack in memory

Page 30: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

Exercise :STACKS

x= pop(&n);

push(11,&n);

x= pop(&n);x= pop(&n);x= pop(&n);

push(3,&n);push(2,&n);

x= pop(&n); }

main(){

stack n; int x;reset(&n);

push(4,&n);push(5,&n);push(3,&n);push(2,&n);push(1,&n);

Page 31: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-31

Figure 8.13 A queue implementation with head and tail pointers

// insert from tail//remove from head

enqueue(A)

enqueue(B)

enqueue(C)

X= dequeue()

enqueue(D)

Y= dequeue()

enqueue(E)

Page 32: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-32

Figure 8.14 A circular queue containing the letters P through V

Page 33: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-33

Storing Binary Trees• Linked structure– Each node = data cells + two child pointers– Accessed via a pointer to root node

struct node {int data; struct node * left;struct node * right; };

• Contiguous array structure (without pointers)– 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]

Page 34: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-34

Figure 8.15 The structure of a node in a binary tree

Page 35: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-35

Figure 8.16 The conceptual and actual organization of a binary tree using a linked

storage system

Page 36: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-36

Figure 8.17 A tree stored without pointers

Page 37: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-37

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

without pointers

Page 38: Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2

8-38

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.• A queue typically needs at least enqueue and dequeue procedures.

– The data structure along with these procedures constitutes a complete abstract tool.