Sie sind auf Seite 1von 9

Homework 5

Solutions

Problem 1
Show that methods above(p) and before(p) are not actually needed to efficiently implement a
dictionary using a skip list. That is, we can implement item insertion and removal in a skip list
using a strict top-down, scan-forward approach, without ever using the above or before methods.
Describe these insertItem and removeElement methods in pseudo-code.
Solution The main idea here is that we can do all the coin flips before we do any skip-list
traversals or insertions.
Since with high probability, the height of S is O(log n), we can always compute the height by
going down from the topmost left element till we hit the rock bottom. The insertion algorithm
consists of the following steps
Flip the coin several times until heads come up
Compute the height of the skip-list if necessary (unless you store this information and update
it during insertion/removal stage).
If the number of coin tosses is more than the height of the skip-list, add new level(s), otherwise
find the topmost level in which to start insertion (i.e. go down heigh-of-the-list - number-
coin-tosses levels).
Starting from the position found in the previous step, do something very similar to SkipSearch
except that you also have to insert the new element at each level befor you skip down.
Assume that algorithms insertItem and removeElement are methods of a skip-list S, so we have
access to its internals. In particular, we have access to toplef t position (the topmost-left position
of the skip-list).
Note that the top level of a skip-list does not contain any actual items.
Algorithm insertItem(k, e)
Input: Item(k,e)
ctosses 0
while (random() < 1/2) do
ctosses ctosses + 1
height 1
current toplef t
while (below(current) 6= null) do
current below(current)
height height + 1
{ Insert new levels if necessary and find the first level to insert at}
if (height < ctosses) then
for i 1 to ctosses height do
oldtoplef t toplef t
toplef t new Item(,null)
after(toplef t) new Item(,null)
below(toplef t) oldtoplef t
below(after(toplef t)) after(oldtoplef t)

CS 16 Introduction to Algorithms and Data Structures Semester II, 9900


2

insertat below(toplef t)
else
insertat toplef t
for i 0 to (height ctosses + 1) do
insertat below(insertat)
{ Now do SkipSearch, inserting before going down }
oldnewnode null
while (insertat 6= null) do
while(key(after(insertat)) k) do
insertat after(insertat)
newnode new Item(k,e)
after(newnode) after(insertat)
if (oldnewnode 6= null) then
below(oldnewnode) newnode
after(insertat) newnode
oldnewnode newnode
insertat below(insertat)

Note that this solution is O(log(n)) expected time (for the same reasons tha SkipSearch is
O(log(n))).
For the removal algorithm, we SkipSearch down the skip-list for the topmost leftmost item such
that the item to the right has key k.
Algorithm removeElement(k)
Input: Key k of an element to remove
current toplef t
while (below(current) 6= null) do
current below(current)
while key(after(current)) < k do
current after(current)
if after(current) = k then
tmp after(current)
after(current) after(after(current))
delete tmp

CS 16 Introduction to Algorithms and Data Structures Semester II, 9900


3

Problem 2 (YADP: Yet Another Drawing Problem.)

62

44 78

17 50 88

48 54

Figure 1: AVL tree for problems 2(a) and 2(b)

(a) Draw the AVL tree resulting from the insertion of an item with a key 52 into the AVL
tree of Figure 1 (show the tree after each rotation, if any).
Solution

62 62
z=a
44 78 50 78
y=b
17 50 88 44 54 88
w=c
48 54 17 48 52

52

(b) Draw the AVL tree resulting from the removal of the item with key 62 from the AVL
tree of Figure 1 (show the tree after each rotation, if any).
Solution
c=z
78 50

y=a
44 88 44 78
b=x
17 50 17 48 54 88

48 54

CS 16 Introduction to Algorithms and Data Structures Semester II, 9900


4

