1 / 30

Data Structures and Operations: Arrays, Linked Lists, Stacks, Queues, Heaps

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.

micaht
Download Presentation

Data Structures and Operations: Arrays, Linked Lists, Stacks, Queues, Heaps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Data organization The basic possibilities are to store data either in arrays: or to link them with pointers:

  3. Some types of “linked objects” Linked lists: Double-linked lists:

  4. Some types of “linked objects” Trees:

  5. Implementation of linked lists

  6. Implementation of binary trees

  7. Implementation of general trees

  8. 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, )

  9. Stacks Operations MakeStack() Push(Key,S) Pop(S) IsEmpty(S) [Picture from J.Morris]

  10. Stacks Operations MakeStack() Push(Key,S) Pop(S) IsEmpty(S) LIFO Last - in - first - out

  11. 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

  12. 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

  13. Queues FIFO First - in - first - out Operations MakeQueue() Enqueue(Key,Q) Dequeue(Q) IsEmpty(Q)

  14. Queues - MakeQueue struct Cell{intKey, pointerNext} struct Queue{pointerHead, pointerTail} procedure MakeQueue(): Q newQueue Q.Head  0 Q.Tail  0 return Q

  15. 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

  16. 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

  17. 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

  18. Heaps - Examples This may be Heap

  19. Heaps - Examples This may be Heap

  20. Heaps - Examples This can not be Heap

  21. Heaps - Examples This can not be Heap

  22. Heaps - Examples This is Heap 1 2 12 3 45 13 14

  23. Heaps - Examples This is not Heap 1 2 12 3 45 5 14

  24. Heaps - Operations Min() ExtractMin() DecreaseKey(Key) Insert(Key) Delete(Key) MakeHeap() Heapify() InitialiseHeap()

  25. 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)

  26. 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

  27. Heaps - Implementation with an array [Adapted from T.Cormen, C.Leiserson, R. Rivest]

  28. Heaps - Insert 2 3 12 7 45 13 1 T(n) = (h) = (log n)

  29. Heaps - Delete 2 3 12 7 45 13 14 T(n) = (h) = (log n)

  30. Heaps - ExtractMin 1 3 12 7 45 13 14 T(n) = (h) = (log n)

More Related