



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




A -time non-recursive procedure to reverse a singly linked list of n elements with constant
storage is the following:
p ← head [ L ]
return error “empty list”
q ← next [ p ]
r ← NIL
do next[p] ← r r ← p p ← q q ← next[p]
next[p] ← r
head[L] ← p
Suppose we have a multi-array representation of length n such that the nodes of the doubly linked list with m nodes are compacted in the first m index entries. We organize the remaining n-m entries into a stack representing the free space. The top of the stack is top[free] , which is initially equal to 1 before the creation of the doubly linked list. Allocating a node is straightforward. However when a node is to be returned to free space, we need to recompact the linked list so that the nodes reside in the first indexed entries, and all the pointers are adjusted appropriately. We do this by moving the node that appears right before the top of the stack free into the location freed and adjust the pointers to that node.
Allocate-Object( )
If top[free]>n then error “out of space” else { x=top[free] ; top[free]=top[free]+1 ; return x ; }
Free-Object(x)
y=top[free]-1 ; if ( y=x ) then { top[free]=y ; return } else {if prev[y] is not NIL then next[prv[y]]=x ; else head[L]=x ; if next[y] is not NIL then prev[next[y]]=x ; next[x]=next[y]; prev[x]=prev[y]; key[x]=key[y]; top[free]=y; }
If only two pointers and a Boolean value can be used in each node, we will use one pointer as before to point to the leftmost child left[x] , and the other pointer either to point to a sibling unless it is the rightmost child in which case it points to the parent. To distinguish between these two cases, we use the Boolean value (say 0 when the pointer is used for a sibling and 1 when it points to a parent). It is clear that for any node we can explore all its children in time linear in the number of children and we can reach the parent by following the sibling pointer until we reach a node with the Boolean value set to 1 in which case we can reach the parent immediately.
Now define the random variable Y to be the total number of collisions, so that.
The expected number of collisions is
Y = (^) ∑ k ≠ lX kl
kl k l
kl k l
≠
≠
∑
∑
a. Since we assume uniform hashing, we can use the same observation as is used in Corollary 11.7: that inserting a key entails an unsuccessful search followed by placing the key into the first empty slot found. As in the proof of Theorem 11.6, if we let X be the random variable denoting
α ≤ 1 /2. Letting i = k + 1, we have Pr { X > k } = Pr { X ≥ k + 1 } ≤ (1/ 2)( k^ +1) 1^ −^ = 2 − k.
b. Substituting k = 2 lg n into the statement of part (a) yields that the probability that the i th
∪ · · · ∪ A n. Using Boole’s inequality, (C.18), we have
1 2
2
d. We use the definition of expectation and break the sum into two parts:
{ }
{ } { }
{ } { }
{ } { }
1 2lg
1 2lg 1 2lg
1 2lg 1 2lg
1 2lg 1
n
k n (^) n
k k n n (^) n
k k n n (^) n
k k n
= ⎡⎢ ⎤⎥
= = (^) ⎡⎢ ⎤⎥+ ⎡⎢ ⎤⎥
= = (^) ⎡⎢ ⎤⎥+ ⎡⎢ ⎤⎥
= = (^) ⎡⎢ ⎤⎥+
∑
∑ ∑
∑ ∑
∑ ∑
Since X takes on exactly one value, we have that
{ } { } and
2lg
1
n
k
⎡⎢ ⎤⎥
=
∑ =^ =^ ≤^ ⎡⎢^ ⎤⎥ ≤ {^ }^ {^ } 2lg 1
n
k n
= ⎡⎢ ⎤⎥+
∑ =^ ≤^ >^ ≤ n^ ,^ by
part (c). Therefore,
set of k keys. The probability that these k keys are inserted into the slot in question and that all other keys are inserted elsewhere is
k n
−
k
Since there are ways to choose our k keys, we get
k n k k
−
and let A i be the event that X i = k , i.e., that exactly k keys hash to slot i. From part (a), we have Pr
{ A } = Qk. Then,
k
i n i
≤ ≤
≤ Pr {there exists i such that X i = k } = Pr { A 1 ∪ A 2 ∪ · · · ∪ An }
of equation (3.17), we have
k n k k
n k k
k
k k k
−
−