(c) Draw an example of an AVL tree such that a single remove operation causes rotations
to propagate all the way to the root. (Use triangles to represent subtreees that are not affected by
this operations.) The height of your example tree should be at least 5. Show the tree after each
rotation.
Solution
[Solution courtesy of the former cs16 sarcastic graduate TAs]
In a generic sense, we are looking for a certain type of subtree (of arbitrary height) for which
removing an element causes the entire subtree to decrease in height. Consider the tree rooted at z:
zk
``` `
6 B y`
k
 B  ``
k+1 B B xk
PP
 T B  B 
 B  U B B B 6
? B  B  B  B k
 V B W B ?
 B  B

If T were to decrease in height by one (to k), that would cause this whole (sub)tree to become
unbalanced, causing a rotation and resulting in the following post-rotation tree:
yk
((((hhhh
zk zk
PP PP
6 B B B B 6
k  B  B  B  B k
? T B  U B  V B  W B ?
 B  B  B  B

Note that before the rotation, the tree was of height k + 3, but now it is k + 2: it has shrunk
by one level. Now, in a bit of handwaving, we blithely said that T would decrease in height by
one; with that we constructed a larger tree which would also decrease in height by one (after a
rotation). The base case of this little induction is where k = 0 and T is just a single node which
is deleted; from which we can construct any number of larger trees by performing the construction
with T equal to a smaller tree with this property.
So here is a concrete example, which is (not coincidentally) also the most correct homework
answer: an AVL tree of height 5 which, upon the deletion of one node, causes two rotations. (The
externals are omitted because that would just get sooooooo messy.)

z2
(
( hhhh
((((
(
 hh
h

6 z1 y2
 
3 H  ````


 H
H
 
 `
16 y1 x2
?   
 XXX
@  H H  X
 
 H
 
 X

x1 6
?      2
H H


 HH
 

 HH

   
?
The leftmost unmarked node is by itself the subtree T in the first iteration of the construction
(T1 ); the other subtrees U, V, W are null here. This is a textbook example of an AVL rotation,
and clearly when this node is removed, the height of the tree rooted at z1 will shrink by one. So

CS 16 Introduction to Algorithms and Data Structures Semester II, 9900


5

if we consider the subtree rooted at z1 to be itself the subtree T2 , for the second iteration, we can
construct around it a larger tree which would have to undergo further rotation.
Note that I called that the most correct answer to the problem; this is because, if I wanted
to, I could plug this tree into yet another iteration of the algorithm, and come out with a height-7
tree which would require three rotations. Ad infinitum. A slightly less correct solution would be if
y2 s left subtree (a.k.a. U2 ) were one level higher; then the tree would require two rotations, but
wouldnt really be a general example. (No points were deducted for this, however.)
There are, of course, a few valid variations; for example, z, y, and x can be related in any
of the four ways leading up to a rotation (either single or double). Instead of the anonymous
3-node subtrees we used for U2 , V2 , and W2 , we could have used 2-node subtrees, and all the
conditions would still hold. And, of course, we could make our base case with k1 = 1 instead of
k1 = 0, which wouldnt have affected the analysis, but would have made the diagrams a lot messier.
(It also would have made the tree of height 6.) Speaking of messy diagrams: the problem did
allow for answers which specified irrelevant subtrees with triangles; thus, we could reasonably have
substituted triangles of height 2 for the three anonymous 3-node subtrees, but the tree is small
enough that its also not unreasonable to just draw them out.
Analysis: this construction generates trees such that the number of required rotations r = h1 2
(assuming k1 = 0). The height h of an AVL tree is always (log n). Thus this tree (and all
generated like it) will require (log n) rotations.

Problem 3
(a) Consider a (2,4) tree T storing 100,000 items. What are the best-case and the worst-
case heights of T ? Justify.
Solution
Best-case: It is known from proposition 8.5 that the height cannot be less then 1/2 log 100, 001 =
8.3048275 . . . . So the question is: can it be 9? And the answer is: yes. Basically the idea is to
place as many 4-nodes as possible at each level starting from the lowest level, and then carefully
rediscribute the overflow. The following is a possible node distribution per level that a 2-4 tree can
have to have height 9:
Level Nodes 4-Nodes 3-Nodes 2-Nodes
0 1 0 0 1
1 2 1 1 0
2 7 5 1 1
3 25 24 0 1
4 98 97 1 0
5 391 390 1 0
6 1,563 1,562 1 0
7 6,251 6,249 1 1
8 25,001 24,999 1 1
9 100,001

