Sunteți pe pagina 1din 6

tree

Consider the insertion of items with the following keys (in the given order) into an initially empty
AVL tree: 30, 40, 24, 58, 48, 26, 11, 13. Draw the final tree that results

Consider the insertion of items with the following keys (in the given order) into an initially empty
splay tree: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18. Draw the final tree that results.

Answer the following questions so as to justify Theorem 2.8.

a. Draw a binary tree with height 7 and maximum number of external nodes.

b. What is the minimum number of external nodes for a binary tree with height h? Justify your
answer.

c. What is the maximum number of external nodes for a binary tree with height h? Justify your
answer.

d. Let T be a binary tree with height h and n nodes. Show that log ( n+1 ) −1≤ h ≤(n−1)/2

e. For which values of n and h can the above lower and upper bounds on h be attained with
equality?

Design algorithms for the following operations for a node v in a binary tree T:

Preorder Next(v) : return the node visited after v in a preorder traversal of T

Inorder Next(v) : return the node visited after v in an inorder traversal of T

Postorder Next(v) : return the node visited after v in a postorder traversal of T.

What are the worst-case running times of your algorithms?

Define the internal path length, I (T) , of a tree T to be the sum of the depths of all the internal nodes
in T. Likewise, define the external path length, E(T) , of a tree T to be the sum of the depths of all the
external nodes in T. Show that if T is a binary tree with n internal nodes, then E(T) = I(T) + 2n

insert items with the following keys (in the given order) into an initially empty binary search tree: 30,
40, 24, 58, 48, 26, 11, 13. Draw the tree after each insertion.

Consider the following sequence of keys: (5,16,22,45,2,10,18,30,50,12,1).

Consider the insertion of items with this set of keys, in the order given, into:

a. An initially empty (2,4) tree T ′ .

b. An initially empty red-black tree T ′′.

Draw T ′ and T ′′ after each insertion.

Explain why performing a rotation in an n-node binary tree represented using a sequence takes Ω(n)
time.
Draw an example red-black tree that is not an AVL tree. Your tree should have at least 6 nodes, but
no more than 16.

For each of the following statements about red-black trees, determine whether it is true or false. If
you think it is true, provide a justification. If you think it is false, give a counter example.

a. A subtree of a red-black tree is itself a red-black tree.

b. The sibling of an external node is either external or it is red.

c. Given a red-black tree T, there is an unique (2,4) tree T ′ associated with T.

d. Given a (2,4) tree T, there is a unique red-black tree T ′ associated with T.

Perform the following sequence of operations in an initially empty splay tree and draw the tree after
each operation.

a. Insert keys 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, in this order.

b. Search for keys 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, in this order.

c. Delete keys 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, in this order.

Design an algorithm for performing findAllElements(k) in an ordered dictionary implemented with an


ordered array, and show that it runs in time O(logn + s), where n is the number of elements in the
dictionary and s is the number of items returned.

Design an algorithm for performing the operation findAllElements(k) in an ordered dictionary


implemented with a binary search tree T, and show that it runs in time O(h+s), where h is the height
of T and s is the number of items returned.

Describe how to perform the operation removeAllElements(k) in an ordered dictionary implemented


with a binary search tree T, and show that this method runs in time O(h + s), where h is the height of
T and s is the size of the iterator returned.

Show that at most one node in an AVL tree becomes unbalanced after operation
removeAboveExternal is performed within the execution of a removeElement dictionary operation.

Consider a variation of splay trees, called half-splay trees, where splaying a node at depth d stops as
soon as the node reaches depth ⌊d/2⌋. Perform an amortized analysis of half-splay trees
Hashing

Draw the 11-item hash table resulting from hashing the keys 12, 44, 13, 88, 23, 94, 11, 39, 20, 16,
and 5, using the hash function h(i)=(2i+5) mod 11 and assuming collisions are handled by chaining.

What is the result of the previous exercise, assuming collisions are handled by linear probing?

Show the result of Exercise R-2.19, assuming collisions are handled by quadratic probing, up to the
point where the method fails because no empty slot is found.

What is the result of Exercise R-2.19 assuming collisions are handled by double hashing using a
secondary hash function h k 7 k mod 7 ?

Give a pseudo-code description of an insertion into a hash table that uses quadratic probing to
resolve collisions, assuming we also use the trick of replacing deleted items with a special
“deactivated item” object.

Show the result of rehashing the hash table shown in Figure 2.55 into a table of size 19 using the
new hash function h k 2k mod 19.

The hash table dictionary implementation requires that we find a prime number between a number
M and a number 2M. Implement a method for finding such a prime by using the sieve algorithm. In
this algorithm, we allocate a 2M cell Boolean array A, such that cell i is associated with the integer i.
We then initialize the array cells to all be “true” and we “mark off” all the cells that are multiples of
2, 3, 5, 7, and so on. This process can stop after it reaches a number larger than 2M.

Give the pseudo-code description for performing a removal from a hash table that uses linear
probing to resolve collisions where we do not use a special marker to represent deleted elements.
That is, we must rearrange the contents of the hash table so that it appears that the removed item
was never inserted in the first place.

The quadratic probing strategy has a clustering problem that relates to the way it looks for open
slots when a collision occurs. Namely, when a collision occurs at bucket h(k) , we check A[(h(k) +f(j))
mod N] , for f(j) =j2, using j=1, 2,…, N-1.
a. Show that f(j) mod N will assume at most (N+1)/2 distinct values, for N prime, as j ranges from 1 to
N-1. As a part of this justification, note that f(R) =f(N-R) for all R.

