Vous êtes sur la page 1sur 22

Forward iterator

? ADT
• DIter-C = {it| it – iterator over c Î C }
• Operations
init(ContainerSeq c) construct, create
(current = first elem.)
current *
moveNext next ++
valid (not) isDone, end
--------------
rewind first , begin

ADT tuple

• ADT tuple (generalization)


Dn-tuple = { (e1,e2,…,en) | ei Î TEi , i Î {1,…,n}}
• compulsory operations
set and get operations
• Other operation  fields type
– init. default
– other type of initialization

ADT List

Domain
DList(Position; TE) = {l | l is a list of elements of type TE;
each element has a position of type Position}

Operations:
(1) Subalg. initEmpty(l)
Desc: create an empty list
pre: true
post: l : List and l is empty

(2) Funct. getElement(l; p)


Desc: get the element from a given position
pre: l : List and p : Position and valid(p; l)

1
post: result = the element e at the position p and e : TE

(3) Subalg. setElement(l; p; e)


Desc: set the element at a given position
pre: l : List and p : Position and valid(p; l) and e : TE
post: the element at the position p is equal to e

(4) Subalg. insert (l; p; e)


Desc: insert an element on a position (before)
pre: l : List and p : Position and e : TE and valid(p; l)
post: l’ is the list l after inserting e on (before) position p

(4’) Subalg. insertAfter(l; p; e)


Desc: insert an element after a position
pre: l : List and p : Position and e : TE and valid(l, p)
post: l’ is the list l after inserting e on the next position of p

(5) Subalg. delete(l; p)


Desc: delete the element on a position
pre: l : List and p : Position and valid(l, p)
post: l’ is the list l after removing the element on the position p
-----------
(6) Funct. getFirstPosition(l)
Desc: get first position of a list
pre: l : List
post: getFirstPosition =  if l is empty list
p, p : Position , p - the first position in l
if l is not empty

(7) Funct. getLastPosition(l)


Desc: get last position of a list
pre: l : List
post: getLastPosition =  if l is empty list
p, p : Position, p - the last position in l
if l is not empty

(8) Funct. next(l; p)


Desc: get next position
pre: l : List and p : Position and valid(l; p)
post: next = the next position of p, if p is not the last position in l

2
, if p is the last position

(9) Funct. prev(l; p)


Desc: get previous position
pre: l : List and p : Position and valid(l; p)
post: prev = the previous position of p, if p is not the first position in l
, if p is the last position

(10) Funct. valid(l; p)


Desc: verify if a position is valid
pre: l : List and p : Position
post: valid = true, if p is a valid position in l
false, otherwise
-----------

(11) Funct. getIterator(l)


Desc: create an iterator for current list
pre: l : List
post: getIterator = iterator for the list l

Doubly-Linked list

Node: record
info: TE
next: Position
prev: Position
end
Position: ^Node

Insertion of an object

(4’) Subalg. insertAfter(l; p; e)


Desc: insert an element after a position
pre: l : List and p : Position and e : TE and valid(l; p)
post: l’ is the list l after inserting e on the next position of p

Subalg. InsertAfter (l, p, e)


@ create a new node n and

3
n.info e
n.next p.next
n.prev @ the position of node p
@ in node indic. by p.next – set the field prev to position of node n
@ put in p.next the position of node n
endInsertAfter

(4’’) Subalg. insertBefore(l; p; e)


Desc: insert an element after a position
pre: l : List and p : Position and e : TE and valid(l; p)
post: l’ is the list l after inserting e on the position before p

Subalg. InsertBefore (l, p, e)


@ create a new node n and
// store element e into it
n.info e

@ in node indic. by p.prev – set the field next to position of node n
@ put in p. prev the position of node n
endInsertAfter

Singly-Linked list (SLL)

Node: record:
info: TE
next: Position
end
Position: ^Node

Operations see ADT List



Insertion of an element

(4’) Subalg. insertAfter(l; p; e)

4
Desc: insert an element after a position
pre: l : List and p : Position and e : TE and valid(l; p)
post: l’ is the list l after inserting e on the next position of p

Subalg. InsertAfter (l, p, e)


