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 organization The basic possibilities are to store data either in arrays: or to link them with pointers:
Some types of “linked objects” Linked lists: Double-linked lists:
Some types of “linked objects” Trees:
Implementation of linked lists Key Pointer 1
Implementation of binary trees Key Pointer 1 Pointer 2
Implementation of general trees
Operations 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, )
Stacks Operations MakeStack() Push(Key,S) Pop(S) IsEmpty(S) [Picture from J.Morris]
LIFO Stacks Operations MakeStack() Push(Key,S) Pop(S) IsEmpty(S) Last - in - first - out
Stacks - MakeStack, Push struct Cell{int Key, pointer Next} struct Stack{pointer Head} procedure MakeStack(): S new Stack S.Head 0 return S procedure Push(int Key, Stack S): C new Cell C.Next S.Head C.Key Key S.Head C
Stacks - Pop, IsEmpty procedure Pop(Stack S): C S.Head Key C.Key S.Head C.Next delete C return Key procedure IsEmpty(Stack S): if S.Head 0 then return 0 else return 1
FIFO Queues Operations MakeQueue() Enqueue(Key,Q) First - in - first - out Operations MakeQueue() Enqueue(Key,Q) Dequeue(Q) IsEmpty(Q)
Queues - MakeQueue struct Cell{int Key, pointer Next} struct Queue{pointer Head, pointer Tail} procedure MakeQueue(): Q new Queue Q.Head 0 Q.Tail 0 return Q
Queues - Enqueue procedure Enqueue(int Key, Queue Q): C new Cell C.Next 0 C.Key Key if Q.Head = 0 then Q.Head C else Tail Q.Tail Tail.Next C Q.Tail C
Queues - Dequeue, IsEmpty procedure Dequeue(Queue Q): C Q.Head Key C.Key Q.Head C.Next if Q.Head = 0 then Q.Tail 0 delete C return Key procedure IsEmpty(Queue Q): if Q.Head 0 then return 0 else return 1
Heaps 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
Heaps - Examples This may be Heap
Heaps - Examples This may be Heap
Heaps - Examples This can not be Heap
Heaps - Examples This can not be Heap
Heaps - Examples This is Heap 1 2 12 3 45 13 14
Heaps - Examples This is not Heap 1 2 12 3 45 5 14
Heaps - Operations Min() ExtractMin() DecreaseKey(Key) Insert(Key) Delete(Key) MakeHeap() Heapify() InitialiseHeap()
Heaps - Relation between size and height Theorem For heap with n elements the height h of the corresponding binary tree is log n, i.e. h = (log n)
Heaps - Implementation with an array 1 LC(j) = 2j – n – 1 RC(j) = 2j – n – 2 2 12 P(j) = 1 + (j + n)/2 3 45 13 13 45 3 12 2 1
Heaps - Implementation with an array [Adapted from T.Cormen, C.Leiserson, R. Rivest]
Heaps - Insert 2 3 12 7 45 13 1 T(n) = (h) = (log n)
Heaps - Delete 2 3 12 7 45 13 14 T(n) = (h) = (log n)
Heaps - ExtractMin 1 3 12 7 45 13 14 T(n) = (h) = (log n)