1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory...

10
1 Data Organization Example 1: Heap storage management Keep track of free chunks of memory Example 2: A simple text editor Maintain a sequence of lines Example 3: Stable matching problem Maintain the set of currently free men Keep track of each person's preferences • Quickly find the highest-ranked woman to whom a man has not proposed yet Quickly find whether a woman is engaged and to whom

Transcript of 1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory...

Page 1: 1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.

1

Data Organization

Example 1: Heap storage management– Keep track of free chunks of memory

Example 2: A simple text editor– Maintain a sequence of lines

Example 3: Stable matching problem– Maintain the set of currently free men– Keep track of each person's preferences

• Quickly find the highest-ranked woman to whom a man has not proposed yet

– Quickly find whether a woman is engaged and to whom

Page 2: 1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.

2

Data Organization

All these examples have a common organizational model:– A sequence of similar items

• (memory blocks, lines, men/women)

– Certain desired operations • find, insert, delete

Page 3: 1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.

3

The list ADT

Data:– A collection of homogeneous elements

arranged in a sequence Operations

– Insert– Delete– Find– Update– Retrieve– Length

Page 4: 1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.

4

The list ADT

Most operations refer to a certain position in the list. How will this be designed?– Maintain a "current" position?– Specify an index?– Specify a pointer to an element?– Specify a generalized pointer to an element?

Page 5: 1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.

5

The list data structure

Implementation 1: Contiguous memory– Use a dynamic array– How is each operation implemented?– How efficient is each operation?

• Random access capability is good for retrieval if we use an index for element access

– Important: The list ADT does not provide random access.

• We need to shift elements every time we insert or delete

• We need to reallocate whenever the array fills up.

Page 6: 1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.

6

The list data structure

Implementation 2: Linked memory– Use a node structure to store the data and a pointer to

the next node, to create a chain of nodes.– Uses more space than the array (due to the pointers)

but insert/delete do not require shifting.– However, deleting requires us to traverse the whole

list in order to access the predecessor of the node to be deleted.

• Easy solution: keep in mind the abstract image of a linked list!– Move the next node's contents into the one to be deleted, and

then physically remove the next node.

– We can use a similar trick for the insert operation

– Does this work in all cases?

Page 7: 1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.

7

Other list flavors

Doubly-linked list– Each node has a pointer to its successor and

its predecessor. • Faster insert/delete, but more space.

Circular list– The last node points back to the head.

Sorted list– Items stored in sorted order.– Which implementation provides faster

operations? Array or linked memory?

Page 8: 1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.

8

Other list flavors

XOR list– A space saving list– Instead of both a previous and next pointer,

store the XOR of the predecessor and successor.

• Node B stores &A XOR &C• If you are at B and know the address of A, you can

compute the address of C.• The list can be traversed in any direction, provided

you know where you came from.

– Interesting, but not very useful...

Page 9: 1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.

9

Other list flavors

Unrolled linked list– A space saving list– Store k data items in each node

• It's a list of arrays• Reduces cache misses• Each node should be at least half-full• If a node is too empty after a delete, merge it with a

neighbor.• If a node overflows after an insert, split it.

Page 10: 1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.

10

Other list flavors

Satellite list– An easily reversible list– Developed for use in TSP algorithms– Imagine each node as having two "satellites",

north and south. A chain of pointers links all the northern satellites and another chain links all the southern ones.

– Reversing part of the list requires changing only 4 pointers.

• Compare this to reversing a doubly-linked list.