@ get memory for a new node n and
// store element e into it
n.info e
// make n to point to the same node as p does
n.next p.next
@ put in p.next the position of node n
endInsertAfter

Removal of an element

(5’) Subalg. deleteAfter(l; p)


Desc: delete the element after a position
pre: l : List and p : Position and valid(l; p) and valid(l; next(p))
post: l’ is the list l after removing the element on the position after p

Subalg. deleteAfter (l, p)


a p.next // node pointed by a will be removed
p.next a.next
@ free memory used for a
endDeleteAfter

struct Node{
TE info;
Node* next; };
typedef Node* Position;

Set

Domain
DSet (TE) = {s | s is a set of elements of type TE }

5
Operations:
Subalg. initEmpty(s) //create, createEmpty
Desc: create an empty set
pre: true
post: s : Set and s is empty

Subalg. add (s; e)


Desc: insert an element
pre: s : Set and e : TE
post: s’ is the set s after adding element e
// nothing is done if e is already in s

Subalg. remove (s; e) //delete , (!!) extract


Desc: remove an element
pre: s : Set and e : TE
post: s’ is the set s after removing the element e
// nothing to do if e is not in s

Function belongs(s, e);


Desc: verify if the set contains the element
pre: s : Set, e: TE
post: belongs = true if e  s
false if e  s

Function cardinal(s);
Desc: get the cardinal
pre: s : Set
post: cardinal = the number of elements of s

-----------
Subalg. union (s1, s2, s3)

Subalg. intersection (s1, s2, s3)

Subalg. difference (s1, s2, s3)

Subalg. inclusion (s1, s2, s3)

-----------
Funct. getIterator(s)
Desc: create an iterator for current set
pre: s : Set
post: getIterator = iterator for the set s
-----------
conversions operations
Funct. toVector (s) //getEnumeration

6
Desc: create an array with the elements of the set
pre: s : Set
post: toVector = the array with the elements of the set s

Bag
- a group of elements
- allow duplicate elements

other terms: multiset, collection

operations
 ~set

MAP

~collection of (key, value)


key: TCE
value: TE (mapped value)
(element)

other terms:
(unique) associative container, associative array, dictionary, finite map

operations
Subalg. initEmpty(d)
Subalg. add (d, k; v)
Func. remove (d; k)
//  value
Function size(d);
-----------
//belongs
Function searchByKey(d,k)
//  value
Function searchByValue(d,v)
//  key
-----------
conversions operation
Funct. keySet (d)
Funct. valueMultiset(d)

-----------
Funct. getIterator(d)

7
//  key
//  value

Multimap
multiple associative container
keys are not unique

ADT Queue

DQueue(TE) = {c | c is a collection of elements of type TE}


Operations:
(1) Subalg. init (c) //create, createEmpty, initEmpty
Desc: create an empty queue
pre: true
post: c : Queue and c is empty
(2) Subalg. push (c; e) //add, enqueue, push_back
Desc: add an element into queue
pre: c : Queue and e : TE and c – not full
post: the element e is added in c
(3) Function pop (c) // extract, dequeue, pop_front
(or equivalent subalg.)
Desc: extract an element from the queue
pre: c : Queue and c- not empty
post: pop = e and e : TE and e- the first introduced element
and c’ = c – {e}
-----------
(5) Function isEmpty(c)
Desc: verify if the queue is empty
pre: c : Queue
post: isEmpty = true if c is empty
false if c is not empty
----------- // size
(6) Function isFull(c)
Desc: verify if there is room to add at least one element (into the stack)
pre: c : Queue
post: isFull = true if c is full (cannot add an element)
false if c is not full
-----------
(4) Function front (c) (or equivalent subalg.)
Desc: find the FI element from the queue

8
pre: c : Queue and c- not empty
post: front = e and e : TE and e- the first introduced element

DS:
queue : record
el: array[1..MAX] of TE;
first, last: Integer
end

ADT Stack

DStack(TE) = {s | s is a collection of elements of type TE}


