data structures what does that mean? in general there are two aspects: how data will be organized in...

30
Data structures Data structures does that mean? neral there are two aspects: data will be organized in computer memory t will be the operations that will be performed wit

Upload: juniper-blake

Post on 18-Jan-2018

220 views

Category:

Documents


0 download

DESCRIPTION

Data structures Some types of “linked objects” Linked lists: Double-linked lists:

TRANSCRIPT

Page 1: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structures

What does that mean?

In general there are two aspects:

• how data will be organized in computer memory

• what will be the operations that will be performed with them

Page 2: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresData organizationThe basic possibilities are to store data either in arrays:

or to link them with pointers:

Page 3: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresSome types of “linked objects”Linked lists:

Double-linked lists:

Page 4: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresSome types of “linked objects”Trees:

Page 5: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresImplementation of linked lists

Key Pointer 1

Page 6: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresImplementation of binary trees

Key Pointer 1

Pointer 2

Page 7: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresImplementation of general trees

Page 8: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresOperations with data structures

Dynamic Dictionaries

LookUp(Key)Insert(Key)Delete(Key)Make()

Priority Queues

Min()ExtractMin()DecreaseKey(Key)Insert(Key)Delete(Key)Make()

Other popular operations with data structures

- unify elements of 2 data structures into one (Union, Meld, )

Page 9: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresStacks

Operations

MakeStack()Push(Key,S)Pop(S)IsEmpty(S)

[Picture from J.Morris]

Page 10: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresStacks

Operations

MakeStack()Push(Key,S)Pop(S)IsEmpty(S)

LIFOLast - in - first - out

Page 11: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structures

struct Cell{int Key, pointer Next}struct Stack{pointer Head}

procedure MakeStack(): S new Stack S.Head 0return S

procedure Push(int Key, Stack S):C new Cell C.Next S.HeadC.Key KeyS.Head C

Stacks - MakeStack, Push

Page 12: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structures

procedure Pop(Stack S):C S.Head Key C.KeyS.Head C.Nextdelete Creturn Key

procedure IsEmpty(Stack S):if S.Head 0 then return 0else return 1

Stacks - Pop, IsEmpty

Page 13: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresQueues

Operations

MakeQueue()Enqueue(Key,Q)Dequeue(Q)IsEmpty(Q)

FIFOFirst - in - first - out

Page 14: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structures

struct Cell{int Key, pointer Next}struct Queue{pointer Head, pointer Tail}

procedure MakeQueue(): Q new Queue Q.Head 0Q.Tail 0return Q

Queues - MakeQueue

Page 15: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structures

procedure Enqueue(int Key, Queue Q):C new Cell C.Next 0C.Key Key if Q.Head = 0 then

Q.Head Celse Tail Q.Tail

Tail.Next CQ.Tail C

Queues - Enqueue

Page 16: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structures

procedure Dequeue(Queue Q): C Q.Head Key C.KeyQ.Head C.Nextif Q.Head = 0 then Q.Tail 0delete Creturn Key

procedure IsEmpty(Queue Q):if Q.Head 0 then return 0else return 1

Queues - Dequeue, IsEmpty

Page 17: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresHeaps• They are binary trees with all levels completed, except the lowest one which may have uncompleted section on the right side

• They satisfy so called Heap Property - for each subtree of heap the key for the root of subtree must not exceed the keys of its (left and right) children

Page 18: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresHeaps - Examples This may be Heap

Page 19: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresHeaps - Examples This may be Heap

Page 20: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresHeaps - ExamplesThis can not be Heap

Page 21: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresHeaps - ExamplesThis can not be Heap

Page 22: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresHeaps - Examples

2

45

12

143

1

13

This is Heap

Page 23: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresHeaps - Examples

2

45

12

143

1

5

This is not Heap

Page 24: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresHeaps - Operations

Min()ExtractMin()DecreaseKey(Key)Insert(Key)Delete(Key)MakeHeap()

Heapify()InitialiseHeap()

Page 25: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresHeaps - Relation between size and heightTheoremFor heap with n elements the height h of the correspondingbinary tree is log n, i.e. h = (log n)

Page 26: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresHeaps - Implementation with an array

2

45

12

3

1

13

13 45 3 12 2 1

LC(j) = 2j – n

RC(j) = 2j – n – 1

P(j) = (j + n)/2

Page 27: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresHeaps - Implementation with an array

[Adapted from T.Cormen, C.Leiserson, R. Rivest]

Page 28: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresHeaps - Insert

3

45

12

7

2

13 1

T(n) = (h) = (log n)

Page 29: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresHeaps - Delete

3

45

12

7

2

13 14

T(n) = (h) = (log n)

Page 30: Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will

Data structuresData structuresHeaps - ExtractMin

3

45

12

7 13 14

T(n) = (h) = (log n)

1