300 likes | 313 Views
Learn about organizing data in computer memory using arrays, linked lists, stacks, queues, and heaps. Explore various operations such as insert, delete, extract min, and more.
E N D
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:
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]
Stacks Operations MakeStack() Push(Key,S) Pop(S) IsEmpty(S) LIFO Last - in - first - out
Stacks - MakeStack, Push struct Cell{intKey, pointerNext} struct Stack{pointerHead} procedure MakeStack(): S newStack S.Head 0 return S procedurePush(int Key, Stack S): C newCell C.Next S.Head C.Key Key S.Head C
Stacks - Pop, IsEmpty procedurePop(Stack S): C S.Head Key C.Key S.Head C.Next delete C return Key procedureIsEmpty(Stack S): if S.Head 0then return 0 else return 1
Queues FIFO First - in - first - out Operations MakeQueue() Enqueue(Key,Q) Dequeue(Q) IsEmpty(Q)
Queues - MakeQueue struct Cell{intKey, pointerNext} struct Queue{pointerHead, pointerTail} procedure MakeQueue(): Q newQueue Q.Head 0 Q.Tail 0 return Q
Queues - Enqueue procedureEnqueue(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 procedureDequeue(Queue Q): C Q.Head Key C.Key Q.Head C.Next if Q.Head= 0 then Q.Tail 0 delete C return Key procedureIsEmpty(Queue Q): if Q.Head 0then 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 LC(j) = 2j–n – 1 1 RC(j) = 2j–n–2 2 12 P(j) = 1 + (j+ n)/2 3 45 13
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)