Operations:
(1) Subalg. init (s) //create, createEmpty, initEmpty
Desc: create an empty stack
pre: true
post: s : Stack and s is empty
(2) Subalg. push (s; e)
Desc: add an element into the stack
pre: s : Stack and e : TE and s - not full
post: the element e is added in s
(3) Function pop (s) (or equivalent subalg.)
Desc: extract an element from the stack
pre: s : Stack and s- not empty
post: pop = e and e : TE and e- the last introduced element
and s’ = s – {e}
-----------
(4) Function peek (s) (or equivalent subalg.)
Desc: get the value of top element from the stack
pre: s : Stack and s- not empty
post: peek = e and e : TE and e- the last introduced element
-----------
(5) Function isEmpty(s)
Desc: verify if the stack is empty
pre: s : Stack
post: isEmpty = true if s is empty
false if s is not empty

(6) Function isFull(s)


Desc: verify if there is room to add at least one (more) element
pre: s : Stack

9
post: isFull = true if s is full (cannot add an element)
false if s is not full

Tree as ADT

DTree <TE>= {a | tree with elements of type TE}

operations

Subalg. initEmpty(a)

Funct. isEmpty(a)

Funct root(a)
Desc: get the root of the tree
Pre: a DTree
Post: root = position of the root of a

Funct. rootValue(a)
Desc: get the value in the root of the tree
Pre: a – not empty
Post: e : TE and e – the value in the root of tree a

Funct. getSubTree(a, p, j)
Desc: get the subtree nr. j of p which is a node of a
Pre: a DTree , p – not empty (=> a – not empty)
Post: getSubTree = lt and lt DTree and lt is the subtree jof a

Funct. getParent (a, p)

Function search(a,e)

Subalg. nodeList(a,l)
Desc: get the list of values in the tree
Pre: a DTREE
Post: l : List and l-contains all the elements in a

10
Function. iterator(a)

 return iterator for tree a!!

Delete, insert

Function search(a,e)
p:=root(a)
while (p<>NIL) and (0<>compare(e, [p].info )
if (-1=compare(e, [p].info ) ) then
p := [p].left
else
p:= [p].right
endif
endwhile
search := p
endSearch

Subalgorithm add(a,e)
//Pointer to New node
pn := new TreeNode
[pn].info := e
[pn].left := NIL
[pn].right := NIL
p:=root(a)
pred := NIL
while (p<>NIL)
pred := p
if (-1=compare(e, [p].info ) ) then
p := [p].left
else
p:= [p].right
endif
endwhile
if pred = NIL then
setRoot(a, pn)
else
if (-1 = compare(e, [pred].info) then
[pred].left := pn
else
[pred].right := pn

11
endif
endif
endAdd

Subalg. delete(T, z)
if [z].left = NIL or [z].right = NIL then y := z
else y := SUCCESSOR(z)
endif
if [y].left <>NIL then x := [y].left
else x := [y].right
endif
q := [y].parent
if y <> z then z := y
endif
if x <> NIL then [x].parent := q
endif
if q = NIL then @root(T) := x
else if y = [q].left then [q].left := x
else [q].right := x
endif
endif
@delete y

Heap

Heap – insertion
1. Add the element on the bottom level of the heap.
2. Compare the added element with its parent
3. If they are not in the correct order
3.1 swap the element with its parent
3.2 goto step 2

Heap – delete the root


1.replace the root with the last elem. on the last level
2. the wrong node is swapped with its larger child
until it satisfies the heap property in its new position.

12
subalg. add (H,e)
H.n := H.n +1
H.Element[H.n] := e
upHeap (H, H.n)
endAdd

subalg. upHeap (H,i)


e := H.Element[i]
k:=i
p:=k div 2
while (p>=1) and (H.Element[p]<e) do
H.Element[k] := H.Element[p]
k:=p
p:=p div 2
endwhile
H.Element[k]:=e
endSubalg

subalg. extractMax (H,e) //if size(H)>=1


extractMax :=H.Element[1]
H.Element[1]:=H.Element[H.n]
H.n := H.n -1
downHeap(H,1)
endExtractMax

subalg. downHeap(H,poz)
e:=H.Element[poz]; p:=poz; ch:=2*poz
while ch<=H.n do
if ch<H.n then
if H.Element[ch]<H.Element[ch+1] then
ch:=ch+1
endif
endif
if H.Element[ch]<e then ch:=H.n+1
else H.Element[p]:=H.Element[ch]
p:=ch; ch:=2*ch
endif
endwhile
A.Element[p]:=e
endDownHeap

13
Red-Black Tree

Subalg. LeftRotate(T,x)
y:=[x].right
[x].right:=[y].left
if [y].left <> NIL then
[[y].left].parent:=x
endif
[y].parent := [x].parent
if [x].parent =NIL then
root(T) := y
else
if x= [[x].parent].left then
[[x].parent].left :=y
else
[[x].parent].right := y
endif
endif
[y].left := x
[x].parent := y
endSubalg

RBT_insert(T,e)
x:=BST-insert(T,e)
[x].color := red
while x<>root(T) and [[x].parent].color = red do
if [x].parent = [[[x].parent].parent].left then
y:= [[x].parent].parent].right
if [y].color=red then
[[x].parent].color:=black
[y].color:=black
x:=[[x].parent].parent
[x].color := red
else
if x=[[x].parent].right then
x:=[x].parent
LeftRotate(T,x)
endif

14
[[x].parent].color:=black
[[[x].parent].parent].color:=red
RightRotate(T, [[x].parent].parent])
endif
else

