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 be performed with them
Data structures Data organization The basic possibilities are to store data either in arrays: or to link them with pointers:
Data structures Some types of “linked objects” Linked lists: Double-linked lists:
Data structures Some types of “linked objects” Trees:
Data structures Implementation of linked lists KeyPointer 1
Data structures Implementation of binary trees KeyPointer 1Pointer 2
Data structures Implementation of general trees
Data structures 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, )
Data structures Stacks Operations MakeStack() Push(Key,S) Pop(S) IsEmpty(S) [Picture from J.Morris]
Data structures Stacks Operations MakeStack() Push(Key,S) Pop(S) IsEmpty(S) LIFO Last - in - first - out
Data structures 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 - MakeStack, Push
Data structures 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 Stacks - Pop, IsEmpty
Data structures Queues Operations MakeQueue() Enqueue(Key,Q) Dequeue(Q) IsEmpty(Q) FIFO First - in - first - out
Data structures 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 - MakeQueue
Data structures 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 - Enqueue
Data structures 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 Queues - Dequeue, IsEmpty
Data structures 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
Data structures Heaps - Examples This may be Heap
Data structures Heaps - Examples This may be Heap
Data structures Heaps - Examples This can not be Heap
Data structures Heaps - Examples This can not be Heap
Data structures Heaps - Examples This is Heap
Data structures Heaps - Examples This is not Heap
Data structures Heaps - Operations Min() ExtractMin() DecreaseKey(Key) Insert(Key) Delete(Key) MakeHeap() Heapify() InitialiseHeap()
Data structures 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)
Data structures Heaps - Implementation with an array LC(j) = 2j – n RC(j) = 2j – n – 1 P(j) = (j + n)/2
Data structures Heaps - Implementation with an array [Adapted from T.Cormen, C.Leiserson, R. Rivest]
Data structures Heaps - Insert T(n) = (h) = (log n)
Data structures Heaps - Delete T(n) = (h) = (log n)
Data structures Heaps - ExtractMin T(n) = (h) = (log n) 1