data structures what does that mean? in general there are two aspects: how data will be organized in...
DESCRIPTION
Data structures Some types of “linked objects” Linked lists: Double-linked lists:TRANSCRIPT
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
Data structuresData structuresData organizationThe basic possibilities are to store data either in arrays:
or to link them with pointers:
Data structuresData structuresSome types of “linked objects”Linked lists:
Double-linked lists:
Data structuresData structuresSome types of “linked objects”Trees:
Data structuresData structuresImplementation of linked lists
Key Pointer 1
Data structuresData structuresImplementation of binary trees
Key Pointer 1
Pointer 2
Data structuresData structuresImplementation of general trees
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, )
Data structuresData structuresStacks
Operations
MakeStack()Push(Key,S)Pop(S)IsEmpty(S)
[Picture from J.Morris]
Data structuresData structuresStacks
Operations
MakeStack()Push(Key,S)Pop(S)IsEmpty(S)
LIFOLast - in - first - out
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
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
Data structuresData structuresQueues
Operations
MakeQueue()Enqueue(Key,Q)Dequeue(Q)IsEmpty(Q)
FIFOFirst - in - first - out
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
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
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
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
Data structuresData structuresHeaps - Examples This may be Heap
Data structuresData structuresHeaps - Examples This may be Heap
Data structuresData structuresHeaps - ExamplesThis can not be Heap
Data structuresData structuresHeaps - ExamplesThis can not be Heap
Data structuresData structuresHeaps - Examples
2
45
12
143
1
13
This is Heap
Data structuresData structuresHeaps - Examples
2
45
12
143
1
5
This is not Heap
Data structuresData structuresHeaps - Operations
Min()ExtractMin()DecreaseKey(Key)Insert(Key)Delete(Key)MakeHeap()
Heapify()InitialiseHeap()
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)
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
Data structuresData structuresHeaps - Implementation with an array
[Adapted from T.Cormen, C.Leiserson, R. Rivest]
Data structuresData structuresHeaps - Insert
3
45
12
7
2
13 1
T(n) = (h) = (log n)
Data structuresData structuresHeaps - Delete
3
45
12
7
2
13 14
T(n) = (h) = (log n)
Data structuresData structuresHeaps - ExtractMin
3
45
12
7 13 14
T(n) = (h) = (log n)
1