Professional Documents
Culture Documents
Set terminology
- belongs to or is an element of
Set terminology
- is a subset of
- is a proper subset of
Set terminology
Set operations
complement () not
A {x U : x A}
| A || U | | A |
intersection () and
A B {x U : x A x B}
union () or
A B {x U : x A x B}
| A B || A | | B | | A B |
DSA - lecture 3 - T.U. Cluj-Napoca - M. Joldos
Set operations
A \ B {x U : x A x B} A B
| A \ B || A | | A B |
A B ( A \ B) ( B \ A)
( A B) ( A B)
U={0, 1, 2, 3, 4, 5, 6, 7,
8, 9} universe of keys
K={1, 4, 5, 7} actual
keys
collision
For insertion we
successively examine
the hash table looking
for an unoccupied slot
The slots we check
depend on the key we
wish to insert
10
Hash function h
Array (called table) of size N
11
Hash functions
h(x) = h2(h1(x))
12
Hash codes
Memory address:
Integer cast:
Component sum:
13
Hash codes
Polynomial p(x) can be
evaluated in O(n) time
We partition the bits of the key
into a sequence of components of using Horners rule:
Polynomial
accumulation:
a0 a1 an1
The following
polynomials are
successively computed,
each from the previous
one in O(1) time
p0(x) = an1
pi (x) = ani1 + xpi1(z)
(i = 1, 2, , n 1)
We have p(x) = pn1(x)
14
Compression Functions
Division:
h2 (y) = y mod m
The size m of the
hash table is usually
chosen to be a prime
The reason has to do
with number theory
and is beyond the
scope of this course
15
Rehashing Strategies
Linear hashing
Quadratic hashing
16
Rehashing Strategies
Double
hashing
17
18
small databases
compilers
browser caches
19
20
http://www.engin.umd.umich.edu/CIS/course.des/
cis350/hashing/WEB/HashApplet.htm
http://www.cs.auckland.ac.nz/software/AlgAnim/h
ash_tables.html
21
Mapping ADT
22
23
24
Reflexive property: x x
Anti-symmetric property: x y y x x = y
Transitive property: x y y z x z
25
Unsorted list
Performance:
Sorted list
Performance:
26
Binary tree
At the lowest level, where some leaves may be missing,
we require that all missing leaves are to the right of all
leaves that are not on the lowest level.
Tree is partially ordered: the priority of node v is no greater
than the priority of the children of v
27
insert in a POT
new element as
far left as possible
on the lowest level
exchange node
with its parent
Initial tree
28
it is empty or
its left subtree is complete of height h-1 and its right subtree
is completely full of height h-2 or
its left subtree is completely full of height h-2 and its right
subtree is complete of height h-1.
all the leaves are either on the same level or two adjacent
ones, and
all nodes at the lowest level are as far to the left as
possible.
29
Heaps
it is empty or
the key in the root is larger than that in either child
and both subtrees have the heap property.
heap (min-heap)
value of the heap structure: we can both extract
the highest (lowest) priority item and insert a new
one in O(log n) time
DSA - lecture 3 - T.U. Cluj-Napoca - M. Joldos
30
31
32
33
Heap operations
34
Heap operations
35
http://www.cs.auckland.ac.nz/software/AlgAn
im/heaps.html
http://www.cs.auckland.ac.nz/software/AlgAn
im/heapsort.html
36
Summary
Sets in general
Abstract data types
based on sets
operations
Implementations
lists
Mapping ADT
Priority queues
Implementations
Hash table
Implementations
Dictionary ADT
Implementations
Operations: insert,
deleteMin
Heaps
Operations: insert,
deleteMax
Implementation in arrays
37
Reading
38