Download presentation
Presentation is loading. Please wait.
Published byJuniper Blake Modified over 9 years ago
1
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
2
Data structures Data organization The basic possibilities are to store data either in arrays: or to link them with pointers:
3
Data structures Some types of “linked objects” Linked lists: Double-linked lists:
4
Data structures Some types of “linked objects” Trees:
5
Data structures Implementation of linked lists KeyPointer 1
6
Data structures Implementation of binary trees KeyPointer 1Pointer 2
7
Data structures Implementation of general trees
8
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, )
9
Data structures Stacks Operations MakeStack() Push(Key,S) Pop(S) IsEmpty(S) [Picture from J.Morris]
10
Data structures Stacks Operations MakeStack() Push(Key,S) Pop(S) IsEmpty(S) LIFO Last - in - first - out
11
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
12
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
13
Data structures Queues Operations MakeQueue() Enqueue(Key,Q) Dequeue(Q) IsEmpty(Q) FIFO First - in - first - out
14
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
15
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
16
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
17
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
18
Data structures Heaps - Examples This may be Heap
19
Data structures Heaps - Examples This may be Heap
20
Data structures Heaps - Examples This can not be Heap
21
Data structures Heaps - Examples This can not be Heap
22
Data structures Heaps - Examples 2 45 12 14 3 1 13 This is Heap
23
Data structures Heaps - Examples 2 45 12 14 3 1 5 This is not Heap
24
Data structures Heaps - Operations Min() ExtractMin() DecreaseKey(Key) Insert(Key) Delete(Key) MakeHeap() Heapify() InitialiseHeap()
25
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)
26
Data structures Heaps - Implementation with an array 2 45 12 3 1 13 4531221 LC(j) = 2j – n RC(j) = 2j – n – 1 P(j) = (j + n)/2
27
Data structures Heaps - Implementation with an array [Adapted from T.Cormen, C.Leiserson, R. Rivest]
28
Data structures Heaps - Insert 3 45 12 7 2 131 T(n) = (h) = (log n)
29
Data structures Heaps - Delete 3 45 12 7 2 1314 T(n) = (h) = (log n)
30
Data structures Heaps - ExtractMin 3 45 12 7 1314 T(n) = (h) = (log n) 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.