Download presentation
Presentation is loading. Please wait.
Published byGordon Green Modified over 6 years ago
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
Key Pointer 1
6
Implementation of binary trees
Key Pointer 1 Pointer 2
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
LIFO Stacks Operations MakeStack() Push(Key,S) Pop(S) IsEmpty(S)
Last - in - first - out
11
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
12
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
13
FIFO Queues Operations MakeQueue() Enqueue(Key,Q)
First - in - first - out Operations MakeQueue() Enqueue(Key,Q) Dequeue(Q) IsEmpty(Q)
14
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
15
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
16
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
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
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
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.