Académique Documents
Professionnel Documents
Culture Documents
? 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 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
1
post: result = the element e at the position p and e : TE
2
, if p is the last position
Doubly-Linked list
Node: record
info: TE
next: Position
prev: Position
end
Position: ^Node
Insertion of an object
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
Node: record:
info: TE
next: Position
end
Position: ^Node
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
Removal of an element
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
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
operations
~set
MAP
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
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
9
post: isFull = true if s is full (cannot add an element)
false if s is not full
Tree as ADT
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
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
12
subalg. add (H,e)
H.n := H.n +1
H.Element[H.n] := e
upHeap (H, H.n)
endAdd
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.
15
endif
else
…
endif
endwhile
[x].color:=black
Hash
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
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
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
Counting sort
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
Bucket Sort
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
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
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