Sei sulla pagina 1di 4

7/24/12 Data Structures Cheat Sheet

1/4 www.scribd.com/doc/39557873/Data-Structures-Cheat-Sheet
Scribd
Upload
Search books, presentations, business, academics... Search
Explore
Documents
Books - Fiction
Books - Non-fiction
Health & Medicine
Brochures/Catalogs
Government Docs
How-To Guides/Manuals
Magazines/Newspapers
Recipes/Menus
School Work
+ all categories
Featured
Recent
People
Authors
Students
Researchers
Publishers
Government & Nonprofits
Businesses
Musicians
Artists & Designers
Teachers
+ all categories
Most Followed
Popular
amitgos
View Public Profile
My Documents
My Collections
My Shelf
My Stats
Messages
Notifications
Account
Help
Log Out

Data Structures - Cheat Sheet

" ! 0 `
1. 0 * " : A red child must have a black father
2. " ! 0 * " : All paths to external nodes pass through the
$ ` .
Melding: If the heap is represented by an arra
arrays together and Heapify-Up. O(n).
7/24/12 Data Structures Cheat Sheet
2/4 www.scribd.com/doc/39557873/Data-Structures-Cheat-Sheet
2. " ! 0 * " : All paths to external nodes pass through the
samenumberofblacknodes.
3. All the leaves are black, and the sky is grey.
Rotationsareterminalcases.Onlyhappenonceperxup.
If we have a series of insert-delete forwhichtheinsertionpoint
isknown,theamortizedcosttoeachactionis O(n).
Height:log n h 2log n
Limit of rotations: 2 per insert.
Bound of ratios between two branches L, R: S(R) (S(L))
2
Completely isomorphic to 2-4 Trees.
`
d denes the minimum number of keysonanode
Height: h log
d
n
1. Every node has at most d children and at least
d
2
children
(root excluded).
2. The root has at least 2 children if it isnt a leaf.
3. A non-leaf node with k childrencontains k 1 keys.
4. On B+ trees, leaves appear at the same level.
5. Nodes at each level form linked lists
d isoptimizedforHDD/cacheblocksize
$ ( ` ) Add to insertion point. If the node gets too large,
split.O(log n) O(log
d
n)
& " ) Themiddleofthenode(lowmedian)movesuptobethe
edge of the father node. O(d)
" ) Ifthekeyisnotinaleaf,switchwithsucc/pred.Delete,
and deal with short node v:
1. If v istheroot,discard;terminate.
2. If v hasanon-shortsibling,stealfromit;terminate.
3. Fuse v withitssibling, repeat with p p [v].
` + ` ( " (

t==null
print (t)
Traverse(t.left)
(OR) print(t)
Traverse(t.right)
(OR) print(t)

$ ` . 0 $ %# " 0 %$
findMin (1) (1) (1)
deleteMin (log n) (log n) O(log n)
insert (log n) O(log n) (1)
decreaseKey (log n) (log n) (1)
meld
(n) (log n) (1)
$ %# "
Melding:Unifytreesbyranklikebinarysummation.
%$ 0 &
- # * # 0 ` D(n)

log

n

; =
(1+
Minimumsizeofdegree k: s
k
F
k+2
` ! $ Everynodewhichlostonechildismarked.
( $ 0 * ) : Cut every marked node clim
Keeps amortized O(log n) time for deleteMin.
O(

n).
` %% 0% 0)
k
$ % 0( / 0 %* $
1. All subtrees of junction j, sorted by order of insertion are of
degree D[s
i
] i 2 (Proof: when xslargestsubtreewasadded,
since D[x] was i 1, so was the subtree. Since then
lose only one child, so it is at least i 2)
2. F
k+2
=1 +

k
i=0
F
i
; F
k+2

k
3. If x is a node and k = deg [x], S
x

(Proof: Assume induction after the base cases
2+

k
i=2
S i2 2+

k
i=2
Fi =1 +

k
i=0
Fi =
) ` * ) * ` (
Median Heap: one min-heap and one max-he
min, y max: x > y thentheminimumisonthemedian-heap

%# & ` " (
Algorithm Expected Worst St
* ! %` ) O(nlog n) O

n
2

In-Place

* " %` ) O

n
2

In-Place
" ) %$ %` ) O

n
2

In-Place
n
A[n]
A[n 1] .
& %` ) O(nlog n) Aux
$ ( ` ) %$ %` ) Aux
` %` ) O(nlog n) Aux
$ ` 0 #
* ! ) %` ) (n):
If the range is known, make the appropriate number of buckets,
then:
1.Scatter:Goovertheoriginalarray,puttingeachobjectinits
bucket.
2. Sort each non-empty bucket (recursively or otherwise)
1

7/24/12 Data Structures Cheat Sheet
3/4 www.scribd.com/doc/39557873/Data-Structures-Cheat-Sheet
3. Gather: Visit the buckets in order and put all elements back
into the original array.
%* $ ) %` ) (n):
1. Given an array A boundedinthediscreterange C, initialize
anarraywiththatsize.
2.Passingthrough A,incrementeveryoccurenceofanumber i
in its proper slot in C.
3. Passing through C,addthenumberrepresentedby i into A
a total of C[i] times.
- %` ) (n):
1. Take the least signicant digit.
2. Group the keys based on that digit, but otherwise keep the
original order of keys. (This is what makes the LSD radix sort
astablesort).
3. Repeat the grouping process with each more signicant digit.

* ! " ) O(n) O

n
2

) * & " 0 " )

$ + ` ( " 0 # " . : a family of mappings H. h H. h : U
[m] is universal i k
1
= k
2
U : Pr
hH
[h(k
1
) = h(k
2
)]
1
m
Example: If U = [p] = 0, 1, . . . , p 1then H
p,m
=
h
a,b
1 a p; 0 b p and every hash function is
h
a,b
(k)=((ak +b) mod(p)) mod(m)
$ ` 0 ` % $ : Searchinincrementalorderthroughthetable
from h(x) until a vacancy is found.
& $ Addressing Use h
1
(x) to hash and h
2
(x)topermute.
No pointers.
& $ Hashing
` ) 0 ( Whenonefunctionclashes,tryanother. O( ).
% 0 ) %` Thelengthofa possiblecollisionchain. When
U= n, =
m
n
.
) % (
% * " ` Multipilicative, Additive, Tabular(byte)-additive
` %` # $
$ $ E[X] %` ( ) 0 (
SuccessfulSearch/Del
1
2
(1+ ) n
Failed Search/Veried Insert 1+ n
` % $
$ ` h(k, i) =(h

(k)+ i) mod m
* ` ) h