endif
endwhile

Delete as in BSTree
• A node to be deleted will have at most one child
• If the deleted node has a red child, repaint the child to black.
• If the deleted node is red, the tree is still a red-black tree.

while x<> root(T) and color (x)=black do


if x=[[x].parent].left then
w:=[[x].parent].right
if [w].color=red then
[w].color:=black
[[x].parent].color:=red
LeftRotate(T,[x].parent)
w:=[[x].parent].right
else
if [[w].left].color=black and [[w].right].color=black
then
[w].color:=red
x:=[x].parent
else
if [[w].right].color=black then
[[w].left].color=black
[w].color:=red
RightRotate(T,w)
w:=[[x].parent].right
endif
[w].color:=[[x].parent].color
[[x].parent].color:=black
[[w].right].color:=black
LeftRotate(T,[x].parent)
x:=root(T)
endif

15
endif
else

endif
endwhile
[x].color:=black

Hash

Direct address table

Function search(T,k)
Search := T[k]
endSearch
Subalg. insert(T,x)
T[key(x)] := x
endInsert
Subalg. delete(T,x)
T[key(x)] = NIL
endDelete

Collision resolution by chaining

Dictionary operations on a hash table T


insert(T,x)
insert x at the head of list T[h(key[x])
search(T,k)
search for an element with key k in list T[h(k)]
delete(T,x)
delete x from the list T[h(key[x])]

Open addressing-Linear probing

Assume that: (for next examples)


• the key k is identical to the element
• the elements in the hash table T are keys with no satellite information;
• each slot contains either a key or NIL

Function Insert(T,k)

16
i:=0
endProc:=false
repeat
j:=h(k,i)
if (T[j]=NIL) then
T[j]:=k
Insert:=j
endProc:=true
else
i:=i+1
endif
until (i=m) or endProc
if not endProc then @ error: "hash table overflow“ endif
endInsert

Function Search(T,k)
i:=0
Search := NIL
repeat
j:=h(k,i)
if T[j]=k then
Search := j
i:=m // exit repeat
else
i:=i+1
endif
until T[j]=NIL or i=m
endSearch

AVL tree

Insertion

• insert an element like in BST case


• consider all the ancestors (to the root)
and rebalance the tree (if it is the case)
rebalance  one or more tree rotations

Function RotateRight ( q )
p = [q].Left

17
[q].left = [p].right;
[p].right = q;
[q].height = Max( Height( [q].left ),
Height( [q].right ) ) + 1;
[p].height = Max( Height( [p].left ), [q].height ) + 1;
RotateRight := p /* New root */
}
Function DblRotateLeftRight ( r)
[r].left := RotateLeft ([r].left )
DblRotateLeftRight := RotateRight ( r );
endDblRotateLeftRight

Function Insert(x, p )
if( p == NIL) then
p = new AvlTreeNode
[p].info = x
[p].left = NIL
[p].right = NIL
[p].height = …
else
if( x < [p].info) then
[p].left = Insert( x, [p].left);
if(Height([p]. right) - Height( [p].left) = -2 )
if( x < [[p].left].info) then
p = RotateRight ( p );
else
p = DblRotateLeftRight ( p );
endif
endif
else // if( x >= [p].info)