b. A better strategy is to choose a prime N such that N is congruent to 3 modulo 4 and then to check
the buckets A[(h(k) +f(j)) mod N] as j ranges from 1 to (N-1)/2, alternating between addition and
subtraction. Show that this alternate type of quadratic probing is guaranteed to check every bucket
in A.

Describe the limitations of using a linked list to store a collection of key-value pairs subject to
put(k,v) and get(k).

Give the pseudocode description for performing insertion, searching, and removal from a hash table
that uses linear probing to resolve collisions where we use a special marker to represent deleted
elements

In our description of hashing with the separate chaining rule, we assumed each cell in the array, A,
was a pointer to a linked list, which wastes space in the case where the list holds only one item.
Show how to modify our implementation so that we don’t waste space on additional pointers to
linked-list nodes for the cells in A that hold only one item.

Suppose that both the hash function, h, and the hash function, f, used in the double hashing open
addressing scheme are random functions. Show that the expected time to perform the get(k)
operation is O(1).

Dr. Wayne has a new way to do open addressing, where, for a key k, if the cell h(k) is occupied, then
he suggests trying (h(k)+i · f(k)) mod N, for i =1 ,2,3,...,until finding an empty cell, where f(k) is a
random hash function returning values from 1 to N −1. Explain what can go wrong with Dr. Wayne’s
scheme if N is not prime.

Skip list

Give a pseudo-code description of the removeElement dictionary operation, assuming the dictionary
is implemented by a skip-list structure.

Draw an example skip list resulting from performing the following sequence of
operationsontheskiplistinFigure19.18: remove(38),insert(8,x),insert(24,y), remove(55). Assume the
coin flips for the first insertion yield two heads followed by tails, and those for the second insertion
yield three heads followed by tails.

Giveapseudocodedescriptionoftheremovedictionaryoperation,assumingthe dictionary is
implemented by a skip-list structure.
Show that the 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
strictly top-down, scan-forward approach, without ever using the above or before methods.

Text Processing

Drawafigureillustratingthecomparisonsdonebythebrute-
forcepatternmatchingalgorithmforthecasewhenthetextis "aaabaadaabaaa" andthepattern is
"aabaaa".

Repeat the previous problem for the BM pattern matching algorithm, not counting the comparisons
made to compute the last function

RepeatthepreviousproblemfortheKMPpatternmatchingalgorithm,notcounting the comparisons made


to compute the failure function

Compute a table representing the last function used in the BM pattern matching algorithm for the
pattern string "the quick brown fox jumped over a lazy cat" assuming the following alphabet (which
starts with the space character): Σ={ ,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}

Compute a table representing the KMP failure function for the pattern string "cgtacgttcgtac"

Draw a standard trie for the following set of strings: {abab,baba,ccccc,bbaaaa,caa,bbaacc,cbcc,cbca}.

Draw a compressed trie for the set of strings given in Exercise R-23.8

Draw the compact representation of the suffix trie for the string "minimize minime".

What is the longest prefix of the string "cgtacgttcgtacg" that is also a suffix of this string?

Give an example of a text T of length n and a pattern P of length m that force the brute-force pattern
matching algorithm to have a running time that is Ω(nm).

Show how to modify the KMP string pattern matching algorithm so as to find every occurrence of a
pattern string P that appears as a substring in T, while still running in O(n + m) time. (Be sure to catch
even those matches that overlap.)

Show the longest common subsequence table, L, for the following two strings: X = "skullandbones" Y
= "lullabybabies". What is a longest common subsequence between these strings?

Draw the frequency table and Huffman tree for the following string: "dogs do not, spot tot pots or
cats".

Show how to use dynamic programming to compute the longest corniflou subsequâncè between the
twO strings "babbabab" and "bbabbaaab"

Give an example of a text T of length n and a pattern P of length m that force brute-force pattern
matching algorithm to have a running time that is Ω(nm)

Computational Geometry

What would be the worst-case space usage of a range tree, if the primary structure were not
required to have O(logn) height?
Argue that the algorithm for answering three-sided range-searching queries with a priority search
tree is correct.

What is the worst-cäse depth ola k-d tree d!fined on n points in the plane7 What about in higher
dimensions?

Suppose a set S contains n two-dimensional points whose coordinates are all integers in the range
[0,N]. What is the worst-case depth of a quadtree defined on S?

Draw a quadtree for the following set of points, assuming a 16 x 16 bounding box: {(1,2), (4,10),
(14,3), (6,6), (3,15), (2,2), (3,12), (9,4), (12, 14)}.

Construct a k-d tree fOr the. point set of Exercise R-12.14.

Construct a Priority Search tree fOr the. point set of Exercise R-12.14.

Give a pseudo-éode description of an algorithm for constructing a range tree from a set of n points
in the plane in O(nlogn) time.

Suppose we are given a range-searching data structure D that can answer rangesearching queries for
a set of n points in d-dimensionai space for any fixed dimension d (like 8, 10, or 20) in time that is
O(logd n + k), where k is the number of answers. Show how tO use D to änswer the following queries
for a set S of n rectangles in theplane:

findAIIContaining(,y): Return an enumeration of all rectangles in S that containthe point(x,y).

findAlllntersecting(a, b, c, d): Return an enumeration of all rectangles that intersect the rectangle
with x-range [a, b] and y-range [c, d].

What is the running time needed to answer each of these queries?

Describe an efficient method for inserting an object into a (balanced) priority search tree. What is
the running time of this method?

S-ar putea să vă placă și