k, i) =

h

(k)+ c
1
i +c
2
i
2

mod m
%* " h(k, i) = (h (k)+ ih (k)) # % m
, % + " 0 ( $
Thenumberofcollisionsperlevel:

n1
i=0

n
i
2
1. Choose m = n and h such that Col< n.
2. Store the n
i
elementshashedto i in a small table of size
using a perfect hash function h
i
.
$ %# 0 " %` ) # 0 %` 0 %$ ( ) ` * ) $ 0 perfect
( 0) "
1. Choose a random h from H(n) and compute the number of
collisions.Iftherearemorethanncollisions,repeat.
2.Foreachcell i,if ni > 1,choosearandomhashfunctionfrom
H(ni2). If there are any collisions, repeat.
- & ) 0 %$ ( ) ` * ) %$ 0) # 0 O(n)
%` ( ) 0 ( 0( ` 0) # 0 O(1)

MakeSet(x) Union(x, y) Find(x)
O(1) O(1) O((k))
$ %$ 0 . 0 $ ! The larger tree remains the master tree in
every union.
) 0 %# & ` ( ( %$ every nd operationrstndsthemaster
root,thenrepeatsitswalktochangethesubroots.

( ) ` 0 %` # for T (n) = aT

n
b

+ f (n
1, > 0:


T (n) =

n
log a

f (n) = O

n
log
T (n) =

n
log

a
log
k+1
n

f (n) =

n
log
T (n)=(f (n))
f (n) =

n
log

af

n
b

cf (n)
Building a recursion tree: build one tree for running times (at
T (n)) and one for f (n).

f = O(g) limsup
x
f
g
< f = o(g)
f
g
f = (g) lim
x
f
g
R
+
f = (g) limi nf
x
f
g
> 0 f = (g)
f
g

%) $ ) " 0 ) % Set to examine a parameter on data
stucture D
i
where i indexes the state of the str
theactualcostofaction i, then c
i
= c
i
+(D
i
)
The total potential amortized cost will then

n
i=1
(c
i
+(D
i
) (D
i1
)) =

n
i=1
c
i
+(D
) ` # $ ( ) 0 " %` ) # Alwayspredictable.
) ` " $ ( 0 & & ` %- # ) %$ n!

2n

n
7/24/12 Data Structures Cheat Sheet
4/4 www.scribd.com/doc/39557873/Data-Structures-Cheat-Sheet
Leave a Comment
Submit
Characters: 400
Submit
Characters: ...
Upload
Search books, presentations, business, academics... Search
Follow Us!
scribd.com/scribd
twitter.com/scribd
facebook.com/scribd
About
Press
Blog
Partners
Scribd 101
Web Stuff
Support
FAQ
Developers / API
Jobs
Terms
Copyright
AdChoices
Advertise
Privacy
Copyright 2012 Scribd Inc.
Language:
English


%* " h(k, i) = (h
1
(k)+ ih
2
(k)) # % m
E[X] $ ( * ( ( * " 0 ` 0 * ( ( * " 0 `
Uni. Probing
1
1
1

ln
1
1
Lin. Probing
1
2

1+

1
1

2

1
2

1+
1
1

SoLinearProbingisslightlyworsebutbetterforcache.
%" " ( %$ 0 - & ) ) %$ P[X

2E[X]]

1
2
So:
1. if m = n then E[Col< n]
n
2
2. if m = n
2
then E[Col< 1]
1
2
And with 2 there are no
collisions.
) ` " $ ( 0 & & ` %- # ) %$ n!

2n

n
e
xlog x x
Scribbled by Omer Shapira, based on the course Data Structures at Tel Aviv
University.
Redistributefreely.
Website: http://www.omershapira.com

Potrebbero piacerti anche