endif
[p].height = Max( Height( [p].left ),
Height( [p].right ) ) + 1;
Insert := p
endif

Deletion
- require the tree to be rebalanced
I) First: find the node x where k is stored

18
II) Second: delete the contents of node x
Claim: Deleting a node in an AVL tree can be reduced to deleting a leaf

DeleteRebalanceRight
if( Height( [p].right ) - Height( [p].left ) = 2 ) then
if(Height( [[p].left ].left = Height( [p].right ) +1) then
p = RotateRight ( p )
else
p = DblRotateLeftRight(p)
endif

Function RebalanceForDelete (x, p )


if x = [p].info then
//delete …
else
if( x > [p].info) then
[p].right = Delete ( x, [p].right );
@ DeleteRebalanceRight
else // x( x < [p].info)
[p].left = Delete ( x, [p].left);
//… rebalance : DeleteRebalanceLeft
endif
[p].height = Max( Height( [p].left ), Height( [p].right ) ) + 1;
endif
RebalanceForDelete := p
End RebalanceForDelete

Counting sort

• the sort runs in O(n) time


Subalg. CountingSort ( A, B, k )
for i := 1 to k do C[i] := 0 endfor
for j := 1 to length[A] do C[A[j]] := C[A[j]] + 1 endfor
// C[i] now contains the nr. of elem. equal to i

for i := 2 to k do C[i] := C[i] + C[i-1] endfor


// C[i] now contains the nr. of elem. less than or equal to i

for j := length[A] downto 1 do


B[C[A[j]]] := A[j]

19
C[A[j]] := C[A[j]] - 1
endfor
EndCountingSort

Radix Sort
• sort by least significant digit first
into groups
but (otherwise) keep the original order
• combine them
• repeat the grouping process with each more significant digit

Subalg. radixSort(A, d) (Steps !!)


for i =1 to d do
@ do use a stable sort to sort array A on digit i
endfor
endRadixSort

Bucket Sort

• works by partitioning an array into a number of buckets


Steps of BucketSort subalg.:
1. Set up an array of initially empty buckets.
2. Go over the original array, putting each object in its bucket.
3. Sort each non-empty bucket.
4. Visit the buckets in order and put all elements back into the original array

Subalg. bucketSort(A)
n := length(A)
for i := 1 to n do
@ insert A[i] into list B[floor(n*A[i])]
endfor
for i := 0 to n –1 do
@ sort list B[i]
endfor
@concatenate the lists B[0], B[1], . . . , B[n - 1] // in order
endBucketSort.

20
Subsequence matching

• naïve algorithm complexity: O(n*m)


length(T)*length(P)
for i:=0 to length(T)-length(P) do
if
@ seq(P[1]…P[length(P)])
= seq(T[i+1] … T[i+length(P)])
then
@ P occurs in T starting with position i+1
endif
endfor

KMP algorithm

some terminology
Let x = x0 ... xk-1, a sequence of length k
• A prefix of x is a subsequence u with
u = x0 ... xb-1 where b Î {0, ..., k}
i.e. x starts with u.
• A suffix of x is a subsequence u with
u = xk-b ... xk-1 where b Î {0, ..., k}
i.e. x ends with u.
Example: x = abacab
prefixes: a, ab, aba, abac, abaca (ε)
suffixes: b, ab, cab, acab, bacab

Subalg. ComputePrefixFunct(P, PF) KMP


m := length (P)
PF[1]:=0 k:=0
for q:=2, m do
while k>0 and P[k+1]<>P[q] do
k:=PF[k]
endwhile
if P[k+1]=P[q] then k:=k+1 endif
PF[q]:=k
endfor
endCompute

Subalg. KMP match _ (T,P)

21
n := length (T); m := length (P)
ComputePrefixFunct(P, PF)
q:=0
for i:=1, n do
while q>0 and P[q+1]<>T[i] do
q:=PF[q]
endwhile
if P[q+1]=T[i] then q:=q+1 endif
if q=m then
@one endposition is i
q :=PF[q]
endif
endfor
endComputePrefixFunct

22

Vous aimerez peut-être aussi