Worst-case: We know from proposition 8.5 that the height cannot be more then log 100, 001 =
16.609655 . . . . The question is, can we do as well as 16, and the answer is, yes. Imagine that we
put 100,001 nodes in the bottom level of the tree, and then try to go upwards from there in the
most inefficient way possible. Which is to say, we try to use as many 2-nodes as possible. At each
level of the tree, we can make pairs of nodes be children of a 2-node a level higher, and if there is
an odd number of nodes in the level, we make the last node a child of one of the 2-nodes above

CS 16 Introduction to Algorithms and Data Structures Semester II, 9900


6

(thus turning it into a 3-node).

even number
in this level

odd number
in this level

So we start at level h with 100, 001 nodes. If a level has n nodes, then the level above it is going
to have n/2 nodes if n it is even, and (n 3)/2 + 1 = (n 1)/2 nodes if it is odd. Let me be extra
meticulous for a second, and show exactly how many nodes of each kind we should have at each
level to achieve 16 levels:
Level Nodes 2-Nodes 3-Nodes
0 1 1 0
1 2 1 1
2 5 4 1
3 11 10 1
4 23 23 0
5 46 46 0
6 92 91 1
7 185 185 0
8 390 389 1
9 781 781 0
10 1,562 1,561 1
11 3,125 3,125 0
12 6,250 6,250 0
13 12,500 12,500 0
14 25,000 25,000 0
15 50,000 49,999 1
16 100,001
(b) A hapless cs16 grad TA claims that a (2,4) tree storing a set of items will always have
the same structure, regardless of the order in which the items are inserted. Show that she is wrong.
(Your example should have no more then 7 items. Draw the tree after each insertion.)
Solution If we insert integers from 1 to 5 in the following orders, we will end up with different
(2,4) trees: (1,2,3,4,5) and (2,3,4,5,1).

CS 16 Introduction to Algorithms and Data Structures Semester II, 9900


7

(1,2,3,4,5)

1 1 2 1 2 3 3 3

1 2 4 1 2 4 5

(2,3,4,5,1)

2 2 3 2 3 4 4 4

2 3 5 1 2 3 5

Problem 4
(a) Consider the following sequence of keys: (18,30,50,12,1). Insert the items with this set
of keys in the order given into the red-black tree in Figure 2. Draw the tree after each insertion.

16

10 22

2 15 45

Solution

CS 16 Introduction to Algorithms and Data Structures Semester II, 9900


8

16 16 16

10 22 10 22 10 22

2 15 18 45 2 15 18 45 2 15 18 45

30 30 50

16 16

10 22 10 22

2 15 18 45 2 15 18 45

12 30 50 1 12 30 50

(b) Design an O(log n) algorithm that determines whether a red-black tree with n key s
stores any keys within a certain (closed) interval. That is, the input to the algorit hm is a red-black
tree T and two keys, l and r (l r). If T has at least one key k such that l k r, then the
algorithm returns true , otherwise it returns false.
Solution
Red-black tree is actually just a binary search tree with bells and whistles, so we can use
TreeSearch algorithm used on binary search trees for it. Note that if there is no key within a
particular closed interval, then TreeSearch algorithm for both, l and r, will terminate at the same
external node. Remember, if a key does not exist in a binary search tree, TreeSearch terminates at
an external node encountered in the inorder traversal of the tree after all the internal nodes with
keys smaller than the key and before all the internal nodes with keys greater than it. So the fact
that TreeSearch on both interval endpoints terminates at the same external nodes means exactly
that there are no keys in that interval in our tree.
TreeSearch is described on page 343 of Packet 3 (Chapter 8).
Algorithm hasKeysInInterval(T ,l,r)
Input: Red-black tree T , keys l and r
Output: true if there exists a key in T such that l k r, false otherwise
lef tnode TreeSearch(T ,T .root(),l)
rightnode TreeSearch(T ,T .root(),r)
if (T .isExternal(lef tnode) and lef tnode = rightnode) then
return false
return true
TreeSearch has O(height) running time (it goes down the tree starting from the root). Since

CS 16 Introduction to Algorithms and Data Structures Semester II, 9900


9

red-black trees have height O(log(n)), the running time of this algorithm is also O(log(n)) (as it
consists of two TreeSearches and some constant time tests).

CS 16 Introduction to Algorithms and Data Structures Semester II, 9900

Das könnte Ihnen auch gefallen